Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 15:40:55
|
| I have a table with id, test date and test resultsI created a temp table with all results and pulled the most recent as shown below.--1st most recent a1c value--select d.pid, d.a1c1_value, d.a1c_dateinto dbo.bds_a1c1_test1from dbo.bds_a1c1 dgroup by d.pid, d.a1c1_value, d.a1c_datehaving a1c_date = (select max (c.a1c_date)from dbo.bds_a1c1 c whered.pid=c.pid)What I want to do next is the select the next most recent id, date and results so I can update to a second column in my master table. Every time I try the approach below it errors out.select d.pid, d.a1c1_value, d.a1c_datefrom dbo.bds_a1c1 dgroup by d.pid, d.a1c1_value, d.a1c_datehaving d.a1c_date =(select max(d.a1c_date) as a1c_datefrom dbo.bds_a1c1 dgroup by d.pid, d.a1c_datehaving a1c_date < (select max (c.a1c_date)from dbo.bds_a1c1_test1 c whered.pid=c.pid))errorError: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. (State:21000, Native Code: 200) |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 15:41:46
|
| when I pull the 2nd most recent it has to be a unqiue row based on id |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-28 : 15:48:37
|
Try thisselect d.pid, d.a1c1_value, d.a1c_datefrom dbo.bds_a1c1 dgroup by d.pid, d.a1c1_value, d.a1c_datehaving d.a1c_date =(select max(d.a1c_date) as a1c_datefrom dbo.bds_a1c1 d1 where d.pid=d1.pidgroup by d.pid, d.a1c_datehaving a1c_date < (select max (c.a1c_date)from dbo.bds_a1c1_test1 c whered1.pid=c.pid)) PBUH |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 15:54:35
|
| I get this errorError: Each GROUP BY expression must contain at least one column that is not an outer reference. (State:37000, Native Code: A4) |
 |
|
|
barflyz
Starting Member
47 Posts |
Posted - 2010-04-28 : 15:57:38
|
| I changed all references to d1 and get this errorError: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. (State:21000, Native Code: 200) |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-28 : 16:03:31
|
quote: Originally posted by barflyz I get this errorError: Each GROUP BY expression must contain at least one column that is not an outer reference. (State:37000, Native Code: A4)
Sorry I think this should workselect d.pid, d.a1c1_value, d.a1c_datefrom dbo.bds_a1c1 dgroup by d.pid, d.a1c1_value, d.a1c_datehaving d.a1c_date =(select max(d1.a1c_date) as a1c_datefrom dbo.bds_a1c1 d1 where d.pid=d1.pidgroup by d1.pid, d1.a1c_datehaving a1c_date < (select max (c.a1c_date)from dbo.bds_a1c1_test1 c whered1.pid=c.pid)) PBUH |
 |
|
|
|
|
|
|
|