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 |
|
hirani_prashant
Yak Posting Veteran
93 Posts |
Posted - 2008-07-07 : 07:57:13
|
| Hello.Mine Query A is returning me...ID Amt2106 6252140 6252123 6252108 625Mine Query B is returning me...ID Amt2103 6252111 6252123 6252108 625When i am doing A UNION B then it will returning me...ID Amt2103 625.002106 625.002108 625.002111 625.002123 625.002140 625.00---------------------------------------------------------------------I need output like this...ID Amt2106 625.002140 625.002123 625.002108 625.002103 625.002111 625.00Can any one help me??? |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-07 : 08:00:39
|
| not sure i really understand your problem... is it just the order you have an issue with?Em |
 |
|
|
hirani_prashant
Yak Posting Veteran
93 Posts |
Posted - 2008-07-07 : 08:02:01
|
| Yes.... |
 |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-07-07 : 08:05:15
|
| how about something like...select ID,Amtfrom (select ID,Amt,1 as tab_ordfrom TableAUNIONselect ID,Amt,2from TableB) table_unionorder by tab_ordEm |
 |
|
|
hirani_prashant
Yak Posting Veteran
93 Posts |
Posted - 2008-07-07 : 08:14:39
|
| Mine Query is ... SELECT Row_Number() OVER (ORDER BY Deal, BrokerID) Line_No, BrokerID, Brokerage FROM ( SELECT Deal, 'BrokerID' = CASE WHEN y.ID = 1 THEN CP1B1 ELSE CASE WHEN y.ID = 2 THEN CP1B2 ELSE CASE WHEN y.ID = 3 THEN CP1B3 ELSE CP1B4 END END END, 'Brokerage' = CASE WHEN y.ID =1 THEN CP1A1 ELSE CASE WHEN y.ID = 2 THEN CP1A2 ELSE CASE WHEN y.ID = 3 THEN CP1A3 ELSE CP1A4 END END END FROM DBO.TRADE X, ( SELECT 1 ID UNION ALL SELECT 2 ID UNION ALL SELECT 3 ID UNION ALL SELECT 4 ID ) Y WHERE Month(X.MODIFY_DATE)<= Month(@P_Start_Date) and Month(X.MODIFY_DATE)>= Month(@P_END_Date) and CONVERT(SMALLDATETIME, CONVERT(NVARCHAR, X.MODIFY_DATE,101)) between @P_Start_Date and @P_END_Date and X.Version = ( SELECT MaX(Version) FROM DBO.Trade where Deal = X.Deal and Month(MODIFY_DATE)<=Month(@P_Start_Date) and Month(MODIFY_DATE)>=Month(@P_END_Date) ) AND X.ACTION_TYPE != 'D' AND Deal = @Deal -- ================================================================================================================= UNION --ALL -- ================================================================================================================= SELECT Deal, 'BrokerID' = CASE WHEN y.ID = 1 THEN CP1B1 ELSE CASE WHEN y.ID = 2 THEN CP1B2 ELSE CASE WHEN y.ID = 3 THEN CP1B3 ELSE CP1B4 END END END, 'Brokerage' = CASE WHEN y.ID =1 THEN CP1A1 ELSE CASE WHEN y.ID = 2 THEN CP1A2 ELSE CASE WHEN y.ID = 3 THEN CP1A3 ELSE CP1A4 END END END FROM DBO.TRADE X, ( SELECT 1 ID UNION ALL SELECT 2 ID UNION ALL SELECT 3 ID UNION ALL SELECT 4 ID ) Y WHERE Month(X.MODIFY_DATE)>=DatePart(Month,(@P_Start_Date))-1 and Month(X.MODIFY_DATE)<=DatePart(Month,(@P_END_Date))-1 and X.Version = ( SELECT MAX(Version) FROM DBO.Trade WHERE Deal= X.Deal and Month(X.MODIFY_DATE)<= DatePart(Month, @P_Start_Date)-1 and Month(X.MODIFY_DATE)>= DatePart(Month, @P_END_Date)-1 ) AND X.ACTION_TYPE != 'D' AND Deal = @Deal ) X--------------------------------------------------------------------Now can you please help me??? |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-07-07 : 08:22:50
|
| I guess by using Union all output will be what you are expecting but you will get duplicate records which you have to eliminate. |
 |
|
|
hirani_prashant
Yak Posting Veteran
93 Posts |
Posted - 2008-07-07 : 08:25:00
|
| i am using UNION Only..And i am not getting output the way i want it... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-07 : 13:34:56
|
| [code]SELECT Row_Number() OVER (ORDER BY Deal, BrokerID) Line_No,BrokerID, BrokerageFROM (SELECT Deal,'BrokerID' = CASE WHEN y.ID = 1 THEN CP1B1ELSE CASE WHEN y.ID = 2 THEN CP1B2ELSE CASE WHEN y.ID = 3 THEN CP1B3ELSE CP1B4ENDEND END,'Brokerage' = CASE WHEN y.ID =1 THEN CP1A1 ELSE CASE WHEN y.ID = 2 THEN CP1A2ELSE CASE WHEN y.ID = 3 THEN CP1A3ELSE CP1A4ENDENDEND,1 AS OrdFROM DBO.TRADE X,(SELECT 1 ID UNION ALLSELECT 2 ID UNION ALLSELECT 3 ID UNION ALLSELECT 4 ID) YWHERE Month(X.MODIFY_DATE)<= Month(@P_Start_Date) and Month(X.MODIFY_DATE)>= Month(@P_END_Date) and CONVERT(SMALLDATETIME, CONVERT(NVARCHAR, X.MODIFY_DATE,101)) between @P_Start_Date and @P_END_Date and X.Version = ( SELECT MaX(Version) FROM DBO.Trade where Deal = X.Deal and Month(MODIFY_DATE)<=Month(@P_Start_Date) and Month(MODIFY_DATE)>=Month(@P_END_Date)) AND X.ACTION_TYPE != 'D' ANDDeal = @Deal-- =================================================================================================================UNION --ALL -- ================================================================================================================= SELECT Deal,'BrokerID' = CASE WHEN y.ID = 1 THEN CP1B1ELSE CASE WHEN y.ID = 2 THEN CP1B2ELSE CASE WHEN y.ID = 3 THEN CP1B3ELSE CP1B4ENDEND END,'Brokerage' = CASE WHEN y.ID =1 THEN CP1A1 ELSE CASE WHEN y.ID = 2 THEN CP1A2ELSE CASE WHEN y.ID = 3 THEN CP1A3ELSE CP1A4ENDENDEND,2 AS OrdFROM DBO.TRADE X,(SELECT 1 ID UNION ALLSELECT 2 ID UNION ALLSELECT 3 ID UNION ALLSELECT 4 ID) YWHERE Month(X.MODIFY_DATE)>=DatePart(Month,(@P_Start_Date))-1 and Month(X.MODIFY_DATE)<=DatePart(Month,(@P_END_Date))-1 and X.Version = ( SELECT MAX(Version) FROM DBO.Trade WHERE Deal= X.Deal and Month(X.MODIFY_DATE)<= DatePart(Month, @P_Start_Date)-1 and Month(X.MODIFY_DATE)>= DatePart(Month, @P_END_Date)-1 ) AND X.ACTION_TYPE != 'D' ANDDeal = @Deal) XORDER BY X.Ord[/code] |
 |
|
|
hirani_prashant
Yak Posting Veteran
93 Posts |
Posted - 2008-07-08 : 01:55:27
|
| Thanks visakh16.Still i am facing same problem. I think by just doing ordering, it will not solve the problem. I want to maintain order while doing (A) U (B)Ex..A = {1,2,5}B = {2,4,6}then I need Answer (A) U (B) = {1,2,5,4,6}-- RegardsPrashant Hirani |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-08 : 05:45:14
|
quote: Originally posted by hirani_prashant Thanks visakh16.Still i am facing same problem. I think by just doing ordering, it will not solve the problem. I want to maintain order while doing (A) U (B)Ex..A = {1,2,5}B = {2,4,6}then I need Answer (A) U (B) = {1,2,5,4,6}-- RegardsPrashant Hirani
Can you try dumping result onto a temporary table with an id column and then retrieving from it order by id and ord? |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-07-08 : 23:35:11
|
| If I understand your problem correctly, I think you can do this by also outer joining your second table to your first and only showing those that have have not already occurred in the first.select * from( select 1 as ord, * from A union all select 2 as ord, * from B left outer join A on (...) where A.PK is null ) Xorder by ord,...You might find CTEs useful here as you need to use A twice. |
 |
|
|
|
|
|
|
|