| Author |
Topic |
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-07 : 11:07:35
|
Hi All.I have two table:Request=============================Request_Id,CustomerNo,LName,FName,ServiceDateandPayment=============================Payment_Id,Request_Id,PaidDate,AmountI would like join both table and select all fields from Request table with max(PaidDate) from Payment table.How it to do?I tried like:select r.Request_Id,Customer_No,LName,FName,s.Status,x.EffDate,Amountfrom dbo.Request r, (SELECT Status_Id, Request_Id, MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Status_Id) xjoin dbo.v_Status son x.Status_Id = s.Status_Idwhere r.Request_Id = x.Request_Id but that is wrong.Thanks. |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-07 : 11:26:29
|
| what is the error you getting?--------------------------Joins are what RDBMS's do for a living |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-07 : 11:29:57
|
| Thanks for replay.The error is:"Column 'dbo.Payments.Request_Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-07 : 11:35:03
|
dont mix old and new join syntaxes. use consistent methodAlso i dont think relationship is proper. my guess is it would be something like belowif you can post sample data it might be more clear for usselect r.Request_Id,Customer_No,LName,FName,s.Status,p.EffDate,Amountfrom dbo.Request rINNER JOIN dbo.Payments pON p.Request_ID = r.Request_IDINNER JOIN (SELECT Request_Id,Status_Id, MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Request_ID,Status_Id) xON p.Request_Id = x.Request_IdAND p.Status_Id = x.Status_IdInner join dbo.v_Status son p.Status_Id = s.Status_Id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
xhostx
Constraint Violating Yak Guru
277 Posts |
Posted - 2012-08-07 : 11:36:22
|
you never selected Request_id inside the inner query(SELECT Status_Id, MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Status_Id) x and you are trying to call it using an an alia here where r.Request_Id = x.Request_Id try to add it inside the inner query, if this won't change the group by result.--------------------------Joins are what RDBMS's do for a living |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-07 : 11:56:11
|
| Base on visakh16 code I got result of select:54 12345678911 Dou John Adjustment 2012/08/06 100.0054 12345678911 Dou John Redetermination 2012/08/07 100.00But I would like to get in select records only with latest PaidDate.Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-07 : 11:58:29
|
quote: Originally posted by eugz Base on you code I got result of select:54 12345678911 Dou John Adjustment 2012/08/06 100.0054 12345678911 Dou John Redetermination 2012/08/07 100.00But I would like to get in select record only with latest PaidDate.Thanks.
how are tables related?please post some data from tables in below formathttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-07 : 12:17:40
|
| Hi visakh16.That is tables and dataPayments [Payment_Id] [bigint] IDENTITY(1,1) NOT NULL, [Request_Id] [bigint] NULL, [Status_Id] [bigint] NULL, [PaidDate] [datetime] NULL, [PaidAmount] [smallmoney] NULL57 54 1719 NULL 2012-08-06 00:00:00.000 False58 54 1728 NULL 2012-08-07 00:00:00.000 FalseRequest [Request_Id] [bigint] IDENTITY(1,1) NOT NULL, [CustomerNo] [varchar](11) NULL, [LName] [varchar](50) NULL, [FName] [varchar](50) NULL, [ServiceDate] [datetime] NULL, [Status_Id] [bigint] NULL, [Amount] [smallmoney] NULL54 12345678911 Dou John 2012-08-06 00:00:00.000 NULL 100.0000 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-07 : 12:18:00
|
it can even be thisbut I'm not sure unless you show me how the data is in your tablesselect r.Request_Id,Customer_No,LName,FName,s.Status,p.EffDate,Amountfrom dbo.Request rINNER JOIN dbo.Payments pON p.Request_ID = r.Request_IDINNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Request_ID) xON p.Request_Id = x.Request_IdAND p.PaidDate = x.EffDateInner join dbo.v_Status son p.Status_Id = s.Status_Id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-07 : 15:05:55
|
| Thanks. It works. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-07 : 15:09:53
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-08 : 16:01:10
|
| Hi visakh16.I found problem. If Request table has new record and Payment table doesn't has record for that Request_Id, that new record not display in select. How to fix it?Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-08 : 16:08:59
|
| [code]select r.Request_Id,Customer_No,LName,FName,s.Status,p.EffDate,Amountfrom dbo.Request rLEFT JOIN(SELECT p.*FROM dbo.Payments pINNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Request_ID) xON p.Request_Id = x.Request_IdAND p.PaidDate = x.EffDate)tON t.Request_ID = r.Request_IDLeft join dbo.v_Status son t.Status_Id = s.Status_Id[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-08 : 16:37:27
|
| Hi visakh16.p.EffDate is red wave underline with tooltip - The multi-part identifier "p.EffDate" could not be bound.I tried to say x.EffDate but result the same.Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-08-08 : 17:22:45
|
quote: Originally posted by eugz Hi visakh16.p.EffDate is red wave underline with tooltip - The multi-part identifier "p.EffDate" could not be bound.I tried to say x.EffDate but result the same.Thanks.
it should be tselect r.Request_Id,Customer_No,LName,FName,s.Status,t.EffDate,Amountfrom dbo.Request rLEFT JOIN(SELECT p.*FROM dbo.Payments pINNER JOIN (SELECT Request_Id,MAX(PaidDate) AS EffDate FROM dbo.Payments GROUP BY Request_ID) xON p.Request_Id = x.Request_IdAND p.PaidDate = x.EffDate)tON t.Request_ID = r.Request_IDLeft join dbo.v_Status son t.Status_Id = s.Status_Id ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
eugz
Posting Yak Master
210 Posts |
Posted - 2012-08-08 : 17:27:10
|
| Thanks. |
 |
|
|
|