带有两个表连接的SQL语句和另一个表

时间:2016-03-22 15:14:52

标签: sql-server join left-join

好的,这是您的指南后面的信息。这是表脚本+一些表。

USE [tempdb]
GO

CREATE TABLE [dbo].[table1](
    [cn] [nvarchar](1024) NULL,
    [member] [nvarchar](1024) NULL,
    [description] [nvarchar](1024) NULL,
    [Date] [datetime] NULL,
       [IsArchived] [bit] null
)

GO


CREATE TABLE [dbo].[table2](
    [cn] [nvarchar](1024) NULL,
    [member] [nvarchar](1024) NULL,
    [description] [nvarchar](1024) NULL,
    [Date] [datetime] NULL,
       [IsArchived] [bit] null
)

GO


CREATE TABLE [dbo].[table4](
    [cn] [nvarchar](1024) NULL,
    [member] [nvarchar](1024) NULL,
    [description] [nvarchar](1024) NULL,
    [Date] [datetime] NULL,
       [IsArchived] [bit] null
)

GO

CREATE TABLE [dbo].[table3](
    [ID] [int] not NULL,
    [ProductID] [nvarchar](1024) NULL,
    [ProductOmschrijving] [nvarchar](1024) NULL,
    [ProductPrijs] [money] NULL,
)

GO

INSERT INTO table1 VALUES ('grp-sec-spla-office','john; chris; jack; marc;','112-112','', '');
INSERT INTO table1 VALUES ('grp-sec-spla-office-prof','jack; marc;','114-114','', '');
INSERT INTO table2 VALUES ('grp-sec-spla-office','cees; klaas','112-112','', '');
INSERT INTO table2 VALUES ('grp-sec-spla-office-prof','jan; piet','114-114','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-office-prof','jack; marc;','114-114','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-office','piet; ellen','112-112','', '');
INSERT INTO table4 VALUES ('grp-sec-spla-visio','henk; alwin','112-116','', '');
INSERT INTO table3 VALUES (1,'112-112','grp-sec-spla-office-eng','10.12');
INSERT INTO table3 VALUES (2,'114-114','grp-sec-spla-office-prof-2016','5.45');
INSERT INTO table3 VALUES (3,'112-116','grp-sec-spla-visio-blabla','7.12');
INSERT INTO table3 VALUES (4,'112-118','grp-sec-ac-office-sta-eng','2.45');
INSERT INTO table3 VALUES (5,'112-120','grp-sec-ac-office-pro-eng','2,50');
GO

我的查询结果是:

+-------------+-------------------------------+--------------+-------+
| description |      ProductOmschrijving      | ProductPrijs | Total |
+-------------+-------------------------------+--------------+-------+
| 112-112     | grp-sec-spla-office-eng       | 10.12        |     9 |
| 114-114     | grp-sec-spla-office-prof-2016 | 5.45         |     8 |
+-------------+-------------------------------+--------------+-------+

但我需要得到:

+-------------+-------------------------------+--------------+-------+
| description |      ProductOmschrijving      | ProductPrijs | Total |
+-------------+-------------------------------+--------------+-------+
| 112-112     | grp-sec-spla-office-eng       | 10.12        |     9 |
| 114-114     | grp-sec-spla-office-prof-2016 | 5.45         |     8 |
| 112-116     | grp-sec-spla-visio-blabla     | 7.12         |     2 |
| 112-118     | grp-sec-ac-office-sta-eng     | 2.45         |     0 |
| 112-120     | grp-sec-ac-office-pro-eng     | 250.00       |     0 |
+-------------+-------------------------------+--------------+-------+

这是我现在使用的查询:

 SELECT
    table1.description,
    table3.ProductOmschrijving,
    table3.ProductPrijs,
    sum((isnull(LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')),-1) + 1) + 
       isnull(LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')),-1) + 1 + 
       isnull(LEN(table4.member) - LEN(REPLACE(table4.member, ';', '')),-1) + 1) AS Total
FROM table1
inner join table3 ON table1.description = table3.ProductID
left outer join table2 ON table1.description = table2.description
AND table2.IsArchived = 0
left outer join table4 on table1.description = table4.description
and table4.IsArchived = 0
where table1.IsArchived = 0
GROUP BY table1.description
    , ProductOmschrijving
    , ProductPrijs

table1,table2,table4可以更改(尤其是成员字段)。当我稍后(将来)向查询添加新表5时,我需要结果相同但只需要将成员添加到结果中。即使新表5中没有成员。

这更清楚吗?谢谢你的时间和帮助。

啊,当我发表整个帖子时,我意识到table3(产品表)当然必须是领先的表格。所以我编辑了查询来从table3而不是table1。不,它的工作完美!!!

 SELECT
    table3.ProductID,
    table3.ProductOmschrijving,
    table3.ProductPrijs,
    sum((isnull(LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')),-1) + 1) + 
       isnull(LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')),-1) + 1 + 
       isnull(LEN(table4.member) - LEN(REPLACE(table4.member, ';', '')),-1) + 1) 
       AS Total
FROM table3
left join table1 ON table3.productid = table1.description
and table1.IsArchived = 0
left join table2 ON table3.productid = table2.description
AND table2.IsArchived = 0
left join table4 on table3.productid = table4.description
and table4.IsArchived = 0
GROUP BY table3.productid
    , ProductOmschrijving
    , ProductPrijs

结果:

+-----------+-------------------------------+--------------+-------+
| ProductID |      ProductOmschrijving      | ProductPrijs | Total |
+-----------+-------------------------------+--------------+-------+
| 112-112   | grp-sec-spla-office-eng       | 10.12        |     9 |
| 112-116   | grp-sec-spla-visio-blabla     | 7.12         |     2 |
| 112-118   | grp-sec-ac-office-sta-eng     | 2.45         |     0 |
| 112-120   | grp-sec-ac-office-pro-eng     | 250.00       |     0 |
| 114-114   | grp-sec-spla-office-prof-2016 | 5.45         |     8 |
+-----------+-------------------------------+--------------+-------+

1 个答案:

答案 0 :(得分:0)

要将这些内容添加到现有查询中,您需要将它们包含在列列表和分组依据中。

SELECT SUM(table1_total + table2_total) AS Total,
       description
       , ProductOmschrijving
       , ProductPrijs
FROM (
    SELECT
        table1.description,
        table3.ProductOmschrijving,
        table3.ProductPrijs,
        table1.IsArchived,
        LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')) + 1 AS table1_total,
        LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')) + 1 AS table2_total
    FROM table1
    INNER JOIN table3 ON table1.description = table3.ProductID
    LEFT OUTER JOIN table2 ON table1.description = table2.description
) a
GROUP BY description
    , ProductOmschrijving
    , ProductPrijs

或者你可以像这样大大简化这个查询。

SELECT
    table1.description,
    table3.ProductOmschrijving,
    table3.ProductPrijs,
    --table1.IsArchived, --don't think this is required???
    sum(LEN(table1.member) - LEN(REPLACE(table1.member, ';', '')) + 1 + LEN(table2.member) - LEN(REPLACE(table2.member, ';', '')) + 1) AS Total
FROM table1
INNER JOIN table3 ON table1.description = table3.ProductID
LEFT OUTER JOIN table2 ON table1.description = table2.description
GROUP BY description
    , ProductOmschrijving
    , ProductPrijs