用于选择行的Where语句层次结构

时间:2019-03-08 19:13:40

标签: sql select exists case-when

我有一个表,该表的列名为ReportTypeId。我想选择ReportTypeId = 1的行,但是如果对此不存在任何行,那么我希望ReportTypeId =2。我正在尝试使用WHEN EXISTS,但是我不知道如何选择多于一列的内容。我想写一个看起来像这样的查询:

SELECT CASE 
    WHEN EXISTS (SELECT PerformanceReport FROM ReportData
                    WHERE (ReportId = 79 and ReportTypeId = 1))

        THEN (select * from ReferenceData               
                where ReportTypeId = 1)

        ELSE (select * from ReferenceData
                where ReportTypeId = 2)
    END

但是因为我试图返回多个列,所以它不起作用。有没有一种方法可以基于数据是否存在创建基于WHERE语句的查询?

3 个答案:

答案 0 :(得分:1)

您可以使用以下解决方案:

SELECT * 
FROM ReferenceData
WHERE (
    ReportTypeId = 1 
    AND EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 1 AND ReportId = 79)
) OR (
    ReportTypeId = 2
    AND NOT EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 1 AND ReportId = 79)
)

您可以使用JOIN对此进行优化:

SELECT ReferenceData.* 
FROM ReferenceData JOIN (
    SELECT EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 1 AND ReportId = 79) AS state
) isAvailable
WHERE (
    ReportTypeId = 1 AND isAvailable.state = 1
) OR (
    ReportTypeId = 2 AND isAvailable.state = 0
)

您可以使用JOIN添加多个支票:

SELECT ReferenceData.* 
FROM ReferenceData JOIN (
    SELECT EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 1 AND ReportId = 79) AS state
) avail1 JOIN (
    SELECT EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 2 AND ReportId = 79) AS state
) avail2 JOIN (
    SELECT EXISTS (SELECT * FROM ReportData WHERE ReportTypeId = 3 AND ReportId = 79) AS state
) avail3
WHERE (
    ReportTypeId = 1 AND avail1.state = 1
) OR (
    ReportTypeId = 2 AND avail1.state = 0 AND avail2.state = 1
) OR (
    ReportTypeId = 3 AND avail1.state = 0 AND avail2.state = 0 AND avail3.state = 1
) OR (
    ReportTypeId = 4 AND avail1.state = 0 AND avail2.state = 0 AND avail3.state = 0
)
  

demo on dbfiddle.uk

答案 1 :(得分:1)

如果可以订购RecordTypeId **,则可以使用以下

SELECT PerformanceReport 
FROM ReferenceData
WHERE ReportTypeId = (
      SELECT MIN(ReportTypeId) 
      FROM ReportData 
      WHERE ReportTypeId IN (1, 2)
        AND ReportId = 79
)

**按顺序排列,我的意思是MIN聚合函数将返回预期的结果。对于整数,这是有道理的,但是,如果您的报表类型ID是文本uuid,则MIN仍然可以工作,但不会给您预期的结果,因为它将以词法顺序返回最小ID。

答案 2 :(得分:1)

您似乎想要:

SELECT refd.* 
FROM ReferenceData refd
WHERE (refd.ReportType = 1 AND 
       EXISTS (SELECT 1
               FROM ReportData repd
               WHERE repd.ReportId IN (1, 79)
              )
      ) OR
      (refd.ReportType = 2 AND 
       EXISTS (SELECT 1
               FROM ReportData repd
               WHERE repd.ReportId NOT IN (1, 79)
              )
      )