Solved

Return rows that match the first row

Posted on 2016-08-01
3
58 Views
Last Modified: 2016-08-01
I have a query that returns 1 row, it calculates total hours for each person and returns the top result. But I need to return any other rows that match the LastWeekTotal in the first row.

With UserStats (Username, LastWeekTotal) as (  
Select strDomainUsername, dbo.GetLastWeekBilling(strDomainUsername) from tblDomainAccountMap)
Select TOP 1 * from UserStats Order By LastWeekTotal DESC
0
Comment
Question by:mgordon-spi
3 Comments
 
LVL 5

Accepted Solution

by:
Brian Chan earned 500 total points
ID: 41738262
Try this one:
WITH UserStats (Username, LastWeekTotal) AS
(  
	SELECT 
		strDomainUsername
		, dbo.GetLastWeekBilling(strDomainUsername) 
	FROM tblDomainAccountMap
) 
SELECT * 
FROM UserStats 
WHERE lastweektotal = (SELECT max(lastweektotal) from UserStats)
ORDER BY Username

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 41738298
;With UserStats (Username, LastWeekTotal) as (  
Select strDomainUsername, dbo.GetLastWeekBilling(strDomainUsername) from tblDomainAccountMap),
    UserStats2 AS (SELECT *,DENSE_RANK() OVER(ORDER BY LastWeekTotal) rn FROM UserStats)
Select Username, LastWeekTotal from UserStats WHERE rn = 1

Open in new window

0
 

Author Closing Comment

by:mgordon-spi
ID: 41738304
Thank you, this worked perfectly for me.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question