Solved

SQL Query COUNT function

Posted on 2010-11-19
11
417 Views
Last Modified: 2012-05-10
Hello Exports,
I need some help in trying to run a query on our SQL server that will count several fields. I tried to do this in simple statment but got no where, so I created the following stored procedure which get me the correct count but the the software I created that for cannot recognize stored procedures.
Is there any other way to do the below in a SELECT query?

SELECT COUNT (field1) AS Total, AddedOn FROM applications Group By AddedOn
Select COUNT (field2) As ADTotal FROM Applications where field2 = 1 GROUP BY AddedOn, Field2

What I am trying to do is to count several fields within the same table and group them by date received so the results will be something like
Date   Total   ADTotals
09/01     2           4
09/01     1            8
09/02      5          30
I need to count total of six fields in the same table.
Thank you
Roy  
0
Comment
Question by:rfinaly
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 13

Expert Comment

by:Superdave
ID: 34177852
You could combine them something like this (untested):

SELECT COUNT(field2) as ADTotal,AddedOn,
  (SELECT COUNT(field1) FROM Applications AS A2 WHERE A1.AddedOn=A2.AddedOn) AS Total FROM Applications AS A1;
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 34177867
If you are grouping by the date, why is there 2 records with the same date in your sample output?

Do you want to count distinct (number of unique) values for each field?

Can you provide some sample data and the expected output?
0
 
LVL 13

Accepted Solution

by:
Superdave earned 250 total points
ID: 34177906
I meant to put a GROUP BY on that, like this:

SELECT COUNT(field2) as ADTotal,AddedOn,
  (SELECT COUNT(field1) FROM Applications AS A2 WHERE A1.AddedOn=A2.AddedOn) AS Total FROM Applications AS A1 GROUP BY AddedOn;
0
 
LVL 14

Expert Comment

by:leoahmad
ID: 34177917
well, better is, provide some data and required results.
0
 

Author Comment

by:rfinaly
ID: 34177921
Attached is an excel file with fields and results set, hope that helps.
Thank you in advance
Roy
Example.xlsx
0
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.

 
LVL 22

Assisted Solution

by:Thomasian
Thomasian earned 250 total points
ID: 34177936

DECLARE @t table (AddedOn datetime, Admit bit)



INSERT INTO @t

SELECT '11/1/2010', 1

UNION ALL SELECT '11/1/2010', 1

UNION ALL SELECT '11/12/2010', 0

UNION ALL SELECT '11/12/2010', 1

UNION ALL SELECT '11/12/2010', 0

UNION ALL SELECT '11/12/2010', 1



SELECT COUNT(1) 'Total Records Per Day'

      ,COUNT(CASE WHEN Admit=1 THEN 1 END) 'Total Admits'

      ,AddedOn

FROM @t

GROUP BY AddedOn

Open in new window

0
 

Author Comment

by:rfinaly
ID: 34177937
SuperDave your query works if I made those changes.
SELECT COUNT(field2) as ADTotal,AddedOn,
  (SELECT COUNT(field1) FROM Applications AS A2 WHERE A1.AddedOn=A2.AddedOn AND field2=1) AS Total FROM Applications AS A1 GROUP BY AddedOn;
Take a look at the Excel I submitted and see if we are on the right track.
Thank you very much
0
 

Author Comment

by:rfinaly
ID: 34178012
Thomas: Do I need the insert statment to run your query?
Thank you
Roy
0
 
LVL 22

Expert Comment

by:Thomasian
ID: 34178030
No, line 1-9 are just code to test the query.

You only need code from line 11-15 and  change @t to your actual table name.
0
 
LVL 13

Expert Comment

by:Superdave
ID: 34178082
The query looks right (like what I meant).  I don't have a way of easily interpreting weird xml formats so I can't look at it, but what if anything more do you want done with the query?
0
 

Author Closing Comment

by:rfinaly
ID: 34189148
Thank you very much, both solutions works very well.
I appriciate your time.
Roy
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.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

20 Experts available now in Live!

Get 1:1 Help Now