?
Solved

help with cost query

Posted on 2013-06-10
6
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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