【问题标题】:SQL statement with two tables join and another table带有两个表连接和另一个表的 SQL 语句
【发布时间】:2016-03-22 15:14:52
【问题描述】:

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

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可以改变(尤其是member字段)。当我稍后(将来)向查询中添加一个新的 table5 时,我需要结果相同,但只需要将成员添加到结果中。即使新表中没有成员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 |
+-----------+-------------------------------+--------------+-------+

【问题讨论】:

  • 缺少的列不在您的查询中。它们在子查询中,但在主查询中缺失。
  • 嗨,肖恩,我知道。但是当我添加它们时,我得到一个错误。消息 4104,级别 16,状态 1,第 12 行无法绑定多部分标识符“Product.ProductOmschrijving”。
  • 只是说你得到一个错误不是很有帮助。说明错误是什么会有所帮助。在这种情况下,我怀疑这是因为您没有将它们也添加到 group by。
  • 谢谢!!!将它添加到 group by 是解决方案:-) NICE!!!
  • 您的最后两个问题非常含糊。你能尝试更详细地解释它们吗?

标签: sql-server join left-join


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    • 2016-03-31
    相关资源
    最近更新 更多