如何加入多个"选择"声明

时间:2016-06-15 09:53:29

标签: sql-server sql-server-2008 select join

我有三张桌子:

1.Actions:

ActionCode IsBuy Is Sell 
1            1       0
2            1       0
3            0       1
4            1       0
5            1       0
6            0       1

2.ShareType:

Share IsStock IsBond IsOption  
    1       1        0       0
    2       1        0       0 
    3       0        1       0 
    4       1        0       0
    5       1        0       0 
    6       0        1       0

两个表都没有重叠。意思是,期权不能成为股票,买入不能成为卖出等。

和这样的表 - 我使用表1和表2创建的交易(见上文)

Date AccUser Amount IsBuy IsSell IsStock IsBond IsOption 
1.1.14  132   400     1      0      0      1      0
1.1.14  132   200     1      0      0      1      0
1.1.14  132   500     1      0      0      0      1
2.1.14  133   250     0      1      1      0      1

现在,我需要为每个action和sharetype组合执行一组描述性统计计算:

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt1
, sum(Amount) over (partition by AccUser,Date) as Amnt1
from transaction 
where IsBuy = 1 and IsBond = 1 

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt2
, sum(Amount) over (partition by AccUser,Date) as Amnt2
from transaction 
where IsBuy = 1 and IsStock = 1 

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt3
, sum(Amount) over (partition by AccUser,Date) as Amnt3
from transaction 
where IsBuy = 1 and IsOption = 1 


Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt4
, sum(Amount) over (partition by AccUser,Date) as Amnt4
from transaction 
where IsSell = 1 and IsBond = 1 

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt5
, sum(Amount) over (partition by AccUser,Date) as Amnt5
from transaction 
where IsSell = 1 and IsStock = 1 

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt6
, sum(Amount) over (partition by AccUser,Date) as Amnt6
from transaction 
where IsSell = 6 and IsOption = 6 

我在这里只写了一份分享类型和行动样本。 无论如何,我现在有很多"选择" (见上文)我需要加入所有这些来创建这个表:

Date AccUser cnt1 amnt1 cnt2 amnt2 cnt3 amnt3 cnt4 amnt4 cnt5 amnt5 cnt6 amnt6
1.1.14 132     2   600    0    0     1    500  0    0      0     0    0    0  
2.1.14 133     0    0     0    0     0     0   0    0      1    250   0    0

任何加入所有这些的好方法"选择"?感谢' S!

2 个答案:

答案 0 :(得分:0)

join这里不起作用简单使用union all all as this

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt1
, sum(Amount) over (partition by AccUser,Date) as Amnt1
from transaction 
where IsBuy = 1 and IsBond = 1 
UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt2
, sum(Amount) over (partition by AccUser,Date) as Amnt2
from transaction 
where IsBuy = 1 and IsStock = 1 

答案 1 :(得分:0)

尝试使用UNION ALL

Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt1
, sum(Amount) over (partition by AccUser,Date) as Amnt1
from transaction 
where IsBuy = 1 and IsBond = 1 
UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt2
, sum(Amount) over (partition by AccUser,Date) as Amnt2
from transaction 
where IsBuy = 1 and IsStock = 1 
UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt3
, sum(Amount) over (partition by AccUser,Date) as Amnt3
from transaction 
where IsBuy = 1 and IsOption = 1 

UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt4
, sum(Amount) over (partition by AccUser,Date) as Amnt4
from transaction 
where IsSell = 1 and IsBond = 1 
UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt5
, sum(Amount) over (partition by AccUser,Date) as Amnt5
from transaction 
where IsSell = 1 and IsStock = 1 
UNION ALL
Select Date, AccUser, count(AccUser) Over (Partition by AccUser, Date) as Cnt6
, sum(Amount) over (partition by AccUser,Date) as Amnt6
from transaction 
where IsSell = 6 and IsOption = 6