have small doubt in sql server please tell me how to solve
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 data between datefdate and datetdate with entry type b records if this condition satisfy then output we retrive entry type =c records related data and one newcolumns is added to identify records status records in new column if condition not satisfy then we retrive entry type=c records related data and new columns status is "unbilled" Finaly every time we need to compare entry type=c records date is between datefdate and datetdate with entry type B records. if condition satisfy then that records billed other wise unbilled. I tried like below query
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'
its not give correct result. it is multiplay9*18=144 records commening.
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 achieve this issue in sql server.
Aucun commentaire:
Enregistrer un commentaire