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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL View - Division Problem

Author  Topic 

btamulis
Yak Posting Veteran

64 Posts

Posted - 2007-10-05 : 06:57:25
I have a view that I am works fine -

ALTER view [dbo].[_tec_SOP_POP_link] as
select SOP60100.SOPNUMBE as SOP_ORDER_NUMBER,
SOP60100.QTYRECVD as QtyReceivedSoFar,
SOP30200.sopnumbe as SOP_INV_NUMBER,
SOP60100.PONUMBER, SOP60100.QTYONPO as POQTY_REMAININGQTY,
POP10110.ITEMNMBR as ITEM_NUMBER,
POP10110.ITEMDESC as itemdescription, POP30310.UNITCOST as ReceivedCost,
Sum (case when pop30310.ponumber = sop60100.ponumber AND
pop30310.trxsorce like 'POIVC%' then pop30310.umqtyinb else 0 end) as TOTALINVOICEDMATCHED,
Sum (case when pop30310.ponumber = sop60100.ponumber AND
pop30310.trxsorce like 'POIVC%' then pop30310.extdcost else 0 end) as TOTALDOLLARSINVOICEDMATCHED,
from sop60100 inner join sop30200 on sop60100.sopnumbe = sop30200.ORIGNUMB AND sop30200.soptype = 3
inner join POP10110 on SOP60100.PONUMBER = POP10110.PONUMBER
inner join pop30310 on sop60100.ponumber = pop30310.ponumber
where SOP60100.ORD = POP10110.ORD
GROUP BY SOP60100.SOPNUMBE, SOP60100.QTYRECVD, SOP30200.sopnumbe, SOP60100.PONUMBER, SOP60100.QTYONPO,
POP10110.ITEMNMBR, POP10110.ITEMDESC, POP30310.UNITCOST, pop30310.umqtyinb


I want to add a column (UNITCOSTINVOICEDMATCHED)that divides TOTALDOLLARSINVOICEDMATCHED/TOTALINVOICEDMATCHED

I inserted the following (which did not work)-

Sum (TOTALDOLLARSINVOICEDMATCHED/TOTALINVOICEDMATCHED) as UNITCOSTINVOICEDMATCHED

error said - TOTALDOLLARSINVOICEDMATCHED column not recognized

The I inserted this statement - thinking I needed another case statement -

Sum(case when pop30310.ponumber = sop60100.ponumber AND
pop30310.trxsorce like 'POIVC%' then sum(pop30310.extdcost)/ sum(pop30310.umqtyinb)
else 0 end)as UNITCOSTINVOICEDMATCH

this returned the error -

Msg 130, Level 15, State 1, Procedure _tec_SOP_POP_link, Line 2
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.

Any thoughts?

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-05 : 07:09:53
[code]ALTER view [dbo].[_tec_SOP_POP_link] as
Select
SOP_ORDER_NUMBER,
QtyReceivedSoFar,
SOP_INV_NUMBER,
PONUMBER, POQTY_REMAININGQTY,
ITEM_NUMBER,
itemdescription, ReceivedCost,
TOTALINVOICEDMATCHED,
TOTALDOLLARSINVOICEDMATCHED,
TOTALDOLLARSINVOICEDMATCHED/ (Case TOTALINVOICEDMATCHED When 0 then 1 else TOTALINVOICEDMATCHED End) as UNITCOSTINVOICEDMATCHED
From
(
select
SOP60100.SOPNUMBE as SOP_ORDER_NUMBER,
SOP60100.QTYRECVD as QtyReceivedSoFar,
SOP30200.sopnumbe as SOP_INV_NUMBER,
SOP60100.PONUMBER, SOP60100.QTYONPO as POQTY_REMAININGQTY,
POP10110.ITEMNMBR as ITEM_NUMBER,
POP10110.ITEMDESC as itemdescription, POP30310.UNITCOST as ReceivedCost,
Sum (case when pop30310.ponumber = sop60100.ponumber AND pop30310.trxsorce like 'POIVC%' then pop30310.umqtyinb else 0 end) as TOTALINVOICEDMATCHED,
Sum (case when pop30310.ponumber = sop60100.ponumber AND pop30310.trxsorce like 'POIVC%' then pop30310.extdcost else 0 end) as TOTALDOLLARSINVOICEDMATCHED,
from
sop60100 inner join sop30200 on sop60100.sopnumbe = sop30200.ORIGNUMB AND sop30200.soptype = 3
inner join POP10110 on SOP60100.PONUMBER = POP10110.PONUMBER
inner join pop30310 on sop60100.ponumber = pop30310.ponumber
where
SOP60100.ORD = POP10110.ORD
GROUP BY
SOP60100.SOPNUMBE, SOP60100.QTYRECVD, SOP30200.sopnumbe, SOP60100.PONUMBER, SOP60100.QTYONPO,
POP10110.ITEMNMBR, POP10110.ITEMDESC, POP30310.UNITCOST, pop30310.umqtyinb
) t[/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

btamulis
Yak Posting Veteran

64 Posts

Posted - 2007-10-05 : 07:26:19
Thank you - it worked! (I had to remove the comma before the second from).

I have question though - what role/purpose of the lower case t at end on statement? I was curious - so I removed it and I got an error message - If i leave it it works fine.

Sorry to be so less knowledgeable - I do not work with transact sql enough to master it - my role is primarily teaching software application and not data reporting, etc....

I am totally impressed with the knowledge in this forum.
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-10-05 : 07:49:00
It is called alias. This is how you can refer to the columns in the derived table in outer query.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page
   

- Advertisement -