Author |
Topic |
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-19 : 09:54:07
|
I have written threee scriptI need to combine the results Script 1:accounts that were given quotes between jan 1 and dec 1 but have no orderDECLARE @StartDate DATETIMEDECLARE @EndDate DATETIMESET @StartDate = '01/01/07'SET @EndDate = '12/01/07'SELECT DISTINCT T.ID, T.AccountID, Q.LastDateFROM Table1 TINNER JOIN table2 E ON E.TID = T.TIDINNER JOIN Table3 X ON X.TID = T.TIDINNER JOIN ( SELECT TID, MAX(Date) AS LastDate FROM table4 WHERE Type = 'ADD' AND Date BETWEEN @StartDate AND @EndDate GROUP BY TID ) Q ON Q.TID = T.TIDWHERE E.order < 0 ORDER BY T.TIDScript 2: accounts with order but no quotesSELECT DISTINCT T.TID, T.AccountIDFROM Table1 TINNER JOIN table2 E ON E.TID = T.TrIDLEFT JOIN table3 Q ON Q.TID = T.TIDWHERE Q.FID IS NULL AND E.order >= 0Script 3:-- accounts that were given quotes prior to dec 1 but haven't processed yetDECLARE @StartDate DATETIMEDECLARE @EndDate DATETIMESET @StartDate = '01/01/07'SET @EndDate = '12/01/07'SELECT DISTINCT T.ID, T.AccountID, Q.LastDateFROM Table1 TINNER JOIN Table3 X ON X.TID = T.TIDINNER JOIN ( SELECT TID, MAX(Date) AS LastDate FROM Table4 WHERE Type = 'ADD' AND Date BETWEEN @StartDate AND @EndDate GROUP BY TID ) Q ON Q.TID = T.TIDLEFT JOIN ( SELECT DISTINCT AccountID FROM Table5 WHERE Date BETWEEN '01/01/07' AND '12/01/07' ) A ON A.ID = T.AccountIDWHERE A.AccountID IS NULLI have to combine these three scripts and retieve one result setIs it possible |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-12-19 : 10:03:58
|
In what way you want to combine them? If you want to merge them, the resultset of all scripts is not same.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-19 : 10:17:37
|
Try this:-SELECT DISTINCTT.ID, T.AccountID, Q.LastDateFROM Table1 TINNER JOIN table2 E ON E.TID = T.TIDINNER JOIN Table3 X ON X.TID = T.TIDINNER JOIN (SELECT TID, MAX(Date) AS LastDate FROM table4 WHERE Type = 'ADD' AND Date BETWEEN @StartDate AND @EndDateGROUP BY TID) Q ON Q.TID = T.TIDWHEREE.order < 0 ORDER BY T.TIDUNION ALLSELECT DISTINCTT.TID, T.AccountID,NULLFROMTable1 TINNER JOIN table2 E ON E.TID = T.TrIDLEFT JOIN table3 Q ON Q.TID = T.TIDWHEREQ.FID IS NULL ANDE.order >= 0UNION ALLSELECT DISTINCTT.ID, T.AccountID, Q.LastDateFROMTable1 TINNER JOIN Table3 X ON X.TID = T.TIDINNER JOIN (SELECT TID, MAX(Date) AS LastDate FROM Table4 WHERE Type = 'ADD' AND Date BETWEEN @StartDate AND @EndDateGROUP BY TID) Q ON Q.TID = T.TIDLEFT JOIN(SELECT DISTINCT AccountID FROM Table5 WHERE Date BETWEEN '01/01/07' AND '12/01/07') A ON A.ID = T.AccountIDWHEREA.AccountID IS NULL |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-19 : 10:31:35
|
Script 1UNION ALLScript 2UNION ALLScript 3Please notice that there are 3 columns in first and last script and only 2 columns in Script 2.There you have to add ", NULL" to the select list. E 12°55'05.25"N 56°04'39.16" |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 09:10:15
|
Thanks for the response.But as im declaring the Start date and end date on script 1 and script 3 , im getting error.Any idea, plz help me. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-20 : 09:14:32
|
declare it only once above the three scriptsDECLARE...Script1UNION ALLScript2UNION ALLScript3 |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 09:23:42
|
but im getting error at the end of the first scriptServer:Msg156,level 15, State 1, Line22Incorrect sytax near the keyword 'UNION' |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-20 : 09:31:47
|
Remove Order by clause from first script. You cant directly uion all the ordered results. Do order by at the end of the last scriptMadhivananFailing to plan is Planning to fail |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-20 : 09:33:52
|
Take the ORDER BY out of script1. If you want to use ORDER By use ORDER BY as followsSELECT * FROM( Script1UNION ALLScript2UNION ALLScript3) tORDER BY t.TID |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 10:01:04
|
Thanks for the immediate response |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 10:08:46
|
[code]DECLARE @StartDate DATETIME, @EndDate DATETIMESELECT @StartDate = '01/01/07', @EndDate = '12/01/07'SELECT DISTINCT ID, AccountID, LastDateFROM ( SELECT T.ID, T.AccountID, Q.LastDate FROM Table1 AS T INNER JOIN Table2 AS E ON E.TID = T.TID INNER JOIN Table3 AS X ON X.TID = T.TID INNER JOIN ( SELECT TID, MAX(Date) AS LastDate FROM Table4 WHERE Type = 'ADD' AND Date >= @StartDate AND Date < @EndDate GROUP BY TID ) AS Q ON Q.TID = T.TID WHERE E.Order < 0 UNION ALL SELECT T.TID, T.AccountID, NULL FROM Table1 AS T INNER JOIN Table2 AS E ON E.TID = T.TrID LEFT JOIN Table3 AS Q ON Q.TID = T.TID WHERE Q.FID IS NULL AND E.Order >= 0 UNION ALL SELECT T.ID, T.AccountID, Q.LastDate FROM Table1 AS T INNER JOIN Table3 AS X ON X.TID = T.TID INNER JOIN ( SELECT TID, MAX(Date) AS LastDate FROM Table4 WHERE Type = 'ADD' AND Date >= @StartDate AND Date < @EndDate GROUP BY TID ) AS Q ON Q.TID = T.TID LEFT JOIN ( SELECT AccountID FROM Table5 WHERE Date >= @StartDate AND Date < @EndDate ) AS A ON A.ID = T.AccountID WHERE A.AccountID IS NULL ) AS dORDER BY TID[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 10:18:10
|
Is it possible to get the results of three scripts in same table but each thing in different columnsC1-TID,C2-ACCOUNITC3-dateC4-ACCOUNT with E.order < 0 C5-Account E.order >= 0C6= Account haven't processed Are the script needs to be changed? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 10:22:13
|
Yes.C4-C6, are they COUNT, SUM, MIN, MAX or other aggregation? E 12°55'05.25"N 56°04'39.16" |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-20 : 10:41:24
|
sorry,C1-TID,C2-dateC3-ACCOUNTID whose E.order < 0 C4-AccountID whose E.order >= 0C5= AccountID who haven't processed C3-C5 ----ID |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 10:55:45
|
Number (count) of ID's?Or just a list of ID's? E 12°55'05.25"N 56°04'39.16" |
 |
|
sqldbaa
Starting Member
32 Posts |
Posted - 2007-12-26 : 07:49:21
|
listing the Account ID's |
 |
|
|