Author |
Topic |
Yonkouturko
Yak Posting Veteran
59 Posts |
Posted - 2013-05-16 : 22:23:22
|
SELECT ReferenceNumber,(SELECT TOP (1) MODIFIED_DATE FROM Table_FOLLOW_UP WHERE (REFERENCENUMBER = Table_Customer_Record.ID) ORDER BY ID DESC) AS LAST_FOLLOW_UP_DATE,(SELECT TOP (1) REMARKS_STATUS FROM Table_FOLLOW_UP AS Table_FOLLOW_UP_1 WHERE (REFERENCENUMBER = Table_Customer_Record.ID) ORDER BY ID DESC) AS LAST_FOLLOW_UP_STATUS, Company_Name, Contact_Person, Email_Address, Line_of_Business, Telephone_No, Mobile_No, Fax_No, Address, Area, Sales_Representative,Credit_Limit, Remaining_Balance, Website, Terms, Mode_of_payment, Type_of_payment, Local_Service, Site_Service, Member, Sales_End_User, Sales_Corporate, ID, Internet_Cafe, ACTIVEFROM Table_Customer_RecordORDER BY IDNOW I HAVE Two(2) tables.. CUSTOMER RECORDS TABLE and FOLLOW UP TABLEThe goal of this sql query is to get Customer Records WITH the LAST FOLLOW DONEfor each Customer RECORDS..actually that query did it but my problem was... The Query Result is Slow TO displayin my vb.net application now one of my friends did tell me that there is another way of getting my goal done with out using this type of query(because its slow to get records, cause my query has PROCESS within PROCESS thats why its slow and that is his explanation of things)he said to me that i must use JOIN(or OUTER JOIN Specifically) now i've tried LEFT OUTER JOIN but My CUSTOMER RECORDS is Duplicated...if anyone knows some idea on how to use JOINS properly please lend me your Intelligence on this topic...thank you Very much!!! any idea or help is very much appreciated |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-17 : 00:29:06
|
[code]SELECT ReferenceNumber,t.MODIFIED_DATE AS LAST_FOLLOW_UP_DATE,t.REMARKS_STATUS AS LAST_FOLLOW_UP_STATUS,Company_Name, Contact_Person,Email_Address, Line_of_Business, Telephone_No, Mobile_No, Fax_No, Address, Area,Sales_Representative,Credit_Limit, Remaining_Balance, Website, Terms, Mode_of_payment, Type_of_payment, Local_Service, Site_Service, Member, Sales_End_User, Sales_Corporate,ID, Internet_Cafe, ACTIVEFROM Table_Customer_Record cOUTER APPLY (SELECT TOP 1 MODIFIED_DATE,REMARKS_STATUS FROM Table_FOLLOW_UP WHERE REFERENCENUMBER = Table_Customer_Record.ID ORDER BY ID DESC)tORDER BY ID[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Yonkouturko
Yak Posting Veteran
59 Posts |
Posted - 2013-05-17 : 00:52:20
|
@VISAKH16 thanks for your reply..your idea is good but do you have a sample for JOINS? |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-17 : 00:58:40
|
joins approach will require two additional joins to be added to get latest. thats why i prefer APPLYSELECT c.ReferenceNumber,d.MODIFIED_DATE AS LAST_FOLLOW_UP_DATE,d.REMARKS_STATUS AS LAST_FOLLOW_UP_STATUS,Company_Name, Contact_Person,Email_Address, Line_of_Business, Telephone_No, Mobile_No, Fax_No, Address, Area,Sales_Representative,Credit_Limit, Remaining_Balance, Website, Terms, Mode_of_payment, Type_of_payment, Local_Service, Site_Service, Member, Sales_End_User, Sales_Corporate,ID, Internet_Cafe, ACTIVEFROM Table_Customer_Record cLEFT JOIN (SELECT f.REFERENCENUMBER,f.MODIFIED_DATE,f.REMARKS_STATUS FROM Table_FOLLOW_UP f INNER JOIN ( SELECT REFERENCENUMBER,MAX(ID) AS MaxID FROM Table_FOLLOW_UP GROUP BY REFERENCENUMBER )f1 ON f1.REFERENCENUMBER = f.REFERENCENUMBER AND f1.MaxID = f.ID )d ON d.REFERENCENUMBER = c.IDORDER BY c.ID ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-17 : 01:05:50
|
--May be this?SELECT ReferenceNumber, A.LAST_FOLLOW_UP_DATE, A.LAST_FOLLOW_UP_STATUS, Company_Name, Contact_Person, Email_Address, Line_of_Business, Telephone_No, Mobile_No, Fax_No, Address, Area, Sales_Representative, Credit_Limit, Remaining_Balance, Website, Terms, Mode_of_payment, Type_of_payment, Local_Service, Site_Service, Member, Sales_End_User, Sales_Corporate, ID, Internet_Cafe, ACTIVEFROM Table_Customer_Record TCRLEFT JOIN (SELECT REFERENCENUMBER, MODIFIED_DATE, REMARKS_STATUS, ROW_NUMBER() OVER(PARTITION BY REFERENCENUMBER ORDER BY ID DESC) AS RN FROM Table_FOLLOW_UP ) A ON A.REFERENCENUMBER = TCR.ID --Chandu |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-17 : 01:15:33
|
quote: Originally posted by bandi --May be this?SELECT ReferenceNumber, A.LAST_FOLLOW_UP_DATE, A.LAST_FOLLOW_UP_STATUS, Company_Name, Contact_Person, Email_Address, Line_of_Business, Telephone_No, Mobile_No, Fax_No, Address, Area, Sales_Representative, Credit_Limit, Remaining_Balance, Website, Terms, Mode_of_payment, Type_of_payment, Local_Service, Site_Service, Member, Sales_End_User, Sales_Corporate, ID, Internet_Cafe, ACTIVEFROM Table_Customer_Record TCRLEFT JOIN (SELECT REFERENCENUMBER, MODIFIED_DATE, REMARKS_STATUS, ROW_NUMBER() OVER(PARTITION BY REFERENCENUMBER ORDER BY ID DESC) AS RN FROM Table_FOLLOW_UP ) A ON A.REFERENCENUMBER = TCR.IDAND A.RN = 1 --Chandu
this will still return duplicates unless you put additional condition as aboveI didnt suggest this as I'm not sure OP is on SQL 2005 or above seeing the reluctance to use APPLY operator previously------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-05-17 : 01:59:06
|
Hi visakh, I forgot to put that additional condition in ON clause...I think OP can also use FIRST_VALUE() or LAST_VALUE() functions in MSSQL 2012 to get first/last values based on window definitions...--Chandu |
|
|
|
|
|