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 |
hectoreduardo
Starting Member
1 Post |
Posted - 2013-07-01 : 14:26:22
|
Hello everybody, hope someone can help me with this. The soonest reply is very appreciated, but I know we all have lot to do. Thanks!!I have written the following query:select itd.ItemID, ito.voucher, isd.COSTAMOUNTSETTLED, ITO.COSTAMOUNTADJUSTMENT, isr.OperationsAccount, ito.DATEPHYSICAL,isr.Dimension, isr.Dimension2_,isr.Dimension3_, ITd.CustVendAc, isnull(VT.Name,'SinProveedor') as Namefrom inventtrans itdleft outer join INVENTTABLE it on itd.DATAAREAID = it.DATAAREAID and itd.ITEMID = it.ITEMIDleft outer join inventsettlement isd on itd.dataareaid = isd.dataareaid and itd.inventtransid = isd.inventtransidleft outer join inventsettlement isr on isd.dataareaid = isr.dataareaid and isd.settletransid = isr.settletransidleft outer join inventtrans ito on isr.dataareaid = ito.dataareaid and isr.inventtransid = ito.inventtransidleft outer join VENDTABLE vt on ito.dataareaid = vt.dataareaid and itd.CUSTVENDAC = vt.ACCOUNTNUMwhere itd.dataareaid='CAD'and itd.statusissue=0and it.ITEMGROUPID not In ('AZUCAR_TUR','MELADURA','MELAZA','CAÑA','PROD_TER M','ACG','ATG')and isd.CANCELLED=0and ito.DATEPHYSICAL between '01/05/2012' and '30/04/2013'and ito.statusissue=1AND itd.transrefid not in (select pt.purchid from purchtable pt where PT.dataareaid=itd.dataareaid andpt.ISS_CAJACHICA=1)and isr.OPERATIONSACCOUNT like '710%' and ito.inventtransid='00259176_078'order by isr.OperationsAccount, ito.voucher, itd.itemidAnd I receive the following results:Record#1ItemId: F526-244Voucher: DESPF_009278CostAmountSettled: 14.75CostAmountAdjusted: -1.77OperationsAccount: 710210DatePhysical: 2012-07-18Dimension: 60Dimension2_: 600004Dimension3_: 000CustVendAc: PINJ01Name: Inversiones Joel y Johan, SARecord#2ItemId: F526-244Voucher: DESPF_009278CostAmountSettled: 15.95CostAmountAdjusted: -1.77OperationsAccount: 710210DatePhysical: 2012-07-18Dimension: 60Dimension2_: 600004Dimension3_: 000CustVendAc: PINJ01Name: Inversiones Joel y Johan, SAI want to have only one record, with the TOTAL of CostAmountSettled and the unique value (MIN,MAX,AVG) of CostAmountAdjusted. Like this:Record#I wantItemId: F526-244Voucher: DESPF_009278CostAmountSettled: 30.7 (14.75+15.95)CostAmountAdjusted: -1.77 (this value)OperationsAccount: 710210DatePhysical: 2012-07-18Dimension: 60Dimension2_: 600004Dimension3_: 000CustVendAc: PINJ01Name: Inversiones Joel y Johan, SAThis is because I need to have the result of (CostAmountSettledRec#1 + CostAmountSettledRec#2) - CostAmountAdjusted in one field. How should I write the select distinct statement to obtain the result I want???Héctor |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-01 : 14:39:55
|
[code]select itd.ItemID, ito.voucher, SUM(isd.COSTAMOUNTSETTLED), MAX(ITO.COSTAMOUNTADJUSTMENT), isr.OperationsAccount, ito.DATEPHYSICAL,isr.Dimension, isr.Dimension2_,isr.Dimension3_, ITd.CustVendAc, isnull(VT.Name,'SinProveedor') as Namefrom inventtrans itdleft outer join INVENTTABLE it on itd.DATAAREAID = it.DATAAREAID and itd.ITEMID = it.ITEMIDleft outer join inventsettlement isd on itd.dataareaid = isd.dataareaid and itd.inventtransid = isd.inventtransidleft outer join inventsettlement isr on isd.dataareaid = isr.dataareaid and isd.settletransid = isr.settletransidleft outer join inventtrans ito on isr.dataareaid = ito.dataareaid and isr.inventtransid = ito.inventtransidleft outer join VENDTABLE vt on ito.dataareaid = vt.dataareaid and itd.CUSTVENDAC = vt.ACCOUNTNUMwhere itd.dataareaid='CAD'and itd.statusissue=0and it.ITEMGROUPID not In ('AZUCAR_TUR','MELADURA','MELAZA','CAÑA','PROD_TER M','ACG','ATG')and isd.CANCELLED=0and ito.DATEPHYSICAL between '01/05/2012' and '30/04/2013'and ito.statusissue=1AND itd.transrefid not in (select pt.purchid from purchtable pt where PT.dataareaid=itd.dataareaid andpt.ISS_CAJACHICA=1)and isr.OPERATIONSACCOUNT like '710%' and ito.inventtransid='00259176_078'group by itd.ItemID, ito.voucher, isr.OperationsAccount, ito.DATEPHYSICAL,isr.Dimension, isr.Dimension2_,isr.Dimension3_, ITd.CustVendAc, isnull(VT.Name,'SinProveedor')order by isr.OperationsAccount, ito.voucher, itd.itemid[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|