| Author |
Topic |
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-23 : 15:38:06
|
| Please help me solve this.. Here is my query:=============================================================declare @MEMBER_ID varchar(20) set @MEMBER_ID ='11212'Select TbMain.ID, tbUser.EMAIL_ADDR, t.COMPANY, t.REQUESTOR_NAME, t.DATE_RECEIVED FROM tbMainLEFT JOIN tbUserON TbMain.ID = tbUser.IDINNER JOIN ( SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME, SR.DATE_RECEIVED FROM SI INNER JOIN SR ON SI.SR_ID = SR.SR_ID)t ON t.ID = TbMain.ID Where TbMain.MEMBER_ID = @MEMBER_ID ORDER BY t.DATE_RECEIVED DESCEveryting works fine.. I need to modify the code in bold (INNER JOIN)SELECT ID FROM SI WHERE SI.ID = '010001' - Can have more than one record!! I need to display one record, the latest based on the ORDER BY t.DATE_RECEIVED DESCThe way it works now.. It display all so if I have 10 records under the same ID it display all of them.. I wrote this query: That only returns one record (Works fine) But I don't know how to implement it to my main querySELECT top 1SI.ID, SR.COMPANY, SR.REQUESTOR_NAME,SR.DATE_RECEIVED FROM SI INNER JOIN SR ON SI.SR_ID = SR.SR_ID WHERE SI.ID = '223'order by SR.DATE_RECEIVED desc Please help me make it work.. Thank you!! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-23 : 15:46:18
|
| [code]declare @MEMBER_ID varchar(20) set @MEMBER_ID ='11212'Select TbMain.ID, tbUser.EMAIL_ADDR, t.COMPANY, t.REQUESTOR_NAME, t.DATE_RECEIVED FROM tbMainLEFT JOIN tbUserON TbMain.ID = tbUser.IDINNER JOIN (SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME,SR.DATE_RECEIVEDFROM SIINNER JOIN SR ON SI.SR_ID = SR.SR_IDINNER JOIN (SELECT SR_ID,MAX(DATE_RECEIVED) AS MaxDate FROM SR GROUP BY SR_ID)tmpON tmp.SR_ID=SR.SR_IDAND tmp.MaxDate=SR.DATE_RECEIVED)t ON t.ID = TbMain.ID Where TbMain.MEMBER_ID = @MEMBER_ID ORDER BY t.DATE_RECEIVED DESC[/code] |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-23 : 16:58:19
|
| Thanks. I know it is hard to test on your machine..But it still shows all the records. It shows the latest one first.. but the others are displayed as well. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-24 : 00:42:47
|
quote: Originally posted by tarz Thanks. I know it is hard to test on your machine..But it still shows all the records. It shows the latest one first.. but the others are displayed as well.
Do you have more than one record with same date received value?Also it would be really helpful if you can show us some sample data from your tables. |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-26 : 15:25:13
|
| Hi.. Thanks for your help.. Please see the structure of the tables.. I hope it is more clear now..The TbMain tableHas ID column that contains unique ids.ID|Member ( not unique) 12345 1212156843 1212145844 156476The SI table has ids (not unique)SI table:ID | SR_ID12345 156843 412345 656843 46 12345 23The SRIt will search and will grab ONE RECORD based on the SR_ID column and the latest date_received.Note: might have the same date_recieved so would just grab the first one..Thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-26 : 23:47:31
|
quote: Originally posted by tarz Hi.. Thanks for your help.. Please see the structure of the tables.. I hope it is more clear now..The TbMain tableHas ID column that contains unique ids.ID|Member ( not unique) 12345 1212156843 1212145844 156476The SI table has ids (not unique)SI table:ID | SR_ID12345 156843 412345 656843 46 12345 23The SRIt will search and will grab ONE RECORD based on the SR_ID column and the latest date_received.Note: might have the same date_recieved so would just grab the first one..Thank you
May be this. Does SR table have only records with unique SR_ID?declare @MEMBER_ID varchar(20) set @MEMBER_ID ='11212'Select TbMain.ID, tbUser.EMAIL_ADDR, t.COMPANY, t.REQUESTOR_NAME, t.DATE_RECEIVED FROM tbMainLEFT JOIN tbUserON TbMain.ID = tbUser.IDINNER JOIN (SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME,SR.DATE_RECEIVEDFROM (SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID)SIINNER JOIN SR ON SI.MinID= SR.SR_ID)t ON t.ID = TbMain.ID Where TbMain.MEMBER_ID = @MEMBER_ID ORDER BY t.DATE_RECEIVED DESC |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-27 : 11:34:16
|
| yes the SR table has only unique SR_ID values.I appreciate you helping me out.. It is almost done. IT is now grabbing one record!! But there is small bug in the FROM clause:INNER JOIN (SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME, SR.DATE_RECEIVEDFROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SIINNER JOIN SR ON SI.MinID= SR.SR_ID)It is grabbing the minimum number.. This is not correct. I need to grab the latest based on DATE_RECEIVEDAnd not the smallest number. i.e If I run this query:SELECT SI.ID, SR.DATE_RECEIVED, SR.SR_ID FROM SIINNER JOIN SR ON SI.SR_ID = SR.SR_ID WHERE SI.ID= '05'order by SR.DATE_RECEIVED descI would get: ID DATE_RECEIVED SR_ID ------------------------------------------------ --------------------------- ------------------ 05 2002-07-02 00:00:00.000 451105 2002-07-04 00:00:00.000 453905 2003-03-12 00:00:00.000 683705 2006-01-26 00:00:00.000 2008705 2006-05-23 00:00:00.000 2187805 2005-05-19 00:00:00.000 8003062The code you posted would give me the first record because the SR_ID is the smallest number. I would need to get the SR_ID = 21878 because it is the latest based on the date.Thank you!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-27 : 11:45:18
|
quote: Originally posted by tarz yes the SR table has only unique SR_ID values.I appreciate you helping me out.. It is almost done. IT is now grabbing one record!! But there is small bug in the FROM clause:INNER JOIN (SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME, SR.DATE_RECEIVEDFROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SIINNER JOIN SR ON SI.MinID= SR.SR_ID)It is grabbing the minimum number.. This is not correct. I need to grab the latest based on DATE_RECEIVEDAnd not the smallest number. i.e If I run this query:SELECT SI.ID, SR.DATE_RECEIVED, SR.SR_ID FROM SIINNER JOIN SR ON SI.SR_ID = SR.SR_ID WHERE SI.ID= '05'order by SR.DATE_RECEIVED descI would get: ID DATE_RECEIVED SR_ID ------------------------------------------------ --------------------------- ------------------ 05 2002-07-02 00:00:00.000 451105 2002-07-04 00:00:00.000 453905 2003-03-12 00:00:00.000 683705 2006-01-26 00:00:00.000 2008705 2006-05-23 00:00:00.000 2187805 2005-05-19 00:00:00.000 8003062The code you posted would give me the first record because the SR_ID is the smallest number. I would need to get the SR_ID = 21878 because it is the latest based on the date.Thank you!!
then make it like this:-INNER JOIN (SELECT SI.ID, SR.COMPANY, SR.REQUESTOR_NAME, SR.DATE_RECEIVEDFROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SIINNER JOIN SR ON SI.MinID= SR.SR_IDINNER JOIN (SELECT SR_ID,MAX(DATE_RECEIVED) AS MaxDate FROM SR GROUP BY SR_ID)tmpON tmp.SR_ID=SR.SR_IDAND tmp.MaxDate=SR.DATE_RECEIVED) |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-27 : 17:01:29
|
| I changed it . and it still grabs the record with the smallest SR_ID value.. My quess it is to do with ...MIN(SR_ID)AS MinID... Why do we need it there? since it can be always different.. we need to retrieve based on MAX(DATE_RECEIVED)...OMg why is it so complicated.. Thank you very much for help me out.. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-28 : 00:50:29
|
quote: Originally posted by tarz I changed it . and it still grabs the record with the smallest SR_ID value.. My quess it is to do with ...MIN(SR_ID)AS MinID... Why do we need it there? since it can be always different.. we need to retrieve based on MAX(DATE_RECEIVED)...OMg why is it so complicated.. Thank you very much for help me out..
Can you specify how these tables are related to each other illustrated by some sample data and the o/p you want out of them? That would be really helpful to give you accurate solution. |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-28 : 11:33:25
|
| Let me try again!MemberID gets entered "11212" | |Gets all the ids from tblMain table where memberID is 11212. The Ids are unique!!Output:ID--120128989089890797223..etc.. | |Then Get the SR_ID from SI table using the ID found in TblMain table.the SI table can have multiple same IDs. Also join SR table using the SR_ID to get the rest of the dataOUtput:--ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..12012 2323 2002-07-02 00:00:00.000 ...etc12012 2311 2005-07-02 00:00:00.000 89890 8789 ..etc89890 2324589787 98976||One record for each ID will be returned.. based on the latest DATE_RECEIVED..The output: for ID 12012 would be..ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..12012 2311 2005-07-02 00:00:00.000 ..ect..I hope is it more clear now!!Thank you |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-28 : 12:13:48
|
quote: Originally posted by tarz Let me try again!MemberID gets entered "11212" | |Gets all the ids from tblMain table where memberID is 11212. The Ids are unique!!Output:ID--120128989089890797223..etc.. | |Then Get the SR_ID from SI table using the ID found in TblMain table.the SI table can have multiple same IDs. Also join SR table using the SR_ID to get the rest of the dataOUtput:--ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..12012 2323 2002-07-02 00:00:00.000 ...etc12012 2311 2005-07-02 00:00:00.000 89890 8789 ..etc89890 2324589787 98976||One record for each ID will be returned.. based on the latest DATE_RECEIVED..The output: for ID 12012 would be..ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..12012 2311 2005-07-02 00:00:00.000 ..ect..I hope is it more clear now!!Thank you
Not yet. You've told id is unique in TblMain but sample shows duplicates! |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-28 : 13:01:29
|
| oopss sorry I meant to write different IDs for TblMain table. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-05-28 : 13:07:24
|
quote: Originally posted by tarz oopss sorry I meant to write different IDs for TblMain table.
Can you please provide a typical scenario ( single ID's) records from tblMain,SR & SI? |
 |
|
|
tarz
Starting Member
31 Posts |
Posted - 2008-05-28 : 13:17:25
|
| Not sure what you mean by: "provide a typical scenario ( single ID's) records from tblMain,SR & SI?"It is possible to get a single Id in SI table. Than it would just display it's record...Thanks, |
 |
|
|
|