使用生成的值和子查询进行选择

时间:2019-03-08 11:23:50

标签: tsql sql-server-2012

我有一个包含以下表格的产品数据库:

  • 产品*
  • 证书*
  • 报告*
  • 说明*
  • 产品证书*
  • Certificates_Reports
  • Certificates_Descriptions

产品和证书,证书和报告以及证书和描述之间存在多对多关系。

每个标有*的表都有一个带有时间戳的列(可以为null)。

所需的输出

产品表的所有列+带有新列的

  • 0,如果该行的时间戳为空
  • 1,如果该行的时间戳不为null,但任何相关实体(或其相关实体)的时间戳为null
  • 2,如果该行的时间戳记不为空,并且所有相关实体(或其相关实体)的时间戳记都不为空

我什至不知道如何解决这个问题。


这是显示所有表如何连接在一起的查询:

   SELECT Products.*
     FROM Products
LEFT JOIN Products_Certificates
       ON Products_Certificates.ProductNumber = Products.ProductNumber
LEFT JOIN Certificates
       ON Certificates.ID = Products_Certificates.CertificateID
LEFT JOIN Certificates_Reports
       ON Certificates_Reports.CertificateID = Certificates.ID
LEFT JOIN Reports
       ON Reports.ID = Certificates_Reports.ReportID
LEFT JOIN Certificates_Descriptions
       ON Certificates_Descriptions.CertificateID = Certificates.ID
LEFT JOIN Descriptions
       ON Descriptions.ID = Certificates_Descriptions.DescriptionID

1 个答案:

答案 0 :(得分:0)

使用模拟数据并用SQL Server 2014编写,这是一些入门指南-

 declare @Products table
(
  ProductNumber int not null,
  Created date
);

insert into @Products values
(1, '1/7/2019'),
(2, null),
(3, '3/4/2019'),
(4, '3/7/2019');

declare @Products_Certificates table
(
  CertificateID int not null,
  ProductNumber int not null,
  Created date
);

insert into @Products_Certificates values
(50, 1, null),
(51, 1, '2/2/2019'),
(52, 3, '2/2/2019'),
(53, 4, null),
(54, 4, null);

declare @Certificates table
(
  ID int not null,
  Created date
);

insert into @Certificates values
(50, null),
(51, '3/1/2019'),
(52, null);

with
detail as
(
  SELECT
  p.ProductNumber,
  p.Created,
  pc.CertificateID,
  c.ID,
  Products_Certs_Date_Flag = iif(pc.Created is null, 0, 1),
  Certificates_Date_Flag = iif(c.Created is null, 0, 1)
  FROM @Products p
  LEFT OUTER JOIN @Products_Certificates pc     ON pc.ProductNumber = p.ProductNumber
  LEFT OUTER JOIN @Certificates c               ON c.ID = pc.CertificateID
  /*
  LEFT OUTER JOIN @Certificates_Reports cr      ON cr.CertificateID = c.ID --no timestamp in this table
  LEFT OUTER JOIN @Reports r                    ON r.ID = cr.ReportID
  LEFT OUTER JOIN @Certificates_Descriptions cd ON cd.CertificateID = c.ID --no timestamp in this table
  LEFT OUTER JOIN @Descriptions d               ON d.ID = cd.DescriptionID;
  */
),
summary as
(
  select
  ProductNumber,
  Created,
  DateStatus = iif(Products_Certs_Date_Flag + Certificates_Date_Flag > 0, 'date exists', 'no date')
  from detail
),
pivoted as
(
  select
  ProductNumber,
  Created,
  [date exists],
  [no date]
  from summary
  pivot (count(DateStatus) for DateStatus in ([date exists], [no date])) p
)
select
ProductNumber,
Created,
Outcome =
case
  when Created is null then 0
  when [no date] > 0 then 1
  else 2
end
from pivoted
order by
ProductNumber;

以下是CTE中的中间结果集的屏幕截图:
详情 enter image description here


摘要
enter image description here


透视
enter image description here


这是最终结果:
enter image description here

要使其他4个表在detail CTE中处于活动状态,还需要做更多的工作。上面的代码为您提供了如何解决原始帖子中概述的问题的想法。另外,请仔细测试case语句-如果未正确分配1和2值,则对实时数据运行此语句后,可能有必要添加其他条件。