OK, I started this problem last week going down a different road. Now I have two views, one with all of the data that I need including a rowID
(Both of these views have many more rows, I have just shown the data for the first study_participant_id.)
Now I want to use the range and study_participant_id from the second view to get the PSA_Values from the first view. I
have tried variations on
and
but these both give me only the last record in the view. It is correct, but it is the last record and I want to get all of the sets. Below is teh output for both of these last two queries
Code:
RowID study_participant_id proc_labs_psa_date proc_labs_psa_value Phase inidate
1 08800021 2008-07-02 00:00:00.000 1.44 BLN 2013-01-22 09:24:02.183
2 08800021 2008-10-13 00:00:00.000 1.30 BLN 2013-01-22 09:24:02.183
3 08800021 2009-02-09 00:00:00.000 1.20 FU 2013-01-22 09:24:02.183
4 08800021 2009-04-01 00:00:00.000 1.40 FU 2013-01-22 09:24:02.183
5 08800021 2009-07-01 00:00:00.000 1.43 FU 2013-01-22 09:24:02.183
6 08800021 2009-10-14 00:00:00.000 1.33 FU 2013-01-22 09:24:02.183
7 08800021 2009-12-15 00:00:00.000 2.01 FU 2013-01-22 09:24:02.183
8 08800021 2010-04-05 00:00:00.000 5.30 FU 2013-01-22 09:24:02.183
9 08800021 2010-07-05 00:00:00.000 1.60 FU 2013-01-22 09:24:02.183
10 08800021 2010-09-24 00:00:00.000 1.50 FU 2013-01-22 09:24:02.183
11 08800021 2010-12-29 00:00:00.000 1.40 FU 2013-01-22 09:24:02.183
12 08800021 2011-04-14 00:00:00.000 1.80 FU 2013-01-22 09:24:02.183
13 08800021 2011-06-24 00:00:00.000 1.60 FU 2013-01-22 09:24:02.183
14 08800021 2011-09-28 00:00:00.000 1.30 FU 2013-01-22 09:24:02.183
15 08800021 2011-12-16 00:00:00.000 1.90 FU 2013-01-22 09:24:02.183
16 08800021 2012-04-13 00:00:00.000 1.90 FU 2013-01-22 09:24:02.183
17 08800021 2012-08-08 00:00:00.000 1.90 FU 2013-01-22 09:24:02.183
[/code
and another that whittles this down to give me that ranges that I need
[code]
newrowid Study_Participant_ID highrow lowrow
1 08800021 6 1
2 08800021 7 1
3 08800021 8 1
4 08800021 9 2
5 08800021 10 2
6 08800021 11 3
7 08800021 12 5
8 08800021 13 5
9 08800021 14 6
10 08800021 15 8
11 08800021 16 9
12 08800021 17 10
Now I want to use the range and study_participant_id from the second view to get the PSA_Values from the first view. I
have tried variations on
Code:
DECLARE @newid int, @intRowCount int, @spid int, @hr int, @lr int
SELECT @newid = -1, -- Smaller value than any real data
@intRowCount = 1 -- Force first iteration
WHILE @intRowCount >= 1
BEGIN
SELECT @newid = MIN(newrowid)
,@spid=[Study_Participant_ID]
,@hr=[highrow]
,@lr=[lowrow]
FROM [dbEDRN195].[dbo].[PSALimits]
WHERE newrowid > @newid
group by Study_Participant_ID, highrow, lowrow
SELECT @intRowCount = @@ROWCOUNT
IF @intRowCount >= 1
BEGIN
select * from dbEDRN195.dbo.PSADates_Values
where study_participant_id=@spid
and RowID >= @lr and RowID<=@hr
END
END
and
Code:
declare @newid int
declare @spid int
declare @hr int
declare @lr int
SELECT @newid=MIN(newrowid)
,@spid=[Study_Participant_ID]
,@hr=[highrow]
,@lr=[lowrow]
FROM [dbEDRN195].[dbo].[PSALimits]
group by Study_Participant_ID, highrow, lowrow
print @newid
print @hr
print @lr
WHILE @newid is not null
begin
select * from dbEDRN195.dbo.PSADates_Values
where study_participant_id=@spid
and RowID >= @lr and RowID<=@hr
select @newid=MIN(newrowid) from dbEDRN195.dbo.psalimits where newrowid>@newid
end
but these both give me only the last record in the view. It is correct, but it is the last record and I want to get all of the sets. Below is teh output for both of these last two queries
Code:
RowID study_participant_id proc_labs_psa_date proc_labs_psa_value Phase inidate
10 54500442 2010-10-11 00:00:00.000 7.70 BLN 2013-01-22 09:31:02.833
11 54500442 2011-05-16 00:00:00.000 8.90 BLN 2013-01-22 09:31:02.833
12 54500442 2011-08-22 00:00:00.000 8.00 BLN 2013-01-22 09:31:02.833
13 54500442 2012-02-10 00:00:00.000 8.05 BLN 2013-01-22 09:31:02.833
14 54500442 2012-08-16 00:00:00.000 9.90 BLN 2013-01-22 09:31:02.833