0

friends: I'm now have a problem about conditional calculation in SQL Server. I have set some data from SQL Server as an example in excel like this:

No    Employee  Month   Commission1 Commission2
1     A         Jan        10          5
2     A         Jan        10          4
3     B         Jan        15          3
4     B         Jan        15          4
5     C         Jan        10          3
6     C         Jan        10          4
7     D         Jan        13          3
8     D         Jan        13          4
9     DM        Jan        0           6
10    DM        Jan        0           8
11    A         Feb        15          4
12    A         Feb        15          5
13    B         Feb        20          5
14    B         Feb        20          4
15    C         Feb        9           3
16    C         Feb        9           4
17    D         Feb        14          5
18    D         Feb        14          6
19    DM        Feb        0           13
20    DM        Feb        0           10

And the result I want is like this:

Employee    Jan             No#     Feb             No#  
A           20              2       30              2  
B           30              2       40              2  
C           20              2       18              2  
D           26              2       28              2  
DM          44              10      59              10  

For every sales,Employee A,B,C,D only have commission1 as payment,the commission2 is for DM. So , in Jan , DM's commission is SUM(E2:E9)

I can do it easy in excel , but how can I do this in sql server?

I make my try code like this:

select [Month],Employee,SUM(Commission1) Commission,count(distinct([No]))     No#
from table1
WHERE Employee IN ('A','B','C','D')
group by [Month],Employee
union
select 'DM' as Employee,[Month],SUM(Commission2)     Commission,count(distinct([No])) No#
from table1
WHERE Employee IN ('A','B','C','D','DM')
group by [Month],Employee

And I get the result

Employee    Month   Commission  No#
A           Jan      20         2
B           Jan      30         2
C           Jan      20         2
D           Jan      26         2
DM          Jan      44         10
A           Feb      30         2
B           Feb      40         2
C           Feb      18         2
D           Feb      28         2
DM          Feb      59         10

The result format is not what I want.I tried pivot after this query,but failed,it seems I only can pivot one state?

Another question: If I want the month growth automatic (In actual data , there's not only Jan and Feb) in the result ,not write [Jan],[Feb],[Mar]... in pivot code, how to do it?

Who can help me?

Thanks!

5
  • 2
    Posting pictures of data makes it really hard to create testdata in order to solve your problem. Can't use cut and paste Commented Dec 3, 2015 at 8:52
  • @t-clausen.dk is this OK? I'm very sorry for that I don't know how to organize my page... and how to give a good format data.
    – Walker.li
    Commented Dec 3, 2015 at 9:06
  • I edited the format of your question and removed the redundant pictures. You should also include your attempt to solve it. Commented Dec 3, 2015 at 9:11
  • Thank you very much! I will add my attempt later,Please help!
    – Walker.li
    Commented Dec 3, 2015 at 9:17
  • 1
    You need to add the attempt when you post the question, not after..
    – AStopher
    Commented Dec 3, 2015 at 9:26

2 Answers 2

1

Here is a PIVOT solution:

Test data:

DECLARE @t table(Employee varchar(2), Month char(3), Commission1 int, Commission2 int)
INSERT @t values
('A','Jan',10,5 ),('A','Jan',10,4),('B','Jan',15,3),
('B','Jan',15,4 ),('C','Jan',10,3),('C','Jan',10,4),
('D','Jan',13,3 ),('D','Jan',13,4),('DM','Jan',0,6),
('DM','Jan',0,8 ),('A','Feb',15,4),('A','Feb',15,5),
('B','Feb',20,5 ),('B','Feb',20,4),('C','Feb',9,3),
('C','Feb',9,4  ),('D','Feb',14,5),('D','Feb',14,6),
('DM','Feb',0,13),('DM','Feb',0,10)

Query:

;WITH CTE as
( 
 SELECT
   Employee, Month, 
   CASE WHEN Employee = 'DM' THEN
   SUM(Commission2) over (partition by [Month])
   ELSE Commission1 END com,
   CASE WHEN Employee = 'DM' 
   THEN row_number() over
      (PARTITION BY Employee, [Month] ORDER BY (SELECT 1)) ELSE 1 END rn
 FROM @t
) 
SELECT Employee, [Jan], [Feb], [Mar] -- add more months
FROM  
CTE
PIVOT 
(SUM(com) 
 FOR [Month] IN ([Jan], [Feb], [Mar])) AS pvt -- add more months
 WHERE rn = 1

Result:

Employee    Jan Feb Mar
A           20  30  NULL
B           30  40  NULL
C           20  18  NULL
D           26  28  NULL
DM          44  59  NULL
3
  • Thank you very much! But I changed some requests,how should I do with pivot 2 columns?
    – Walker.li
    Commented Dec 3, 2015 at 10:24
  • That is a huge change, you should not change the requirements of the question after posting it, that makes all the answers invalid Commented Dec 3, 2015 at 10:29
  • OK , get it ! I would not do that again ! But can you help me again for this question?Thanks a lot!
    – Walker.li
    Commented Dec 3, 2015 at 10:40
0

In SqlServer you can do so using PIVOT operator as like below:
Please refer PIVOT syntax

select tmp.employee,pv.[jan] as Jan_Commission, pv.[feb] as Feb_Commission from 

(
select employee,month,commission1 from table_name

)tmp
pivot (
sum(commission1)  
for [month] in ([jan],[feb])

)pv;
1
  • Downvoter pls mention the reason, so that we will correct the code
    – Chanukya
    Commented Dec 3, 2015 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.