Solved

SQL Group by

Posted on 2014-01-02
4
260 Views
Last Modified: 2014-01-02
Hello,
can you please help.
I'm trying to group this properly to get only 1 line per client.
but I get quite few.
here is the code

Select  Distinct 'Accountnumber' AS [Accountnumber],
 'WEB' AS [WEB],
 'CSR' AS [CSR]
        From FinalizedOrders
     WHERE 1 = 1
Union All
Select  CONVERT(varchar, Accountnumber, 1)  AS [Accountnumber],
(Select CONVERT (varchar, Count (Operator), 1) where Operator like 'web' ) AS [WEB] ,
(Select CONVERT (varchar, Count (Operator), 1) where Operator not like 'web' ) AS [CSR]
From FinalizedOrders
where accountnumber IN (423,116) and orderdate between '2013-01-01' and '2013-12-31'
Group by Accountnumber, Operator

Sample Results
Accountnumber      WEB      CSR
116                              NULL       31
116                              NULL      6
116                        NULL      30
423                             NULL      13
423                             NULL      15
116                             6679      NULL
423                             NULL      13
423                             474             NULL

it should be
Accountnumber      WEB      CSR
116                              6679      67
423                              474               31

thanks
0
Comment
Question by:W.E.B
[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
  • 3
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 300 total points
ID: 39752824
Modify the second select statement like this:
Select  CONVERT(varchar, Accountnumber, 1)  AS [Accountnumber],
CONVERT (varchar, Count (CASE WHEN Operator like 'web' THEN Operator ELSE NULL END)) AS [WEB] ,
CONVERT (varchar, Count (CASE WHEN Operator like 'web' THEN NULL ELSE Operator END))  AS [CSR]
From FinalizedOrders
where accountnumber IN (423,116) and orderdate between '2013-01-01' and '2013-12-31'
Group by Accountnumber, Operator

Open in new window

BTW, for the first select statement you do not need to do SELECT FROM, just use:
Select  'Accountnumber' AS [Accountnumber],
 'WEB' AS [WEB],
 'CSR' AS [CSR]

Open in new window

So, the whole query will be:
Select  'Accountnumber' AS [Accountnumber],
 'WEB' AS [WEB],
 'CSR' AS [CSR]
UNION ALL
SELECT [Accountnumber], [WEB], [CSR] FROM
(Select  CONVERT(varchar, Accountnumber, 1)  AS [Accountnumber],
CONVERT (varchar, Count (CASE WHEN Operator like 'web' THEN Operator ELSE NULL END)) AS [WEB] ,
CONVERT (varchar, Count (CASE WHEN Operator like 'web' THEN NULL ELSE Operator END))  AS [CSR]
From FinalizedOrders
where accountnumber IN (423,116) and orderdate between '2013-01-01' and '2013-12-31'
Group by Accountnumber, Operator) AS T

Open in new window

0
 

Author Comment

by:W.E.B
ID: 39752829
hello,
thanks for your help,
i still get more than one line,

sample
Accountnumber      WEB      CSR
423                                0              15
116                              6679      0
423                       0               13
423                         474                0
116                             0                50
116                             0                48
423                       0                6
423                            0                2
116                            0                31
0
 

Author Comment

by:W.E.B
ID: 39752850
OK, it's good,
I just needed to change
Group by Accountnumber,) AS T

thanks
0
 

Author Closing Comment

by:W.E.B
ID: 39752851
thank you
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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