mercredi 22 avril 2015

date between fdate and tdate based on condition in sql server

Hi Friends I have small doubt in sql server please tell me how to sovle

Table: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

Here I want comapare entry type= c records date information between datefdate and datetdate when entry type=B if this condition satisfy then output we retrive entry type =c records related data and one columns is added to identify records t we pass "valid" records in new column if condition not satisfy then we retrive entry type=c records related data and new columns status is "invalid" I tried like below query SELECT [a].[pn] ,a.amount,a.date, cASE WHEN CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) <= CONVERT(VARCHAR(10), CAST(ltrim(rtrim(b.datefdate)) AS DATE), 120) AND CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) >= CONVERT(VARCHAR(10), CAST(ltrim(rtrim(b.datetdate)) AS DATE), 120) THEN 'billed' WHEN CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) <> CONVERT(VARCHAR(10), CAST(ltrim(rtrim(b.datefdate)) AS DATE), 120) AND CONVERT(VARCHAR(10), CAST(a.date AS DATE), 120) <> CONVERT(VARCHAR(10), CAST(ltrim(rtrim(b.datetdate)) AS DATE), 120) THEN 'unbilled' END AS [Filter] FROM [dbo].testbilled [a] JOIN [dbo].testbilled [b] ON [a].[pn] = [b].[Pn] where a.entry='b' or b.entry='c'

its not give correct result.

i want output like below: 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

please tell me how to achivet this issue in sql server.

Aucun commentaire:

Enregistrer un commentaire