Solved

Complex Select Statement

Posted on 2011-03-03
3
522 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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