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 |
|
shifis
Posting Yak Master
157 Posts |
Posted - 2003-04-11 : 15:12:26
|
| I have a table that keeps all the inverotory movements, In this table we have fields like, company, location, item, quantity, Transaccion date, and reason code. I need to made a report that bring me in the same line per item the sale and the waste.For example:Cia=2000Location TJU01For 1/01/2002 to 01/31/2002Item Sale Waste Unid --------------------------0010087 30 -5 KG0007834 69 -14 UNIDI can made a query to have the sale or waste in a line but not togethers. For example Reason Code is VTCL if is sale and MERM if is waste.This is the code for waste, if a change the contidion to VTCL, I have the sale. ( I hope you understand my English)SELECT COMPANY, LOCATION, ITEM, COUNT(QUANTITY) AS EXPR1, REASON_CODE, TRAN_UOMFROM dbo.ICTRANSWHERE (COMPANY = @CIA) AND(LOCATION = @LOC) AND (TRANS_DATE BETWEEN @F1 AND @F2) AND (REASON_CODE = 'MERM')GROUP BY COMPANY, LOCATION, ITEM, REASON_CODE, TRAN_UOMORDER BY ITEM DESCEdited by - shifis on 04/11/2003 15:19:48 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2003-04-12 : 12:03:12
|
| I see no reference to "SALE" or to "WASTE" anywhere in your query; more info and sample data would definitely be helpful.However, I *think* I know what you are asking, this may help you out:declare @t table(ID int, Type varchar(5), Amount money)insert into @tselect 1, 'SALE', 100 unionselect 1, 'WASTE', 50 unionselect 2, 'SALE', 150 unionselect 2, 'WASTE', 23 unionselect 3, 'SALE', 45 unionselect 3, 'WASTE', 72select ID, sum(Case when type='SALE' then amount else 0 end) as SaleAmount, sum(Case when type='WASTE' then amount else 0 end) as WasteAmountfrom@tGROUP BY IDyou are essentially doing a very small, fixed column cross-tab. Let me know if this helps at all. It may not apply to you directly and certainly needs some tweaking to fit your needs, but try to learn from it and apply it to your problem.Good luck- Jeff |
 |
|
|
shifis
Posting Yak Master
157 Posts |
Posted - 2003-04-14 : 14:53:14
|
| It works just fine, thanks a lot for your help, this help me a lot.SELECT ITEM,sum(Case when REASON_CODE='VTCL' then QUANTITY else 0 end) as SaleAmount, sum(Case when REASON_CODE='MERM' then QUANTITY else 0 end) as WasteAmount FROM dbo.ICTRANSWHERE (COMPANY = 2000) AND (LOCATION = 'TJU01') AND (TRANS_DATE BETWEEN CONVERT(DATETIME, '2002-01-01 00:00:00', 102) AND CONVERT(DATETIME, '2002-01-31 00:00:00', 102)) AND (REASON_CODE = 'MERM' OR REASON_CODE = 'VTCL')GROUP BY ITEMORDER BY ITEM |
 |
|
|
|
|
|
|
|