Create table tran1(Transactionid int , Customerid int , Type varchar(100))
insert into tran1(Transactionid , Customerid , Type) values
(1 , 100 , 'Card '),
(2 , 101 , 'Cash'),
(3 , 102 , 'Card '),
(4 , 103 , 'Cash '),
(5 , 101 , 'Card ')
alter table tran1 add NewType varchar(100)
Update tran1 set NewType ='Only card' where Customerid IN (
select d.custid from (
select Customerid as custid,SUM(case when [Type]='Card' then 1 else 0 end) card
,SUM(case when [Type]='Cash' then 1 else 0 end) cash
from tran1
group by Customerid)d
where d.card=1
)
Update tran1 set NewType ='Only Cash' where Customerid IN (
select d.custid from (
select Customerid as custid,SUM(case when [Type]='Card' then 1 else 0 end) card
,SUM(case when [Type]='Cash' then 1 else 0 end) cash
from tran1
group by Customerid)d
where d.cash=1
)
Update tran1 set NewType ='Both' where Customerid IN (
select d.custid from (
select Customerid as custid,SUM(case when [Type]='Card' then 1 else 0 end) card
,SUM(case when [Type]='Cash' then 1 else 0 end) cash
from tran1
group by Customerid)d
where d.card=1 and cash=1
)
該值不僅在您運行它的時間點有效嗎?如果他們以後用另一種方法支付,會怎樣在需要時通過查詢提取該值是否更好? – 2014-11-04 17:57:21
是的,我同意價值僅在某個時間點有效。但我想直到現在分析付款方式。我正在尋找最好的方法。如果添加一個新列不是最好的方法,請建議我怎麼做,否則不需要添加列。 – pdpandya 2014-11-04 18:00:04