I know looping is not ideal in SQL, but I couldn't think of another way of doing this.
I want each distinct row from this Table 1 to have each distinct date and hour produced on Table 2.
In other words, Table 2 has the dates between 05/01/2014 through 04/30/2015, with each distinct date having 24 rows, one for each hour of the day. I now want each distinct row in Table 1 to have each distinct date on Table 2, with each of its 24 hours.
Table 1:
DROP TABLE Baylor_Raw..MEDICAL_SERVICE_DESC
SELECT MEDICAL_SERVICE_DESC
INTO Baylor_Raw..MEDICAL_SERVICE_DESC
FROM Baylor_Raw..Raw_ADT
WHERE MEDICAL_SERVICE_DESC IS NOT NULL AND MEDICAL_SERVICE_DESC NOT IN ('#N/A','CANCEL','DAY SURGERY','HOSPICE','INFUSION')
Table 2:
DECLARE @DATE DATE
SET @DATE = '05/01/2014'
DECLARE @HOUR INT
SET @HOUR = 0
DROP TABLE Baylor_Raw..DateTable
CREATE TABLE Baylor_Raw..DateTable
(DATE_OF_DISCHARGE DATE
,HOUR_OF_DISCHARGE INT)
WHILE @DATE<'05/01/2015' BEGIN
WHILE @HOUR<25 BEGIN
INSERT INTO Baylor_Raw..DateTable (DATE_OF_DISCHARGE,HOUR_OF_DISCHARGE)
VALUES (@DATE,@HOUR)
SET @HOUR = @HOUR+1
END
SET @DATE = DATEADD(DD,1,@DATE)
SET @HOUR = 0
END
This below attempt did not work. I canceled after the run time exceeded several minutes.
DECLARE @MEDICAL_SERVICE_DESC NVARCHAR(255)
SET @MEDICAL_SERVICE_DESC = (SELECT MIN(MEDICAL_SERVICE_DESC) FROM Baylor_Raw..MEDICAL_SERVICE_DESC)
DECLARE @DATE DATE
SET @DATE = '05/01/2014'
DECLARE @HOUR INT
SET @HOUR = 0
DROP TABLE Baylor_Raw..DateTable
CREATE TABLE Baylor_Raw..DateTable
(MEDICAL_SERVICE_DESC NVARCHAR,
DATE_OF_DISCHARGE DATE
,HOUR_OF_DISCHARGE INT)
WHILE @MEDICAL_SERVICE_DESC IS NOT NULL BEGIN
WHILE @DATE<'05/01/2015' BEGIN
WHILE @HOUR<25 BEGIN
INSERT INTO Baylor_Raw..DateTable (MEDICAL_SERVICE_DESC,DATE_OF_DISCHARGE,HOUR_OF_DISCHARGE)
VALUES (@MEDICAL_SERVICE_DESC,@DATE,@HOUR)
SET @HOUR = @HOUR+1
END
SET @DATE = DATEADD(DD,1,@DATE)
SET @HOUR = 0
END
DELETE FROM Baylor_Raw..MEDICAL_SERVICE_DESC WHERE MEDICAL_SERVICE_DESC = @MEDICAL_SERVICE_DESC
SET @MEDICAL_SERVICE_DESC = (SELECT MIN(MEDICAL_SERVICE_DESC) FROM Baylor_Raw..MEDICAL_SERVICE_DESC)
SET @DATE = '05/01/2014'
END
So you want all distinct records from table1 paired with all records in table2? That is a cross join:
select *
from (select distinct * from table1) t1
cross join table2;
Or do you want them related by date? Then inner-join:
select *
from (select distinct * from table1) t1
inner join table2 t2 on t1.date = t2.date;
You might get better performance from building lists of dates and hours using loops first, and then combining it with the service names just one time using a cross join, something like this. You may need to tweak it a bit because you understand the source data:
DECLARE @dates TABLE (DATE DATE)
DECLARE @hours TABLE (hours INT)
DECLARE @hour INT
,@date DATE
SET @hour = 0
SET @date = '05/01/2014'
WHILE (@hour < 25)
BEGIN
INSERT INTO @hours
SELECT @hour
SET @hour = @hour + 1
END
WHILE (@ DATE < '05/01/2015')
BEGIN
INSERT INTO @dates
SELECT @date
SET @DATE = DATEADD(DD, 1, @DATE)
END
INSERT INTO Baylor_Raw..DateTable (
MEDICAL_SERVICE_DESC
,DATE_OF_DISCHARGE
,HOUR_OF_DISCHARGE
)
SELECT MEDICAL_SERVICE_DESC
,DATE
,hour
FROM Baylor_Raw..MEDICAL_SERVICE_DESC
CROSS JOIN @date d
CROSS JOIN @hours h
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.