So I have been handed a database that I need to do some reporting out of to find cost per encounter. It basically looks like this:
ID ItmQty1 ItmQty2 ItmQty3 etc...
1 3 1 0
I need to get a total cost by multiplying that by a prices table like so:
ItmCost1 ItmCost2 ItmCost3 etc...
21.50 10.75 3.53
and would like to end with a result like:
ID ItmQty1 ItmTTL1 ItmQty2 ItmTTL2 ... EncounterTTL
1 3 64.50 1 10.75 75.25
Just not sure how a join would work with the many to one relationship between the tables.
This will be done on SQL server 2008 and SSRS.
(joinedColA + joinedColB) as total
(then rollup the totals later which is still an awkward issue, but an easier and more common one). The above is a very bad way to have an SQL schema defined and should only be the output for humans (e.g. a rollup report at the end). The problem here is the multiplicity is across columns (no!) instead of rows (yes!) - NoName 2012-04-05 18:22
Well, your schema is somewhat unorthodox (to say the least), but it's doable:
SELECT
i.ID
, i.ItmQty1
, i.ItmQty1 * c.ItmCost1 as ItmTTL1
, i.ItmQty2
, i.ItmQty2 * c.ItmCost2 as ItmTTL2
, i.ItmQty3
, i.ItmQty3 * c.ItmCost3 as ItmTTL3
-- and so on
FROM Items i
JOIN Costs c on 1=1
itmcost1
throughitemcostn
? And, if so, what isn
- Sorpigal 2012-04-05 18:17