Solved

help with cost query

Posted on 2013-06-10
6
271 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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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