Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Return rows that match the first row

Posted on 2016-08-01
3
Medium Priority
?
69 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
Brian Chan earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

730 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