带有字段值的SQL查询,用于列名中的左连接

时间:2018-03-25 18:40:05

标签: sql sql-server sql-server-2008 tsql

是否可以通过这些表中的一个查询获得我想要的结果? 问题是我怀疑由于代码的名称只是列名,所以不可能。

+-----------+----+-------------+-----------+----------+
| unioncode | qt | productCode | brandCode | shopCode |
+-----------+----+-------------+-----------+----------+
| 00212AA   | 10 |           3 |         2 |        1 |
| 00212AA   | -4 |           3 |         2 |        1 |
| 00212AA   |  3 |           3 |         2 |        1 |
| 00372BC   |  7 |           6 |         4 |        1 |
+-----------+----+-------------+-----------+----------+

+------+-------------+---------+
| code | description |  type   |
+------+-------------+---------+
|    1 | BuyTech     | shop    |
|    2 | Apple       | brand   |
|    3 | iPhone      | product |
|    5 | Samsung     | product |
|    6 | Smartphone  | product |
+------+-------------+---------+

我正在努力创建一个查询来获得我想要的结果:

+-----------+-------+-------------+-----------+----------+
| unioncode | totQt | productName | brandName | shopName |
+-----------+-------+-------------+-----------+----------+
| 00212AA   |     9 | iPhone      | Apple     | BuyTech  |
+-----------+-------+-------------+-----------+----------+
| 00372BC   |     7 | Smartphone  | Null      | BuyTech  |
+-----------+-------+-------------+-----------+----------+

2 个答案:

答案 0 :(得分:3)

You should join three times the second table.

And I'm sorry but there isn't brandCode = 4.

create table t1 (unioncode varchar(10), qt int, productCode int, brandCode int, shopCode int);
insert into t1 values
('00212AA', 10, 3, 2, 1),
('00212AA', -4, 3, 2, 1),
('00212AA',  3, 3, 2, 1),
('00372BC',  7, 6, 4, 1);
GO
create table t2 (code int, description varchar(20), type varchar(10));
insert into t2 values
(1, 'BuyTech', 'shop'),
(2, 'Apple', 'brand'),
(3, 'iPhone', 'product'),
(5, 'Samsung', 'product'),
(6, 'Smartphone', 'product');
GO
select unioncode, 
       tot_qt, 
       tt2.description as productName, 
       tt3.description as shopName, 
       tt4.description as brandName
from
     (select unioncode, sum(qt) tot_qt, productCode, shopCode, brandCode
      from   t1
      group by unioncode, productCode, shopCode, brandCode
     ) tt1
left join  t2 tt2
on    tt2.code = tt1.productCode
and   tt2.type = 'product'
left join  t2 tt3
on    tt3.code = tt1.shopCode
and   tt3.type = 'shop'
left join  t2 tt4
on    tt4.code = tt1.brandCode
and   tt4.type = 'brand'
GO
unioncode | tot_qt | productName | shopName | brandName
:-------- | -----: | :---------- | :------- | :--------
00212AA   |      9 | iPhone      | BuyTech  | Apple    
00372BC   |      7 | Smartphone  | BuyTech  | null     

dbfiddle here

答案 1 :(得分:1)

There isn't brandCode = 4.So the Output is NULL

WITH Cte AS
(
     SELECT unioncode,
            qt,
            description,
            (SELECT TOP 1 description FROM Table2 WHERE code=T1.shopCode) AS shopName,
            (SELECT TOP 1 description FROM Table2 WHERE code=T1.brandCode) AS BrandName
     FROM Table1 t1
     INNER JOIN Table2 t2
     ON t1.productCode=t2.code
 )
SELECT unioncode,
       SUM(qt) AS totqy,
       description,
       BrandName,
       shopName
FROM Cte
GROUP BY unioncode, description, shopName, BrandName

Output

unioncode   totqy   description BrandName   shopName
00212AA     9       iPhone      Apple       BuyTech
00372BC     7       Smartphone  (null)      BuyTech

Demo Link

http://sqlfiddle.com/#!18/782c5d/29