我对SQL Server有疑问,请告诉我如何解决
表:Patient
(MM-dd-yy) (MM-dd-yy) (MM-dd-yy)
pn | rvs |prcode |date |amount |datefdate| datetdate| entry
61 | NULL |MS001 |NULL |NULL |07-23-14| 07-23-14| b
61 | NULL |MS001 |NULL |NULL |08-04-14| 08-04-14| b
61 | NULL |MS001 |NULL |NULL |08-11-14| 08-11-14| b
61 | NULL |MS001 |NULL |NULL |08-20-14| 08-20-14| b
61 | NULL |MS001 |NULL |NULL |08-26-14| 08-26-14| b
61 | NULL |MS001 |NULL |NULL |09-03-14| 09-03-14| b
61 | |00000 | | |01-01-80| 10-06-14| b
61 | |00000 | | |01-01-80| 10-06-14| b
61 | |MS001 | | |01-06-15| 01-06-15| b
61 | 97124 |MS001 |01-06-15|120.00|NULL | NULL | c
61 | 97124 |MS001 |07-23-14|120.00|NULL | NULL | c
61 | 97124 |MS001 |08-04-14|120 | | | c
61 | 97124 |MS001 |08-11-14|120 | | | c
61 | 97124 |MS001 |08-20-14|120 | | | c
61 | 97124 |MS001 |08-26-14|120 | | |c
61 | 97124 |MS001 |09-03-14|120 | | | c
61 | 97124 |MS001 |09-15-14|75 | | | c
61 | 97124 |MS001 |09-15-14|0 | | |c
61 | 60MIN |MS001 |10-27-14|75 | | | c
61 | 60MIN |MS001 |11-04-14|75 | | | c
61 | 60MIN |MS001 |11-10-14|75 | | | c
61 | 60MIN |MS001 |11-25-14|75 | | | c
61 | 60MIN |MS001 |12-02-14|75 | | | c
61 | 60MIN |MS001 |12-09-14|75 | | | c
61 | 60MIN |MS001 |12-15-14|75 | | | c
61 | 60MIN |MS001 |12-22-14|75 | | | c
61 | 60MIN |MS001 |12-30-14|75 | | | c
这里我想比较datefdate和datetdate之间的条目类型= c记录日期数据和条目类型b记录
如果满足此条件,则输出我们检索条目类型= c记录相关数据,并添加一个新列以标识记录
如果条件不满足则在新列中显示状态记录然后我们检索条目类型= c记录相关数据和新列状态是“未开帐单”
最后,每次我们需要比较条目类型= c记录日期是在datefdate和datetdate之间,条目类型为B记录。
如果符合条件,则该记录已开票,否则为未开票。
我尝试过以下查询
SELECT DISTINCT [a].[pn],
a.rvs,
a.date,
a.prcode,
a.amount,
b.datefdate,
b.datetdate,
CASE
WHEN ( CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) BETWEEN CONVERT(VARCHAR(10), CAST(LTRIM(RTRIM(b.datefdate)) AS DATE), 120) AND CONVERT(VARCHAR(10), CAST(LTRIM(RTRIM(b.datetdate)) AS DATE), 120) ) THEN 'billed'
WHEN ( CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) NOT BETWEEN CONVERT(VARCHAR(10), CAST(LTRIM(RTRIM(b.datefdate)) AS DATE), 120) AND CONVERT(VARCHAR(10), CAST(LTRIM(RTRIM(b.datetdate)) AS DATE), 120) ) THEN 'unbilled'
END AS [Filter]
FROM testbilled a
JOIN testbilled b
ON a.pn = '61'
WHERE a.entry = 'c'
AND b.entry = 'b'
它没有给出正确的结果。它是多重播放9 * 18 = 144记录。
我想要输出如下:
pn rvs date bcharge entry prcode filter
61 60MIN 09-15-14 75.00 C MS001 Billed
61 60MIN 10-27-14 75.00 C MS001 UnBilled
61 60MIN 11-04-14 75.00 C MS001 UnBilled
61 60MIN 11-10-14 75.00 C MS001 UnBilled
61 60MIN 11-25-14 75.00 C MS001 UnBilled
61 60MIN 12-02-14 75.00 C MS001 UnBilled
61 60MIN 12-09-14 75.00 C MS001 UnBilled
61 60MIN 12-15-14 75.00 C MS001 UnBilled
61 60MIN 12-22-14 75.00 C MS001 UnBilled
61 60MIN 12-30-14 75.00 C MS001 UnBilled
61 97124 01-06-15 120.00 C MS001 Billed
61 97124 07-23-14 120.00 C MS001 Billed
61 97124 08-04-14 120.00 C MS001 Billed
61 97124 08-11-14 120.00 C MS001 Billed
61 97124 08-20-14 120.00 C MS001 Billed
61 97124 08-26-14 120.00 C MS001 Billed
61 97124 09-03-14 120.00 C MS001 Billed
61 97124 09-15-14 0.00 C MS001 Billed
请告诉我如何在SQL Server中实现此问题。
答案 0 :(得分:1)
你试过Subquery吗?如果没有,那么检查一下:
SELECT pn,
rvs,
date,
amount,
entry,
prcode,
CASE
WHEN EXISTS(SELECT 1
FROM @YourTable b
WHERE a.date BETWEEN b.datefdate AND b.datetdate
AND b.entry = 'b'
AND a.pn = b.pn) THEN 'BILLED' ELSE 'UNBILLED'
END [Filter]
FROM @YourTable a
WHERE a.entry = 'c'
ORDER BY amount