Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Complex Select Statement

Posted on 2011-03-03
3
Medium Priority
?
529 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
[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
  • 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:
Ephraim Wangoya earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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