从表A中选择所有行,并在同一组结果中选择表B中的多个列

时间:2018-04-21 05:43:33

标签: sql sql-server pivot

我在SQL Server 2012中有两个表,我需要使用其中一个来获取多个列,但我可以得到我想要的结果= S:

这是我的两张桌子:

表A

id        name
--------------    
1         AAA
2         BBB
3         CCC
4         DDD
5         EEE
6         FFF
7         GGG

表B

idB    id(A)     price
----------------------
0      1          50
0      2          60
0      3          70
0      4          80
0      5          90
1      1         110
1      2         120
1      6         160  
3      2         220
3      3         230
3      4         240
3      5         250
3      6         260

我需要一个查询来显示此表中的结果:

A.id      A.name    B.Price(idB=0)   B.Price(idB=1)  B.Price(idB=3)
-------------------------------------------------------------------    
1         AAA       50               110             NULL
2         BBB       60               120             220
3         CCC       70               NULL            230
4         DDD       80               NULL            240
5         EEE       90               NULL            250
6         FFF       NULL             160             260

知道我怎么能得到这个结果吗?

我感谢任何帮助

Saludos

3 个答案:

答案 0 :(得分:1)

执行inner join并使用条件加重

select a.id, a.name,
       max(case when b.idB = 0 then b.price end) as [B.Price(idB=0)],
       max(case when b.idB = 1 then b.price end) as [B.Price(idB=1)],
       max(case when b.idB = 3 then b.price end) as [B.Price(idB=3)]
from tableA a
inner join tableB b on b.[id(A)] = a.id
group by a.id, a.name;

答案 1 :(得分:0)

我希望这对你有用

DECLARE @a TABLE(A1 INT, A2 NVARCHAR(10))
INSERT @a VALUES
 (1         ,'AAA')
,(2         ,'BBB')
,(3         ,'CCC')
,(4         ,'DDD')
,(5         ,'EEE')
,(6         ,'FFF')
,(7         ,'GGG')


DECLARE @b TABLE (B1 INT, B2 INT, B3 INT)
INSERT @b VALUES
 (0,      1,         50 )
,(0,      2,         60 )
,(0,      3,         70 )
,(0,      4,         80 )
,(0,      5,         90 )
,(1,      1,         110)
,(1,      2,         120)
,(1,      6,         160)  
,(3,      2,         220)
,(3,      3,         230)
,(3,      4,         240)
,(3,      5,         250)
,(3,      6,         260)


SELECT a.A1 [A.id], a.A2 [A.name], b.B3 [B.Price(idB=0)], b1.B3 [B.Price(idB=1)], b2.B3[B.Price(idB=3)] FROM @a a
LEFT JOIN @b b ON a.A1 = b.B2 AND b.B1 = 0
LEFT JOIN @b b1 ON a.A1 = b1.B2 AND b1.B1 = 1
LEFT JOIN @b b2 ON a.A1 =b2. B2 AND b2.B1 = 3

-------------------------------------------------------------
--Result
-------------------------------------------------------------

    A.id    A.name  B.Price(idB=0)  B.Price(idB=1)  B.Price(idB=3)
    1       AAA     50              110             NULL
    2       BBB     60              120             220
    3       CCC     70              NULL            230
    4       DDD     80              NULL            240
    5       EEE     90              NULL            250
    6       FFF     NULL            160             260
    7       GGG     NULL            NULL            NULL

答案 2 :(得分:0)

请像这样使用 - (相应地更改列名)

SELECT a.*,b1.price,b2.price,b3.price FROM TableA a
LEFT JOIN TableB b1 ON a.id = b1.idA AND b1.idB = 0
LEFT JOIN TableB b2 ON a.id = b2.idA AND b2.idB = 1
LEFT JOIN TableB b3 ON a.id = b3.idA AND b3.idB = 3
WHERE b1.price IS NOT NULL or b2.price IS NOT NULL or b3.price IS NOT NULL

输出

id          name       price       price       price
----------- ---------- ----------- ----------- -----------
1           AAA        50          110         NULL
2           BBB        60          120         220
3           CCC        70          NULL        230
4           DDD        80          NULL        240
5           EEE        90          NULL        250
6           FFF        NULL        160         260

(6 rows affected)