Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Need help with the TSQL.

$
0
0
I have 2 tables and need to check if the record exist in the #diagnose table and meet the rules below than return it. 
I have the query below but need your help to get the desired output below.  SQL 2008

Thanks in advance.


IF OBJECT_ID('Tempdb.dbo.#Temp', 'u') IS NOT NULL
  DROP TABLE #Temp
GO
CREATE TABLE dbo.#Temp
(
   patient_id    INT            NULL,
   creator       VARCHAR(15)    NULL,
   creation_dt   DATETIME       NULL,
   DoB           DATETIME       NULL,
   DoA           DATETIME       NULL,
   Location_Id   INT            NULL,
   DocId         INT            NULL,
   Spect_only    BIT            NULL
)
GO

IF OBJECT_ID('Tempdb.dbo.#Diagnose', 'u') IS NOT NULL
  DROP TABLE #Diagnose
GO
CREATE TABLE dbo.#Diagnose
(
   patient_id    INT            NULL
)
GO

INSERT INTO dbo.#Diagnose( [Patient_ID])
  VALUES (21468044);
INSERT INTO dbo.#Diagnose( [Patient_ID])
  VALUES (21478748);
GO


 -- DELETE #Temp

INSERT INTO dbo.#Temp( [Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES (12999087, 'ABCD\dtaylor', '04/03/2013', '05/10/1967', '04/03/2013', 2, 8, 1);


INSERT INTO dbo.#Temp( [Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only)
  VALUES (12999087, 'ABCD\KLe', '04/03/2013', '05/10/1967', '04/03/2013', 2, 8, 1 );


INSERT INTO dbo.#Temp([Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES (12999088, 'ABCD\LSmith', '04/11/2013', '03/05/1969', '04/06/2013', 7, 4, 0 );

INSERT INTO dbo.#Temp( [Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES              (21468044, 'ABCD\kmjala', '03/06/2013', '03/05/1969', '04/06/2013', 9, 2, 0);

INSERT INTO dbo.#Temp([Patient_ID],[Creator],  [creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES             (21478044, 'ABCD\John', '03/15/2013', '10/07/1971', '04/26/2013', 5, 8, 0 );

INSERT INTO dbo.#Temp([Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES ( 21468097, 'ABCD\David', '02/21/2013', '06/13/1969', '02/18/2013', 3, 6, 0 );

INSERT INTO dbo.#Temp([Patient_ID],[Creator],[creation_dt], DoB, DoA, Location_Id , DocId, Spect_only )
  VALUES ( 21468097, 'ABCD\David', '04/15/2013', '06/13/1969', '02/18/2013', 8, 4, 0 );

INSERT INTO dbo.#Temp([Patient_ID],[Creator],  [creation_dt], DoA, Location_Id , DocId, Spect_only)
  VALUES             (21478736, 'ABCD\NLisa', '04/29/2013', '04/26/2013', 5, 7, 0 );


INSERT INTO dbo.#Temp([Patient_ID],[Creator],  [creation_dt], DoB, DoA, Location_Id , DocId, Spect_only)
  VALUES             (21478748, 'ABCD\NDavid', '01/25/2013', '04/14/1973', '03/24/2013', 5, 7, 1);
GO


/*

--patient
patient_id  creator         creation_dt             DoB                     DoA                     Location_Id DocId       Spect_only
----------- --------------- ----------------------- ----------------------- ----------------------- ----------- ----------- ----------
12999087    ABCD\dtaylor    2013-04-03 00:00:00.000 1967-05-10 00:00:00.000 2013-04-03 00:00:00.000 2           8           1
12999087    ABCD\KLe        2013-04-03 00:00:00.000 1967-05-10 00:00:00.000 2013-04-03 00:00:00.000 2           8           1

12999088    ABCD\LSmith     2013-04-11 00:00:00.000 1969-03-05 00:00:00.000 2013-04-06 00:00:00.000 7           4           0

21468044    ABCD\kmjala     2013-03-06 00:00:00.000 1969-03-05 00:00:00.000 2013-04-06 00:00:00.000 9           2           0
21478044    ABCD\John       2013-03-15 00:00:00.000 1971-10-07 00:00:00.000 2013-04-26 00:00:00.000 5           8           0

21468097    ABCD\David      2013-02-21 00:00:00.000 1969-06-13 00:00:00.000 2013-02-18 00:00:00.000 3           6           0
21468097    ABCD\David      2013-04-15 00:00:00.000 1969-06-13 00:00:00.000 2013-02-18 00:00:00.000 8           4           0

21478736    ABCD\NLisa      2013-04-29 00:00:00.000 NULL                    2013-04-26 00:00:00.000 5           7           0

21478748    ABCD\NDavid     2013-01-25 00:00:00.000 1973-04-14 00:00:00.000 2013-03-24 00:00:00.000 5           7           1

  SELECT *
    FROM #Temp
  go

   SELECT * FROM #Diagnose


#Diagnose
patient_id
-----------
21468044
21478748

*/

/*
-- Rules:
if spect_only = 0 and patient_id exist in #Diagnose table and return it.
if spect_only = 1 return it, regardless exist in #Diagnose table or not.
if DoB, DoA and Location_id <> -1, and DocId <> 64 return it.


-- Desired output:

patient_id  creator         creation_dt             DoB                     DoA                     Location_Id DocId       Spect_only
----------- --------------- ----------------------- ----------------------- ----------------------- ----------- ----------- ----------
12999087    ABCD\dtaylor    2013-04-03 00:00:00.000 1967-05-10 00:00:00.000 2013-04-03 00:00:00.000 2           8           1
12999087    ABCD\KLe        2013-04-03 00:00:00.000 1967-05-10 00:00:00.000 2013-04-03 00:00:00.000 2           8           1

21468044    ABCD\kmjala     2013-03-06 00:00:00.000 1969-03-05 00:00:00.000 2013-04-06 00:00:00.000 9           2           0
21478044    ABCD\John       2013-03-15 00:00:00.000 1971-10-07 00:00:00.000 2013-04-26 00:00:00.000 5           8           0

21478748    ABCD\NDavid     2013-01-25 00:00:00.000 1973-04-14 00:00:00.000 2013-03-24 00:00:00.000 5           7           1


*/



-- Testing...


    SELECT CASE
              WHEN ( b.DoB > '' AND b.DoA > '' AND b.Location_Id <> - 1
              AND b.Doctor_id <> 64 ) THEN 'Y'
              ELSE 'N'
            END AS 'Complete',
            *
      FROM #Temp
      need to check in #Diagnose for spect_only = 0 then return it.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>