【发布时间】:2016-02-10 14:24:59
【问题描述】:
客户表:
customerId customerName startTime projectDateOfCompletion
1 ANZ 2015-12-05 NULL
2 Barklays 2015-11-25 2016-02-10
3 Grindlays 2016-02-06 NULL
报告表:
customerId repId fromDateTime toDateTim
1 1 2015-12-05 2016-02-05
1 2 2016-02-06 NULL
2 3 2015-11-25 2016-02-10
3 1 2016-02-08 NULL
客户支持表:
repId customerSupportRep
1 Jim Daniel
2 Mark Chad
3 Juan Maximo
不确定如何以表格形式格式化表格。希望看起来像桌子。我正在寻求有关以下查询的帮助
SELECT
t.customerName AS 'Customer Name',
u.customerSupportRep AS 'Customer Support Representative',
t.startTime AS 'Start Date/Time'
FROM Customer t
JOIN CustomerSupport u
ON u.repId = ( SELECT repId
FROM Reports
WHERE
customerId = t.customerId AND
(CASE
WHEN (@archiveStartTime IS NOT NULL )
then @archiveStartTime BETWEEN fromDateTime AND toDateTime
ELSE toDateTime IS NULL
END)
)
WHERE
t.projectDateOfCompletion IS NULL OR
t.projectDateOfCompletion = '';
如果@archiveStartTime 为NULL,我想要projectDateOfCompletion 为NULL 的记录
如果@archiveStartTime 不为NULL,那么我想列出@archiveStartTime 位于fromDateTime 和toDateTime 之间且projectDateOfCompletion 为NULL 的所有记录。
任何帮助将不胜感激。
【问题讨论】:
标签: sql if-statement join case