Solved

help with cost query

Posted on 2013-06-10
6
251 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 48

Accepted Solution

by:
PortletPaul earned 500 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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 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