Click here to Skip to main content
15,894,343 members
Home / Discussions / Database
   

Database

 
GeneralRe: Sorry in advanced for this brain dead question Pin
Mike65432128-Feb-10 13:48
Mike65432128-Feb-10 13:48 
AnswerRe: Sorry in advanced for this brain dead question Pin
Jörgen Andersson1-Mar-10 9:51
professionalJörgen Andersson1-Mar-10 9:51 
QuestionSUM question [solved] Pin
danyDude27-Feb-10 19:07
danyDude27-Feb-10 19:07 
AnswerRe: SUM question Pin
Roger Wright27-Feb-10 19:36
professionalRoger Wright27-Feb-10 19:36 
GeneralRe: SUM question Pin
danyDude27-Feb-10 20:14
danyDude27-Feb-10 20:14 
AnswerRe: SUM question [solved] Pin
Mycroft Holmes27-Feb-10 23:35
professionalMycroft Holmes27-Feb-10 23:35 
JokeRe: SUM question [solved] Pin
Dan Mos28-Feb-10 4:20
Dan Mos28-Feb-10 4:20 
QuestionHow to write this query? Pin
Andy_L_J27-Feb-10 13:48
Andy_L_J27-Feb-10 13:48 
I have the following Table Structure in a SQL 2008 database:

ProductCategory
ProdCat_ID  Int PK
Name VarChar(30)

Product
Product_ID Int PK
ProdCat_ID Int FK
...

WorkOrder
WO_ID Int PK
Product_ID Int FK
...

Production
Prodn_ID Int PK
WO_ID Int FK
...

MaterialTransaction
MatTrans_ID Int PK,
TransDate Date,
Prodn_ID FK,
Qty Decimal(18,7)
...



I want to retrieve Qty data for specified Category Names on each date in the query.

If I do the following:

SELECT Distinct(mt.TransDate) As [Date],
         CASE WHEN pc.Name IN('Hadware','Custom')
              THEN SUM(mt.Qty)
              ELSE 0 END AS 'Hardware',
         CASE WHEN pc.Name LIKE 'DS %'
              THEN SUM(mt.Qty)
              ELSE 0 End As 'DataStrip'
              
  From MaterialTransaction mt
    JOIN Production pr
      ON mt.Prodn_ID = pr.Prodn_ID
    JOIN WorkOrder wo
      ON pr.WO_ID = wo.WO_ID
    JOIN Product p
      ON wo.Product_ID = p.ID
    JOIN ProductCategory pc
      ON p.ProdCat_ID = pc.ID
    WHERE Date Between '11/24/2009' And '11/25/2009'

  GROUP BY mt.TransDate, pc.Name


I end up with results similar to:

OUTPUT:

Date                Hardware        DataStrip
2009-11-24 00:00:00 0.0000000000    560.4080000
2009-11-24 00:00:00 2786.3100000    0.0000000
2009-11-25 00:00:00 0.0000000       125.5415000


What I require however is:

Date                Hardware        DataStrip
2009-11-24 00:00:00 2786.310000000  560.4080000
2009-11-25 00:00:00 0.0000000       125.4150000


Any pointers are most appreciated.
I don't speak Idiot - please talk slowly and clearly

'This space for rent'

Driven to the arms of Heineken by the wife

AnswerSolved, but... [modified] Pin
Andy_L_J27-Feb-10 14:55
Andy_L_J27-Feb-10 14:55 
GeneralRe: Solved, but... Pin
Luc Pattyn27-Feb-10 15:01
sitebuilderLuc Pattyn27-Feb-10 15:01 
GeneralRe: Solved, but... Pin
Mycroft Holmes27-Feb-10 17:16
professionalMycroft Holmes27-Feb-10 17:16 
GeneralRe: Solved, but... Pin
Mycroft Holmes27-Feb-10 17:22
professionalMycroft Holmes27-Feb-10 17:22 
GeneralRe: Solved, but... Pin
Andy_L_J27-Feb-10 17:41
Andy_L_J27-Feb-10 17:41 
AnswerRe: How to write this query? Pin
i.j.russell27-Feb-10 23:41
i.j.russell27-Feb-10 23:41 
GeneralRe: How to write this query? Pin
Andy_L_J28-Feb-10 0:15
Andy_L_J28-Feb-10 0:15 
QuestionAdvice for product manager Pin
treefirmy27-Feb-10 12:34
treefirmy27-Feb-10 12:34 
AnswerRe: Advice for product manager Pin
Mycroft Holmes27-Feb-10 17:27
professionalMycroft Holmes27-Feb-10 17:27 
AnswerRe: Advice for product manager Pin
i.j.russell27-Feb-10 23:48
i.j.russell27-Feb-10 23:48 
GeneralRe: Advice for product manager Pin
treefirmy28-Feb-10 4:14
treefirmy28-Feb-10 4:14 
GeneralRe: Advice for product manager Pin
i.j.russell28-Feb-10 5:33
i.j.russell28-Feb-10 5:33 
GeneralRe: Advice for product manager Pin
treefirmy28-Feb-10 5:40
treefirmy28-Feb-10 5:40 
GeneralRe: Advice for product manager Pin
i.j.russell28-Feb-10 5:58
i.j.russell28-Feb-10 5:58 
GeneralRe: Advice for product manager Pin
treefirmy28-Feb-10 6:13
treefirmy28-Feb-10 6:13 
GeneralRe: Advice for product manager Pin
i.j.russell28-Feb-10 7:08
i.j.russell28-Feb-10 7:08 
GeneralRe: Advice for product manager Pin
Mycroft Holmes28-Feb-10 10:58
professionalMycroft Holmes28-Feb-10 10:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.