Solved

Return rows that match the first row

Posted on 2016-08-01
3
39 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 40

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How many Lat/Long for a single UK Postcode 5 12
Caste datetime 2 25
select over clause 1 15
Minus first query 1 6
In this article I will describe the Copy Database Wizard 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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now