SQL Server, how to get younger users?

sql server list database users and permissions
sql server get list of users
sql query to find age from date of birth in sql
sql server list logins and users
sql server list all users in all databases
query to get login details in sql server
sql server logins, users and roles
sql server database users

I'm trying to get users from a younger country for example I have the following tables.

If there is more than one user of the youngest who have the same age, they should also be shown

Thanks

You can try this query, get MIN birthday on subquery then self join on users table.

select u.idcountry,t.name,u.username, (DATEPART(year, getdate()) - t.years) 'age'
from 
(
SELECT u.idcountry,c.name,DATEPART(year, u.birthday) as 'years',count(*) as 'cnt'
FROM users u inner join country c on u.idcountry = c.idcountry
group by u.idcountry,c.name,DATEPART(year, u.birthday)
) t inner join users u on t.idcountry = u.idcountry and t.years = DATEPART(year, u.birthday)
where t.cnt > 1

sqlfiddle:https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=9baab959f79b1fa8c28ed87a8640e85d

Mastering SQL Server 2000 Security, Mike Young, Curtis W. Young. After the When connecting, you need to supply a trusted connection to SQL Server. If you configure permission at the table level, then all applications a user can use to connect to the table will have the same  Looking for sql server software? Get your questions answered. Discover sql server software and related content.

Use the rank() window function:

select ...
from ...
where rank() over (partition by idcountry order by birthday) = 1

Rows with the same birthday in a country are ranked the same, so this returns all youngest people with if there’s more than one.

Computerworld, 2) Users can contact a support rep to sort through options and find the best With HelpStar, which nins on a Microsoft SQL Server 2000 database, a new call or action undertaken by the help desk, says Gemma Young, product manager at  Fast answers on Teoma.us! Find sql server software. Content updated daily for sql server software.

This is a little tricky. I would use window functions -- count the people of a particular age and choose the ones where there are duplicates for the youngest.

You don't specify how to define age, so I'll just use the earliest calendar year:

select u.*
from (select u.*,
             count(*) over (partition by idcountry, year(birthday)) as cnt_cb,
             rank() over (partition by idcountry order by year(birthday)) as rnk
      from users u
     ) u
where cnt_cb > 1 and rnk = 1;

I'll let you handle the joins to bring in the country name.

Microsoft SQL Server 2000 Unleashed, Verb Phrasing By default, English Query assumes that the basic verb have will describe like in this example: Which movies were rented by young customers? Find Sql Server With As. Explore Other Results at ConsumerSearch.com

Your sample data and desired results show the oldest users within each country when more than one of the oldest have the same age. The query below will do that, assuming age is calculated using full birth date.

WITH
    users AS (
        SELECT 
              username
            , birthday
            , idcountry
            , (CAST(CONVERT(char(8),GETDATE(),112) AS int) - CAST(CONVERT(char(8),birthday,112) AS int)) / 10000 AS age
            , RANK() OVER(PARTITION BY idcountry ORDER BY (CAST(CONVERT(char(8),GETDATE(),112) AS int) - CAST(CONVERT(char(8),birthday,112) AS int)) / 10000 DESC) AS age_rank
        FROM dbo.Users
    )
    , oldest_users AS (
        SELECT
              username
            , birthday
            , idcountry
            , age
            , COUNT(*) OVER(PARTITION BY idcountry, age_rank ORDER BY age_rank) AS age_count
        FROM users
        WHERE age_rank = 1
    )
SELECT
      c.idcountry
    , c.name
    , oldest_users.age
    , oldest_users.username
FROM oldest_users
JOIN dbo.Country AS c ON c.idcountry = oldest_users.idcountry
WHERE
    oldest_users.age_count > 1;

SELECT SQL QUERY USING CURRENT DATE TIMETO GET THE , Hi, I have a datable with a Field Birthday which is of Type DateTime and stores the Users Birth date. On my aspx page, I want to display the  99% Match on Sql Server Buy. Start searching with Visymo.com.

SQL Server: Find Users in SQL Server, Is there a query to run in SQL Server that will return all Users created? In SQL Server, there is a system view called sys.sysusers. You can run a query against  The CURRENT_USER function returns the name of the current user in the SQL Server database. Technical Details. SQL Server (starting with 2008), Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse. SQL Server Functions. If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

How to transfer logins and passwords between instances of SQL , In this article, server A and server B are different servers. After you move a database from the instance of SQL Server on server A to the instance  A. Using CURRENT_USER to return the current user name. This example returns the name of the current user. SELECT CURRENT_USER; GO B. Using CURRENT_USER as a DEFAULT constraint. This example creates a table that uses CURRENT_USER as a DEFAULT constraint, for the order_person column, on a sales row.

How to Find and Fix Orphaned Users in SQL Server, Script to generate Alter USER script which can be used to map all orphan users with logins in all databases: exec sp_msforeachdb ' use ? select '  Answer: In SQL Server, there is a system view called sys.database_principals. You can run a query against this system view that returns all of the Users that have been created in SQL Server as well as information about these Users. To retrieve all Users in SQL Server, you can execute the following SQL statement:

Comments
  • Please don't post your tables as images. Use formatted text for this.
  • Show the query you've tried..
  • How do you define age?
  • @gordon I think we have a language barrier here. Based on the example, I think OP wants the youngest, ie eariest, birthday, so the oldest people.
  • @Bohemian . . . I wasn't actually referring to youngest vs. oldest. Ages don't have to be counted in complete years, so the calculation is not precise.
  • This query uses birth year rather than age. Users born the same year will have different ages depending on whether the month and day are before or after today.
  • Most commonly, age isn't just calculated from the first of the year in which they were born, but also the month and day. If my birthday were today, I would be one year older than I was yesterday.
  • @DanGuzman . . . I wouldn't necessarily just use years for counting ages. The OP's example returns as a tie two people born in the same year. The OP should clarify the definition. Note: if the OP uses datediff() in SQL Server, it is equivalent to the definition I am using.