Solved

Complex Select Statement

Posted on 2011-03-03
3
520 Views
Last Modified: 2012-05-11
I an trying to assemble a query that does the following in psudo code but in a single query without using a temp table.  I'm not sure how to do it.  Can someone help?

Query 1
SELECT Distinct UserID
FROM UserTable

Query 2
Start Loop of results from query 1)
  SELECT count(*) as RecordCount
  FROM UserTable
  WHERE UserID = UserID (from query 1)
End Loop of results from query 1)

I put all the results of query 2 into a temp  table.

Then I query the temp table as follows.

SELECT UserID
FROM Query2ResultSet
ORDER BY RecordCount desc
Display Top 10

Thanks in advance.
0
Comment
Question by:SFTProd
  • 2
3 Comments
 

Author Comment

by:SFTProd
ID: 35032633
I meant the above was in psudo code.  I am trying to put together a real sql query that will run in MSSQL 2008 R2.

Thanks.
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35032650
 SELECT top 10 UserID, count(*) as RecordCount
  FROM UserTable
  group by UserID
  order by count(*) desc
0
 

Author Comment

by:SFTProd
ID: 35032701
Thank You!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

17 Experts available now in Live!

Get 1:1 Help Now