| 
                
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 |  
                                    | dhinasqlPosting Yak  Master
 
 
                                        195 Posts | 
                                            
                                            |  Posted - 2013-11-27 : 23:37:56 
 |  
                                            | Hi Friends,I have 3 tables, please find the table structure and sample data belowTable 1 : tblUserInfoUserID | FirstName | Email | JoinedDate1 Testuser01 User01@User.com 10-10-20132 Testuser10 User10@User.com 11-10-20133 Testuser20 User10@User.com 11-10-2013Table 2 : tblOffersOfferID | OfferName | ExpiryDate1 OfferSample1 15-10-20142 OfferSample2 15-9-20143 OfferSample3 10-07-2014Table 3 :tblOfferActivatedActivationID | UserID | OfferID | ActivationCode | ActivatedDate1 2 3 ABC 11-11-2013 2 2 1 CEG 13-11-20133 3 1 JHG 18-11-20134 3 2 KIU 20-11-2013Expected OutputI want to list out the users with the first activated offer details.The OfferName Should be based on the first activated date UserID | FirstName | Email | JoinedDate | OfferID | OfferName |ActivatedDate | ActivationCode1 Testuser01 User01@User.com 10-10-2013 Null Null Null Null2 Testuser10 User10@User.com 11-10-2013 3 OfferSample2 11-11-2013 ABC3 Testuser20 User10@User.com 11-10-2013 4 offerSample3 18-11-2013 JHG |  |  
                                    | bandiMaster Smack Fu Yak Hacker
 
 
                                    2242 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 01:10:51 
 |  
                                          | SELECT ui.*, oa.ActivatedDate, oa.ActivationCode, o.OfferName, o.OfferID FROM (SELECT ActivationID , UserID , OfferID , ActivationCode, ActivatedDate 		, ROW_NUMBER() OVER(PARTITION by UserID ORDER BY ActivatedDate) RN    FROM tblOfferActivated ) oajoin tblUserInfo ui ON ui.UserID = oa.UserID JOiN tblOffers o ON o.OfferID = oa.OfferID WHERE RN=1--Chandu |  
                                          |  |  |  
                                    | dhinasqlPosting Yak  Master
 
 
                                    195 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 03:26:22 
 |  
                                          | quote:Hi Chandu,Thank you for your Query, But my primary thing is I would like to list out all the users whether they have active offer or Not. When there is no Offer for the user i want to display 'Null' for  OfferID | OfferName |ActivatedDate | ActivationCodeExample : Suppose user 'TestUser30' is there on the tblUserInfo, But dont have any activated offer on tblOfferActivated, Then my expected out put :UserID | FirstName | Email | JoinedDate | OfferID | OfferName |ActivatedDate | ActivationCode1 Testuser01 User01@User.com 10-10-2013 3 OfferSample1 15-11-2013 BAC 2 Testuser10 User10@User.com 11-10-2013 1 OfferSample3 11-11-2013 ABC3 Testuser20 User20@User.com 11-10-2013 4 offerSample1 18-11-2013 JHG4 TestUser30 User30@User.com 15-10-2013 Null Null Null NullLet me know if you need more details in this regards.Originally posted by bandi
 SELECT ui.*, oa.ActivatedDate, oa.ActivationCode, o.OfferName, o.OfferID FROM (SELECT ActivationID , UserID , OfferID , ActivationCode, ActivatedDate 		, ROW_NUMBER() OVER(PARTITION by UserID ORDER BY ActivatedDate) RN    FROM tblOfferActivated ) oajoin tblUserInfo ui ON ui.UserID = oa.UserID JOiN tblOffers o ON o.OfferID = oa.OfferID WHERE RN=1--Chandu
 
 |  
                                          |  |  |  
                                    | dhinasqlPosting Yak  Master
 
 
                                    195 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 04:27:18 
 |  
                                          | quote:Friends,Let me know if you need more details in this regards.Originally posted by dhinasql
 
 quote:Hi Chandu,Thank you for your Query, But my primary thing is I would like to list out all the users whether they have active offer or Not. When there is no Offer for the user i want to display 'Null' for  OfferID | OfferName |ActivatedDate | ActivationCodeExample : Suppose user 'TestUser30' is there on the tblUserInfo, But dont have any activated offer on tblOfferActivated, Then my expected out put :UserID | FirstName | Email | JoinedDate | OfferID | OfferName |ActivatedDate | ActivationCode1 Testuser01 User01@User.com 10-10-2013 3 OfferSample1 15-11-2013 BAC 2 Testuser10 User10@User.com 11-10-2013 1 OfferSample3 11-11-2013 ABC3 Testuser20 User20@User.com 11-10-2013 4 offerSample1 18-11-2013 JHG4 TestUser30 User30@User.com 15-10-2013 Null Null Null NullLet me know if you need more details in this regards.Originally posted by bandi
 SELECT ui.*, oa.ActivatedDate, oa.ActivationCode, o.OfferName, o.OfferID FROM (SELECT ActivationID , UserID , OfferID , ActivationCode, ActivatedDate 		, ROW_NUMBER() OVER(PARTITION by UserID ORDER BY ActivatedDate) RN    FROM tblOfferActivated ) oajoin tblUserInfo ui ON ui.UserID = oa.UserID JOiN tblOffers o ON o.OfferID = oa.OfferID WHERE RN=1--Chandu
 
 
 |  
                                          |  |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 06:10:50 
 |  
                                          | [code]SELECT *FROM tblUserInfo uiOUTER APPLY (              SELECT TOP 1 o.OfferID,OfferName, ActivationCode,ActivatedDate              FROM tblOffers o              INNER JOIN tblOfferActivated oa              ON oa.OfferID  = o.OfferID               WHERE oa.UserID = ui.UserID              ORDER BY ActivatedDate ASC             )off[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |  
                                          |  |  |  
                                    | dhinasqlPosting Yak  Master
 
 
                                    195 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 06:40:35 
 |  
                                          | quote:Thanks Visakh.It works fine.Originally posted by visakh16
 
 SELECT *FROM tblUserInfo uiOUTER APPLY (              SELECT TOP 1 o.OfferID,OfferName, ActivationCode,ActivatedDate              FROM tblOffers o              INNER JOIN tblOfferActivated oa              ON oa.OfferID  = o.OfferID               WHERE oa.UserID = ui.UserID              ORDER BY ActivatedDate ASC             )off------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs 
 |  
                                          |  |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2013-11-28 : 07:01:34 
 |  
                                          | welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |  
                                          |  |  |  
                                |  |  |  |  |  |