asked on
- Mike first visit goes to ER on 1/1/2014 :- output this data
- Mike goes again to ER on 1/3/2014 :- output this data
- Mike goes to Clinic on 1/20/2014 :- do not output this data
- Mike goes to Clinic on 5/2/2014 : output this data
- David first visit goes to Clinic on 3/3/2014 : output this data
- David goes back again to Clinic on 3/4/2014 : do not output this data
- David goes to ER on 3/15/2014 : output this data
NAME LOCATION DATE
MIKE ER 1/1/2014
MIKE ER 1/3/2014
MIKE CLINIC 1/20/2014
MIKE CLINIC 5/2/2014
DAVID CLINIC 3/3/2014
DAVID CLINIC 3/4/2014
DAVID ER 3/15/2014
NAME LOCATION DATE
MIKE ER 1/1/2014
MIKE ER 1/3/2014
MIKE CLINIC 5/2/2014
DAVID CLINIC 3/3/2014
DAVID ER 3/15/2014
SET NOCOUNT ON
GO
DECLARE @Init_DataSource TABLE
(
[NAME] VARCHAR(20)
,[DATE] DATE
,[LOCATION] VARCHAR(15)
)
INSERT INTO @Init_DataSource ([NAME], [DATE], [LOCATION])
VALUES ('MIKE', '1-1-2014', 'ER')
,('MIKE', '1-3-2014', 'ER')
,('MIKE', '1-20-2014', 'CLINIC')
,('MIKE', '5-2-2014', 'CLINIC')
,('DAVID', '3-3-2014', 'CLINIC')
,('DAVID', '3-4-2014', 'CLINIC')
,('DAVID', '3-15-2014', 'ER')
SELECT *
FROM @Init_DataSource
ORDER BY 1 DESC, 2 ASC
ASKER
--first visit was ER
or (select itemlocation from cte1 where itemrank=1 and personID=name)= 'ER' AND location ='ER'
or (select itemlocation from cte1 where itemrank=1 and personID=name)= 'ER' AND location ='CLINIC'
AND dateadd(d, -90,itemdate) >(select itemdate from cte1 where itemrank=1 and personID=name)
--first visit was CLINIC
or ((select itemlocation from cte1 where itemrank=1 and personID=name)= 'CLINIC'
AND dateadd(d, -2,itemdate) >(select itemdate from cte1 where itemrank=1 and personID=name)
AND location = 'ER')
because i was told to use Netezza database so i need to convert these codes to some kind of postgresql or some syntax that supports netezza. It seems to work find in SQL but not in Netezza. thanks
ASKER
create TABLE dbo.DataSource
(
[NAME] VARCHAR(20)
,[DATE] DATE
,[LOCATION] VARCHAR(15)
)
INSERT INTO DataSource ([NAME], [DATE], [LOCATION])
VALUES ('MIKE', '1-1-2014', 'ER')
,('MIKE', '1-3-2014', 'ER')
,('MIKE', '1-20-2014', 'CLINIC')
,('MIKE', '5-2-2014', 'CLINIC')
,('DAVID', '3-3-2014', 'CLINIC')
,('DAVID', '3-4-2014', 'CLINIC')
,('DAVID', '3-7-2014', 'CLINIC')
,('DAVID', '3-15-2014', 'ER')
thanks
ITEMRANK NAME DATE LOCATION
1 DAVID 2014-03-03 CLINIC
4 DAVID 2014-03-15 ER
1 MIKE 2014-01-01 ER
2 MIKE 2014-01-03 ER
4 MIKE 2014-05-02 CLINIC
ASKER
ASKER
ASKER
OR ((SELECT ITEMLOC_TYPE FROM CTE1 WHERE ITEMRANK = 1 AND PERSONID=PI_NBR) IN ('ER', 'OBS', 'ELEC','EXY','INP','OBS', 'URG') AND LOC_TYPE IN ('ER', 'OBS', 'ELEC','EXY','INP','OBS', 'URG'))
OR ((SELECT ITEMLOC_TYPE FROM CTE1 WHERE ITEMRANK = 1 AND PERSONID=PI_NBR) IN ('ER', 'OBS', 'ELEC','EXY','INP','OBS', 'URG') AND LOC_TYPE IN ('CLINIC', 'OP', 'OUTP')
so is there a way to overcome this issue or adjust the query like using subquery so i can overcome this issue? thanks
ASKER
ASKER
ASKER
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
http://sqlfiddle.com/#!3/200bb/32
Schema:
Open in new window
Query:
Open in new window
At present, this will only work if properly if the patient has only one event per day, as the date forms part of the join condition.
The data schema could be improved by addition of a unique ID to each patient event.