oracle查找包含某值的所有表 sql兩張表,如何將符合條件的值相加?
sql兩張表,如何將符合條件的值相加?create table T1( id int, 時(shí)間1 datetime, 值1 int, 時(shí)間2 datetime, 值2 in
sql兩張表,如何將符合條件的值相加?
create table T1( id int, 時(shí)間1 datetime, 值1 int, 時(shí)間2 datetime, 值2 int, 時(shí)間3 datetime, 值3 int)create table T2( id int, [2015-1-1] int, [2015-1-2] int, [2015-1-3] int )insert into T1 values(1,"2015-1-1",23,"2015-2-3",21,"2015-3-9",21)insert into T1 values(2,"2015-1-1",23,"2015-2-3",21,"2015-3-9",21)insert into T2 values(1,45,56,13)insert into T2 values(2,12,17,54)--把T2列轉(zhuǎn)行,再和T1合計(jì)with TAs(select * from T2unpivot( val for dat in ([2015-1-1],[2015-1-2],[2015-1-3])) upvt)select id,時(shí)間1,值1 isnull((select val from t where id=t1.id and dat=時(shí)間1),0) As 值1,時(shí)間2,值2 isnull((select val from t where id=t1.id and dat=時(shí)間2),0) As 值2,時(shí)間3,值3 isnull((select val from t where id=t1.id and dat=時(shí)間3),0) As 值3from T1