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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 inner join question

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 tbMain

LEFT JOIN tbUser
ON TbMain.ID = tbUser.ID
INNER 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 DESC

Everyting 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 DESC

The 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 query

SELECT top 1
SI.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 tbMain

LEFT JOIN tbUser
ON TbMain.ID = tbUser.ID
INNER JOIN
(
SELECT
SI.ID,
SR.COMPANY,
SR.REQUESTOR_NAME,
SR.DATE_RECEIVED
FROM SI
INNER JOIN SR ON SI.SR_ID = SR.SR_ID
INNER JOIN (SELECT SR_ID,MAX(DATE_RECEIVED) AS MaxDate
FROM SR
GROUP BY SR_ID)tmp
ON tmp.SR_ID=SR.SR_ID
AND tmp.MaxDate=SR.DATE_RECEIVED

)t ON t.ID = TbMain.ID
Where TbMain.MEMBER_ID = @MEMBER_ID
ORDER BY t.DATE_RECEIVED DESC[/code]
Go to Top of Page

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.
Go to Top of Page

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.
Go to Top of Page

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 table
Has ID column that contains unique ids.
ID|Member ( not unique)
12345 12121
56843 12121
45844 156476

The SI table has ids (not unique)
SI table:
ID | SR_ID
12345 1
56843 4
12345 6
56843 46
12345 23

The SR
It 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
Go to Top of Page

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 table
Has ID column that contains unique ids.
ID|Member ( not unique)
12345 12121
56843 12121
45844 156476

The SI table has ids (not unique)
SI table:
ID | SR_ID
12345 1
56843 4
12345 6
56843 46
12345 23

The SR
It 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 tbMain

LEFT JOIN tbUser
ON TbMain.ID = tbUser.ID
INNER JOIN
(
SELECT
SI.ID,
SR.COMPANY,
SR.REQUESTOR_NAME,
SR.DATE_RECEIVED
FROM (SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID)SI
INNER 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

Go to Top of Page

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_RECEIVED
FROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SI
INNER 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_RECEIVED
And not the smallest number.

i.e If I run this query:
SELECT SI.ID, SR.DATE_RECEIVED, SR.SR_ID
FROM SI
INNER JOIN SR ON SI.SR_ID = SR.SR_ID
WHERE SI.ID= '05'
order by SR.DATE_RECEIVED desc

I would get:
ID DATE_RECEIVED SR_ID
------------------------------------------------ --------------------------- ------------------
05 2002-07-02 00:00:00.000 4511
05 2002-07-04 00:00:00.000 4539
05 2003-03-12 00:00:00.000 6837
05 2006-01-26 00:00:00.000 20087
05 2006-05-23 00:00:00.000 21878
05 2005-05-19 00:00:00.000 8003062

The 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!!
Go to Top of Page

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_RECEIVED
FROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SI
INNER 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_RECEIVED
And not the smallest number.

i.e If I run this query:
SELECT SI.ID, SR.DATE_RECEIVED, SR.SR_ID
FROM SI
INNER JOIN SR ON SI.SR_ID = SR.SR_ID
WHERE SI.ID= '05'
order by SR.DATE_RECEIVED desc

I would get:
ID DATE_RECEIVED SR_ID
------------------------------------------------ --------------------------- ------------------
05 2002-07-02 00:00:00.000 4511
05 2002-07-04 00:00:00.000 4539
05 2003-03-12 00:00:00.000 6837
05 2006-01-26 00:00:00.000 20087
05 2006-05-23 00:00:00.000 21878
05 2005-05-19 00:00:00.000 8003062

The 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_RECEIVED
FROM ( SELECT ID,MIN(SR_ID)AS MinID FROM SI GROUP BY ID ) SI
INNER JOIN SR ON SI.MinID= SR.SR_ID
INNER JOIN (SELECT SR_ID,MAX(DATE_RECEIVED) AS MaxDate
FROM SR
GROUP BY SR_ID)tmp
ON tmp.SR_ID=SR.SR_ID
AND tmp.MaxDate=SR.DATE_RECEIVED
)



Go to Top of Page

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..
Go to Top of Page

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.
Go to Top of Page

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
--
12012
89890
89890
797223..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 data
OUtput:
--
ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..
12012 2323 2002-07-02 00:00:00.000 ...etc
12012 2311 2005-07-02 00:00:00.000
89890 8789 ..etc
89890 23245
89787 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
Go to Top of Page

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
--
12012
89890
89890

797223..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 data
OUtput:
--
ID SR_ID |(data from SR table) DATE_RECEIVED | Requestor Name.. etcc..
12012 2323 2002-07-02 00:00:00.000 ...etc
12012 2311 2005-07-02 00:00:00.000
89890 8789 ..etc
89890 23245
89787 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!
Go to Top of Page

tarz
Starting Member

31 Posts

Posted - 2008-05-28 : 13:01:29
oopss sorry I meant to write different IDs for TblMain table.
Go to Top of Page

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?
Go to Top of Page

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,
Go to Top of Page
   

- Advertisement -