Solved

Handle a SUM and COUNT through 3 GROUPINGS.

Posted on 2013-11-07
6
405 Views
Last Modified: 2013-11-09
Can you suggest if the below can be bettered or simplified.. the logic is as below:

The first 3 queries are put into 3 temp tables A B and C. The only difference between the 3 queries are in the HAVING clause.

Because we are doing a SUM on Col10, I was not able to do a COUNT on another column (Col0) on the same query.

So I am doing my count on Col3, in three seperate queries from A, B, C temp tables - doing a UNION on them, and then grouping them again with 3 SUM operations.

thanks-
0
Comment
Question by:25112
[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
  • 4
6 Comments
 
LVL 5

Author Comment

by:25112
ID: 39630787
SELECT COL0 GroupColumn, COL1 FirstColumn, COl2 SecondColumn, COL3 ThirdColumn, COL4 FourthColumn, (COL5+COL6-COL7+COL8-COL9) FifthColumn
into A
from
JOIN & Conditions
GROUP BY COL0 ,COL1 ,COl2 ,COL3 ,COL4 ,(COL5+COL6-COL7+COL8-COL9)
HAVING sum(COL10)> (COL5+COL6-COL7+COL8-COL9)

------------
SELECT COL0 GroupColumn, COL1 FirstColumn, COl2 SecondColumn, COL3 ThirdColumn, COL4 FourthColumn, (COL5+COL6-COL7+COL8-COL9) FifthColumn
into B
from
JOIN & Conditions
GROUP BY COL0 ,COL1 ,COl2 ,COL3 ,COL4 ,(COL5+COL6-COL7+COL8-COL9)
HAVING sum(COL10)= (COL5+COL6-COL7+COL8-COL9)

---------------
SELECT COL0 GroupColumn, COL1 FirstColumn, COl2 SecondColumn, COL3 ThirdColumn, COL4 FourthColumn, (COL5+COL6-COL7+COL8-COL9) FifthColumn
INTO C
from
JOIN & Conditions
GROUP BY COL0 ,COL1 ,COl2 ,COL3 ,COL4 ,(COL5+COL6-COL7+COL8-COL9)
HAVING sum(COL10)< (COL5+COL6-COL7+COL8-COL9)


---------------

SELECT FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn,
SUM(EighthColumn) [6thCol],
SUM(SeventhColumn) [7thCol],
SUM(SixthColumn) [8thCol]
from
 

(
select FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn,  COUNT(COL0) SixthColumn, 0 SeventhColumn, , 0 EighthColumn from A
group by FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn

UNION

select FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn,  0 SixthColumn, COUNT(COL0) SeventhColumn, , 0 EighthColumn from B
group by FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn

UNION

select FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn,  0 SixthColumn, 0 SeventhColumn, , COUNT(COL0) EighthColumn from C
group by FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn
) A

GROUP BY FirstColumn, SecondColumn, ThirdColumn, FourthColumn, FifthColumn



drop table A
drop table B
drop table C
0
 
LVL 5

Author Comment

by:25112
ID: 39631164
i have edited the above with a CTE so the code is smaller.. can you suggest any improvements/suggestions?

;WITH CTE as
(
select  COL0,COL1 , COL2, COL3, COL4,  ((COL5+  COL6) - ( COL7 + COL8+ COL9)) COL10 ,sum(COL11)  COL11

from

JOIN

ON

where

group by COL0, COL1,COL2,COL4,COL3,((COL5+ COL6) -( COL7 + COL8+ COL9))
)

---------------------

SELECT COL3, COL4,  COL1, COL2, COL10,
SUM(EighthColumn) [6thCol],
SUM(SeventhColumn) [7thCol],
SUM(SixthColumn) [8thCol]
from
(
select COL1, COL2, COL3, COL4, COL10,  COUNT(COL0) SixthColumn, 0 SeventhColumn, 0 EighthColumn from CTE WHERE COL11 > COL10 group by COL1, COL2, COL3, COL4, COL10 union
select COL1, COL2, COL3, COL4, COL10,  0 SixthColumn, COUNT(COL0) SeventhColumn, 0 EighthColumn from CTE WHERE COL11 = COL10 group by COL1, COL2, COL3, COL4, COL10 union
select COL1, COL2, COL3, COL4, COL10,  0 SixthColumn, 0 SeventhColumn, COUNT(COL0) EighthColumn from CTE WHERE COL11 < COL10 group by COL1, COL2, COL3, COL4, COL10
)a
GROUP BY COL1, COL2, COL3, COL4, COL10
 ORDER BY COL1
0
 
LVL 32

Expert Comment

by:awking00
ID: 39631170
It seems that there should be a simpler way. Can you provide some sample relevant data for your beginning table structures and your expected output withe some description of how your results are to be generated?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 5

Author Comment

by:25112
ID: 39631343
king, please create this table..

CREATE TABLE CTE (COL0 INT,COL1 VARCHAR(11) , COL2 INT, COL3 VARCHAR(11), COL4 INT,COL10 SMALLINT,COL11 SMALLINT)

then upload the attached csv to it please using Export/Import.

then run this:


SELECT COL3, COL4,  COL1, COL2, COL10,
SUM(EighthColumn) [6thCol],
SUM(SeventhColumn) [7thCol],
SUM(COL0) [8thCol]
from
(
select COL1, COL2, COL3, COL4, COL10,  COUNT(COL0) COL0, 0 SeventhColumn, 0 EighthColumn from CTE WHERE COL11 > COL10 group by COL1, COL2, COL3, COL4, COL10 union
select COL1, COL2, COL3, COL4, COL10,  0 COL0, COUNT(COL0) SeventhColumn, 0 EighthColumn from CTE WHERE COL11 = COL10 group by COL1, COL2, COL3, COL4, COL10 union
select COL1, COL2, COL3, COL4, COL10,  0 COL0, 0 SeventhColumn, COUNT(COL0) EighthColumn from CTE WHERE COL11 < COL10 group by COL1, COL2, COL3, COL4, COL10
)a
GROUP BY COL1, COL2, COL3, COL4, COL10
 ORDER BY 3
CTE.csv
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39632793
It can be done by a single query, just use case expressions inside the COUNT() function
SELECT
        COL1
      , COL2
      , COL3
      , COL4
      , COL10
      , count(CASE WHEN COL11 < COL10 THEN COL0 END) [6thCol_11_lt_10]
      , count(CASE WHEN COL11 = COL10 THEN COL0 END) [7thCol_10_eq_11]
      , count(CASE WHEN COL11 > COL10 THEN COL0 END) [8thCol_11_gt_10]
FROM CTE
GROUP BY
        COL1
      , COL2
      , COL3
      , COL4
      , COL10
ORDER BY
        COL1
;

Open in new window

as an additional note, if you do ever need to use multiple queries like you have been doing so far AND you are summing those values do NOT use "UNION"

You should have been using UNION ALL

UNION by itself can make rows 'disappear' and your sum() could be wrong

UNION ALL does not make rows disappear, and it is faster too.
0
 
LVL 5

Author Comment

by:25112
ID: 39635770
nice!thx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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