Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


counting / group by confusion


counting / group by confusion

Author
Message
inevercheckthis2002
inevercheckthis2002
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 448
My testtable:

USE TestDB
GO
CREATE TABLE testtable
(
ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED,
CompanyNumber nvarchar(50),
OrderDate date,
ProductOrdered nvarchar(50)
);

INSERT testtable
SELECT
101, '2014-04-22', 'ProdA' UNION ALL SELECT
101, '2014-04-22', 'ProdA' UNION ALL SELECT
101, '2014-04-22', 'ProdA' UNION ALL SELECT
101, '2014-04-23', 'ProdA' UNION ALL SELECT
102, '2014-04-22', 'ProdA' UNION ALL SELECT
102, '2014-04-22', 'ProdA' UNION ALL SELECT
102, '2014-04-22', 'ProdB' UNION ALL SELECT
103, '2014-04-22', 'ProdA' UNION ALL SELECT
103, '2014-04-23', 'ProdB' ;



A simple select:

SELECT CompanyNumber, OrderDate, ProductOrdered
FROM dbo.testtable

CompanyNumber OrderDate ProductOrdered
--------------- ---------- ---------------
101 2014-04-22 ProdA
101 2014-04-22 ProdA
101 2014-04-22 ProdA
101 2014-04-23 ProdA
102 2014-04-22 ProdA
102 2014-04-22 ProdB
102 2014-04-22 ProdB
103 2014-04-22 ProdA
103 2014-04-23 ProdB



There are two things that I'd like to count. One is the number of orders per day. The other is the number of each Product ordered per day.

Desired result

CompanyNumber OrderDate OrderCount ProdA ProdB
--------------- -------- ----------- ----- -----
101 2014-04-22 3 3 0
101 2014-04-23 1 1 0
102 2014-04-22 3 1 2
103 2014-04-22 1 1 0
103 2014-04-23 1 0 1




I think that I'm getting close.. But, where do I go from here.


SELECT CompanyNumber, COUNT (OrderDate) as OrderCount
FROM dbo.testtable
GROUP BY CompanyNumber, OrderDate


CompanyNumber OrderCount
--------------- -----------
101 3
102 3
103 1
101 1
103 1


Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6693 Visits: 17689
Unless I missed something, you just have to add an order by clause.Cool
John Mitchell-245523
John Mitchell-245523
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7414 Visits: 15114
Here's how to get the results you need:
SELECT CompanyNumber, ProductOrdered, OrderDate, COUNT(*) as OrderCount
FROM dbo.testtable
GROUP BY CompanyNumber, OrderDate, ProductOrdered



As you'll notice, they're not presented in the way you need. Many would argue that presentation should be done in the presentation layer, so you have two options:
(1) Read about PIVOTs and cross-tabs in order to denormalise your result set
(2) Let your front end do the pivoting. Excel, for example, can do that.

John
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18113
More than a simple order by is needed. You need to use CROSS TABS (or pivot if you want to complicate yourself :-P).
Here's a nice article on them: http://www.sqlservercentral.com/articles/T-SQL/63681/
And here's the example:

SELECT CompanyNumber, 
OrderDate,
COUNT (ProductOrdered) as OrderCount,
COUNT( CASE WHEN ProductOrdered = 'ProdA' THEN ProductOrdered END) ProdA,
COUNT( CASE WHEN ProductOrdered = 'ProdB' THEN ProductOrdered END) ProdB
FROM dbo.testtable
GROUP BY CompanyNumber, OrderDate
ORDER BY CompanyNumber, OrderDate




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Eirikur Eiriksson
Eirikur Eiriksson
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6693 Visits: 17689
Luis Cazares (4/25/2014)
More than a simple order by is needed. You need to use CROSS TABS (or pivot if you want to complicate yourself :-P)

I really did miss something (he he :WhistlingSmile
inevercheckthis2002
inevercheckthis2002
SSC Journeyman
SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)SSC Journeyman (99 reputation)

Group: General Forum Members
Points: 99 Visits: 448
Luis Cazares (4/25/2014)
More than a simple order by is needed....]

Luis,

Thank you very much for the solution and for the reference.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8506 Visits: 18113
inevercheckthis2002 (4/25/2014)
Luis Cazares (4/25/2014)
More than a simple order by is needed....]

Luis,

Thank you very much for the solution and for the reference.


You're welcome. Be sure to understand how the code works and ask any questions that you have.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search