Solved

Count query problem

Posted on 2012-04-05
2
259 Views
Last Modified: 2012-04-05
Suppose I have a table like this,


ProcessDate          FileID
2011-01-01            A0101
2011-01-01            B0101
2011-01-01            C0101
2011-01-02            A0102
2011-01-02            B0102
...
2011-02-01            A0201
2011-02-02      A0202

etc for each day or month where the number of FileID's per day can vary.

I would like to find the month with the maximum number of FileID's

I can write a query like this,


select
Month(ProcessDate) as [Mth],
Count(distinct TRAN_FileID) as [FileCount]
from MyTable
where ProcessDate >= '2011-01-01'
and ProcessDate < '2012-01-01'
Group by Month(ProcessDate)
order by Month(ProcessDate)

from which I can see the maximum number of files for each month

eg.

1            20
2            35
3            10
4            55
etc

Now I want to get the maximum so tried using this query as subquery like this

select Max(FileCount)
from
(
select
Month(ProcessDate) as [Mth],
Count(distinct TRAN_FileID) as [FileCount]
from MyTable
where ProcessDate >= '2011-01-01'
and ProcessDate < '2012-01-01'
Group by Month(ProcessDate)
order by Month(ProcessDate)
)

Unfortunately this gives me this error,
"The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified."

How I can I find the month with the maximum number of FileIDs using MS SQL Server 2005?
0
Comment
Question by:DaveChoiceTech
  • 2
2 Comments
 
LVL 13

Expert Comment

by:Ashok
ID: 37812160
I think this should work.....

select Max(FileCount)
from
(
select
Month(ProcessDate) as [Mth],
Count(distinct TRAN_FileID) as [FileCount]
from MyTable
where ProcessDate >= '2011-01-01'
and ProcessDate < '2012-01-01'
Group by Month(ProcessDate)
)

HTH
Ashok
0
 
LVL 13

Accepted Solution

by:
Ashok earned 500 total points
ID: 37812763
select Mth, FileCount
from
(
select
Month(ProcessDate) as [Mth],
Count(distinct TRAN_FileID) as [FileCount]
from MyTable
where ProcessDate >= '2011-01-01'
and ProcessDate < '2012-01-01'
Group by Month(ProcessDate)
) a
WHERE FileCount = (
select Max(FileCount)
from
(
select
Month(ProcessDate) as [Mth],
Count(distinct TRAN_FileID) as [FileCount]
from MyTable
where ProcessDate >= '2011-01-01'
and ProcessDate < '2012-01-01'
Group by Month(ProcessDate)
) b );
 
HTH
Ashok
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql help 5 52
Order by but want it in specific order 2 33
Bubble user-defined Sql RAISERROR(...) to c# exception 14 166
Addition to SQL for dynamic fields 6 36
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

815 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

9 Experts available now in Live!

Get 1:1 Help Now