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 |
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2009-04-23 : 00:32:48
|
| Dear All,I tried following quearies and output i got below the queary.Now i want both the queary output i want to combine.Queary 1:select I4.DocEntry,MAX(ot.AbsId) as AbsId, oa.Name, sum(I4.TaxSum) as TaxAmountfrom inv4 I4left join ostt ot on ot.AbsId = I4.StaTypeleft join osta oa on I4.StaCode = oa.Codewhere I4.DocEntry = '91'group by oa.Name,I4.DocEntry order by max(ot.absid)Output 1:DocEntry AbsId Name TaxAmount91 7 Excise 30605.94000091 8 ECESS 612.12000091 9 HCESS 306.06000091 13 OCTROI 10655.43000091 14 OCT_SER 106.56000091 15 VAT_4 14091.300000Queary 2:select i2.DocEntry,i2.ExpnsCode As AbsId,ox.ExpnsName As Name,i2.LineTotal As TaxAmount from inv2 i2 left join oexd ox on ox.ExpnsCode = i2.ExpnsCodewhere i2.DocEntry in (select DocEntry from inv4 where DocEntry = '91')Output 2:DocEntry AbsId Name TaxAmount 91 1 Packing Charges 2316.00000091 2 Transportion Charge 3937.000000Now i want following output. Can anybody tell me how to write queary for that.DocEntry AbsId Name TaxAmount91 1 Packing Charges 2316.00000091 2 Transportion Charge 3937.00000091 7 Excise 30605.94000091 8 ECESS 612.12000091 9 HCESS 306.06000091 13 OCTROI 10655.43000091 14 OCT_SER 106.56000091 15 VAT_4 14091.300000 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-23 : 00:43:26
|
| use UNION ALL betwenn these 2 queries,,,,iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-04-23 : 00:47:34
|
| Pal,You are almost there...just combine the output of the two queries by using Union operator.regards,Anil Kumar. |
 |
|
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2009-04-23 : 00:56:05
|
| Dear Freind,I already tried but it gives error. I am getting exactly where this is error.Error:Incorrect syntax near the keyword 'union'.Tried queary:select I4.DocEntry,MAX(ot.AbsId) as AbsId, oa.Name, sum(I4.TaxSum) as TaxAmountfrom inv4 I4left join ostt ot on ot.AbsId = I4.StaTypeleft join osta oa on I4.StaCode = oa.Codewhere I4.DocEntry = '91'group by oa.Name,I4.DocEntry order by max(ot.absid)union allselect i2.DocEntry,i2.ExpnsCode As AbsId,ox.ExpnsName As Name,i2.LineTotal As TaxAmount from inv2 i2 left join oexd ox on ox.ExpnsCode = i2.ExpnsCodewhere i2.DocEntry in (select DocEntry from inv4 where DocEntry = '91') |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-23 : 01:02:24
|
| Just Post some sample data ...iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2009-04-23 : 01:13:16
|
| Try Like this may be...select DocEntry,AbsId,Name,TaxAmount from (select I4.DocEntry,MAX(ot.AbsId) as AbsId, oa.Name, sum(I4.TaxSum) as TaxAmountfrom inv4 I4left join ostt ot on ot.AbsId = I4.StaTypeleft join osta oa on I4.StaCode = oa.Codewhere I4.DocEntry = '91'group by oa.Name,I4.DocEntryunion allselect i2.DocEntry,i2.ExpnsCode As AbsId,ox.ExpnsName As Name,i2.LineTotal As TaxAmount from inv2 i2 left join oexd ox on ox.ExpnsCode = i2.ExpnsCodewhere i2.DocEntry in (select DocEntry from inv4 where DocEntry = '91'))t order by AbsIdiF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2009-04-23 : 01:36:51
|
| Dear Freind,Thnsk for the reply. Queary working fine and i got the following output.91 1 Packing Charges 2316.00000091 2 Transportion Charge 3937.00000091 7 Excise 30605.94000091 8 ECESS 612.12000091 9 HCESS 306.06000091 13 OCTROI 10655.43000091 14 OCT_SER 106.56000091 15 VAT_4 14091.300000Now last step i want show second entry(Transportation Charge) before OCTROI.Can we show the specific entry on specific row.Can u plz tell me how to this ?I want following output,91 1 Packing Charges 2316.00000091 7 Excise 30605.94000091 8 ECESS 612.12000091 9 HCESS 306.06000091 2 Transportion Charge 3937.00000091 13 OCTROI 10655.43000091 14 OCT_SER 106.56000091 15 VAT_4 14091.300000Harish Patil |
 |
|
|
aprichard
Yak Posting Veteran
62 Posts |
Posted - 2009-04-23 : 03:07:45
|
| You can do, we have to control it by case clause,do it like that in order by clause AbsId order by case when AbsId in (1,7,8,9) then 1 else 2 end |
 |
|
|
hspatil31
Posting Yak Master
182 Posts |
Posted - 2009-04-23 : 03:28:14
|
| Dear Freind,I tried ur solution but it gives the wrong output. Means it showing wrong order means missorder. Plz check it. i want only second record (Transportation Charge)before the OCTROI record.Queary :select DocEntry,AbsId,Name,TaxAmount from (select I4.DocEntry,MAX(ot.AbsId) as AbsId, oa.Name, sum(I4.TaxSum) as TaxAmountfrom inv4 I4left join ostt ot on ot.AbsId = I4.StaTypeleft join osta oa on I4.StaCode = oa.Codewhere I4.DocEntry = '91'group by oa.Name,I4.DocEntryunion allselect i2.DocEntry,i2.ExpnsCode As AbsId,ox.ExpnsName As Name,i2.LineTotal As TaxAmount from inv2 i2 left join oexd ox on ox.ExpnsCode = i2.ExpnsCodewhere i2.DocEntry in (select DocEntry from inv4 where DocEntry = '91'))t order by case when AbsId in (1,7,8,9) then 1 else 2 endOutput :91 8 ECESS 612.12000091 7 Excise 30605.94000091 9 HCESS 306.06000091 1 Packing Charges 2316.00000091 2 Transportion Charge 3937.00000091 14 OCT_SER 106.56000091 13 OCTROI 10655.43000091 15 VAT_4 14091.300000Harish Patil |
 |
|
|
|
|
|
|
|