【问题标题】:T-SQL Advanced JoinT-SQL 高级连接
【发布时间】:2022-11-17 00:51:46
【问题描述】:

我正在努力实现以下结果;

Customer Purchase Total ($)
Customer A 1234.56
Customer B 5678.90
Customer C
Customer D
Customer E 91011.23

表结构如下;

Table Name Fields
Invoice InvoiceId, InvoiceType, CustomerId, DateIssued
Invoice Lines LineId, InvoiceId, ProductId, Date, Price, Quantity, LineTotal
Product ProductId, Code, name
Customer CustomerId, Status, Region, Code, Name

我必须使用以下过滤器得出结果;

Filter Value
Product.Code GTN
Invoice.InvoiceType All invoices (Returns and Sales) are stored in the same table hence, in order to obtain correct result, I need to subtract returns from sales) Sales Invoice Type is 8 and Returns Invoice Type is 3
Customer.Status 0
Customer.Region London
Customer.Code Starts with M
Invoice.Date Year: 2022 Month: 10

我尝试了什么: 我尝试了很多其他的东西,下面是我最新的代码,但我得到了错误的结果。

SELECT C.Name, 
    (SELECT SUM(IL.LineTotal) 
    FROM Invoice I 
    INNER JOIN InvoiceLine IL ON I.InvoiceId= IL.InvoiceId 
    INNER JOIN Product P ON IL.ProductId = P.ProductId
    WHERE IL.CustomerId = C.CustomerId AND P.CODE LIKE 'GTN.%' AND I.TRCODE = 8 AND YEAR(IL.Date) = 2022 AND MONTH(IL.Date) = 10 AND C.Code LIKE 'M.%' AND C.Region = 'London') -
    (SELECT SUM(LI.LineTotal) 
    FROM Invoice I 
    INNER JOIN InvoiceLine IL ON I.InvoiceId= IL.InvoiceId 
    INNER JOIN Product P ON IL.ProductId = P.ProductId
    WHERE IL.CustomerId = C.CustomerId AND P.CODE LIKE 'WLT.%' AND I.TRCODE = 3 AND YEAR(IL.Date) = 2022 AND MONTH(IL.Date) = 10 AND C.Code LIKE 'M.%' AND C.Region = 'London') AS TOTAL
FROM Invoice I
LEFT JOIN Customer C ON I.CustomerId = C.CustomerId
WHERE C.Code LIKE 'M.%'
GROUP BY C.CustomerId, C.Code, C.Name
ORDER BY C.Name;

因为要求只将代码以特定字母开头的产品的总计带到结果中,所以我不能在 Invoice 表上工作,而是在 InvoiceLines 表上工作。也因为还需要列出那些没有购买任何东西的客户,所以我想对 Customer 表使用 LEFT JOIN。

任何帮助,将不胜感激。

【问题讨论】:

  • 意味深长示例数据和预期结果(最好是 DDL 和 DML 语句)将真正帮助我们帮助您。您的查询看起来根本不正确;特别是当你有 3 个 Invoice 的实例时,它们都别名为 I,并且一些范围是共享的。你也有一个 GROUP BY 但实际上并没有聚合任何该范围内的列,那么为什么要有 GROUP BY 呢?
  • 另外,当您需要列 Code 时,为什么将 LEFT JOIN 更改为 Customer必须有一个非NULL 值?如果没有找到行,Code 的值不可能是非NULL
  • 我正在尝试检索数据以分析每个客户特定商品的月销售额。部分分析还需要列出该月未进行任何购买的客户,因此我认为我可以通过 LEFT JOIN 实现这一点。我对 SQL 的了解非常有限,所以如果它没有任何意义,我深表歉意。所有 GROUP BY 语句都在那里,因为 SSMS 抱怨没有它们。
  • SUM(LI.LineTotal) 甚至不起作用,您的查询中没有别名为 LI 的对象。
  • 抱歉,应该是 IL。我已经更正了

标签: sql sql-server tsql


【解决方案1】:

我为你做了腿部工作。这是 SQL 小提琴。您必须针对所有特定变量和过滤器对其进行调整。在需要的地方添加其他限制器应该很容易。

SQL Fiddle

MS SQL Server 2017 架构设置:

CREATE TABLE Invoice (
  InvoiceId int not null
  , InvoiceType nvarchar(50) null
  , CustomerId int not null
  , DateIssued datetime not null
);

CREATE TABLE InvoiceLine (
  LineId int IDENTITY(1,1) not null
  , InvoiceId int not null
  , ProductId int not null
  , [Date] datetime null
  , Quantity decimal(19,4) not null
  , LineTotal decimal(19,2) not null
);

CREATE TABLE Product (
  ProductId int IDENTITY(1,1) not null
  , [Code] nvarchar(256) not null
  , Name nvarchar(50) not null
);

CREATE TABLE Customer (
  CustomerId int IDENTITY(1,1) not null
  , [Status] nvarchar(50) null
  , Region nvarchar(50) null
  , [Code] nvarchar(50) null
  , Name nvarchar(50) not null
);

INSERT INTO Customer ([Status], Name, [Code], Region)
VALUES ('Active', 'ABC Co.', 'Mega', 'London')
  , ('Active', 'XYZ Co.', 'Mega', 'London')
  , ('Active', 'TTL Co.', 'Mega', 'London')
;

INSERT INTO Product ([Code], Name) 
VALUES ('A1B2C3','Widget A')
  , ('D4E5F6', 'Widget D')
  , ('G7H8I9', 'Widget G')
;

INSERT INTO Invoice (InvoiceId, InvoiceType, CustomerId, DateIssued)
VALUES (1234111, 'Invoice', 1, '2022-10-16 00:00:00')
  , (1234777, 'Invoice', 2, '2022-10-14 00:00:00')
  , (1234888, 'Return', 1, '2022-10-16 00:00:00')
;

INSERT INTO InvoiceLine (InvoiceId, ProductId, Quantity, LineTotal)
VALUES (1234111, 1, 1.0000, 56.98)
  , (1234111, 2, 2.0000, 48.28)
  , (1234777, 2, 5.0000, 120.07)
  , (1234777, 3, 2.5000, 99.84)
  , (1234888, 1, 1.0000, 56.98)
;
  

查询 1:

DECLARE @GTN nvarchar(50) = 'A1'
DECLARE @WLT nvarchar(50) = 'D4'

SELECT
  c.Name
  , ISNULL((
      SELECT SUM(il.LineTotal) as LineTotal
      FROM Invoice as i
        INNER JOIN InvoiceLine as il
          ON il.InvoiceId = i.InvoiceId
        INNER JOIN Product as p
          ON p.ProductId = il.ProductId
          AND p.[Code] like @GTN + '%'
      WHERE i.CustomerId = c.CustomerId
        AND i.DateIssued >= DateFromParts(2022, 10, 1)
        AND i.DateIssued < DateFromParts(2022, 11, 1)
    ),0) --WidgetATotalSales
  - ISNULL((
      SELECT SUM(il.LineTotal) as LineTotal
      FROM Invoice as i
        INNER JOIN InvoiceLine as il
          ON il.InvoiceId = i.InvoiceId
        INNER JOIN Product as p
          ON p.ProductId = il.ProductId
          AND p.[Code] like @WLT + '%'
      WHERE i.CustomerId = c.CustomerId
        AND i.DateIssued >= DateFromParts(2022, 10, 1)
        AND i.DateIssued < DateFromParts(2022, 11, 1)
    ),0) --WidgetDTotalSales
    as Total
FROM Customer as c
WHERE 1=1
  AND c.[Code] like 'M%'
  AND c.[Status] = 'Active'
  AND c.[Region] = 'London'
   

Results:

|    Name |   Total |
|---------|---------|
| ABC Co. |   65.68 |
| XYZ Co. | -120.07 |
| TTL Co. |       0 |

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多