Solved

Complex Select Statement

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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