?
Solved

help with cost query

Posted on 2013-06-10
6
Medium Priority
?
283 Views
Last Modified: 2013-07-31
Hi,

 

I have a table that contains the stores, and the number of an item it sold.

store     apple    orange blueberry           pineapple            peach

s1            0              10           3              6              1

s2            6              8              4              3              4

s3            4              3              20           0              48

s4            9              0              45           35           1

s5            4              3              0              8              4

 

I have another table, that has the cost of each of these items

apple     25

orange  21

blueberry            18

pineapple            10

peach    15

 

I need a query that will provide me the cost per store for each item based on the cost table e.g.

store     apple    orange blueberry           pineapple            peach

s1            0              210         54           60           15

s2            150         168         72           30           60

s3            100         63           360         0              720

s4            225         0              810         350         15

s5            100         63           0              80           60

 

If possible I would also like a "total" column at the end of this query that sums up the cost.

 

Any idea how to go about doing this?  Thanks
0
Comment
Question by:iamnamja
6 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39235159
Can you show us what you've got already?
0
 
LVL 11

Expert Comment

by:netballi
ID: 39235174
Try something like this.

SELECT (apple.Itemtablename*applerate.ratetablename),(orange.Itemtablename*orangerate.ratetablename),(blueberry.Itemtablename*blueberryrate.ratetablename),(pineapple.Itemtablename*pineapplerate.ratetablename),(peach.Itemtablename*peachrate.ratetablename)
FROM iteams
INNER JOIN Rates
ON uniquefiled.Iteams=uniquefiled.Rates
ORDER BY Store.Itemstablename;
0
 

Author Comment

by:iamnamja
ID: 39235205
I really don't know how to go about doing this.  but this is what i did below

SELECT storeName, appleCount * Apple.cost as appleSubtotal, orangeCount * Orange.cost as orangeSubtotal, blueberryCount * Blueberry.cost as blueberrySubtotal
FROM storeData,
(SELECT * FROM costSchedule WHERE item = 'Apple') as Apple,
(SELECT * FROM costSchedule WHERE item = 'Orange') as Orange,
(SELECT * FROM costSchedule WHERE item = 'Blueberry') as Blueberry
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:iamnamja
ID: 39235267
netballi,

In your query do I need additional tables?
0
 
LVL 5

Expert Comment

by:DOSLover
ID: 39235993
I will call the firsttable as itemtable and the second table as costtable. Since there is no relation between the tables, here is what I think a way to do it:
select i.store
	 , i.apple * c1.cost as applecost
	 , i.orange * c2.cost as orangecost
	 , i.blueberry * c3.cost as blueberrycost
	 , i.pinapple * c4.cost as pinapplecost
	 , i.peach * c5.cost as peachcost
	 , (i.apple * c1.cost + i.orange * c2.cost + i.blueberry * c3.cost + i.pinapple * c4.cost + i.peach * c5.cost) as totalcost
  from itemtable i
  JOIN costtable c1 ON c1.item='apple'
  JOIN costtable c2 ON c1.item='orange'
  JOIN costtable c3 ON c1.item='blueberry'
  JOIN costtable c4 ON c1.item='pinapple'
  JOIN costtable c5 ON c1.item='peach'

Open in new window

0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39236626
Freely admit I'm not strong with Access, but understand it doesn't have an 'unpivot'. Here the data is in presented in a 'pivoted' (non-normalized) fashion which as seen above will require multiple joins (one per column in effect).

I'm tempted to propose something like the following, which you could then "cross tab" to pivot for presentation.
select
  store
, item
, qty
, qty * rate
from ((
        select store, apple as qty     , 'apple' as item    where apple > 0
        union all
        select store, orange as qty   , 'orange' as item    where orange > 0
        union all
        select store, blueberry as qty, 'blueberry' as item where blueberry > 0
        union all
        select store, pineapple as qty, 'pineapple' as item where pineapple > 0
        union all
        select store, peach as qty    , 'peach' as item     where peach > 0
     ) s
inner join rates on s.item = rates.item)

Open in new window

nb: the quantity of brackets required is a best guess, access seems to need more than most products.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question