Solved

help with cost query

Posted on 2013-06-10
6
219 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 37

Expert Comment

by:Gerwin Jansen
Comment Utility
Can you show us what you've got already?
0
 
LVL 11

Expert Comment

by:netballi
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:iamnamja
Comment Utility
netballi,

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

Expert Comment

by:DOSLover
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now