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

Database

 
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 
AnswerSolved, but... [modified] Pin
Andy_L_J27-Feb-10 14:55
Andy_L_J27-Feb-10 14:55 
I have used the following correlated query:

SELECT mt.TransDate, CASE ISNULL(h.Hardware,0)
                      WHEN 0 Then 0
                      ELSE h.Hardware
                      End As Hardware, 
                     CASE ISNULL(d.Datastrip, 0)
                      WHEN 0 Then 0
                      ELSE d.Datastrip
                      END AS Datastrip
  FROM MaterialTransaction mt
    LEFT JOIN (SELECT mtt.TransDate,SUM(mtt.Qty) As Hardware
            FROM MaterialTransaction mtt
              JOIN Production prr
                ON mtt.Prodn_ID = prr.Prodn_ID
              JOIN WorkOrder woo
                ON prr.WO_ID = woo.WO_ID
              JOIN Product pp
                ON woo.Product_ID = pp.ID
              JOIN ProductCategory pcc
                ON pp.ProdCat_ID = pcc.ID
            WHERE pcc.Name In ('Hardware','Custom', 'Cellular')    
            GROUP BY mtt.TransDate) h
      ON mt.TransDate = h.TransDate
    LEFT JOIN (SELECT mtt.TransDate, SUM(mtt.Qty) As Datastrip
            FROM MaterialTransaction mtt
              JOIN Production prr
                ON mtt.Prodn_ID = prr.Prodn_ID
              JOIN WorkOrder woo
                ON prr.WO_ID = woo.WO_ID
              JOIN Product pp
                ON woo.Product_ID = pp.ID
              JOIN ProductCategory pcc
                ON pp.ProdCat_ID = pcc.ID
            WHERE pcc.Name In ('DS PVC','DS PETG')    
            GROUP BY mtt.TransDate) d
          ON h.TransDate = d.TransDate
    WHERE mt.TransDate BETWEEN '11/24/2009' And '11/25/2009'
    GROUP BY mt.TransDate, h.Hardware, d.Datastrip  


It works as required, but boy is it ugly. Can this be optimized further?

<edit> The final query (with 8 sub-queries) was running rather slowly - ~8 secs to retreive 30 rows from around 4000. An nonclustered index on MaterialTransaction.TransDate sped this up to ~1 sec <edit>
I don't speak Idiot - please talk slowly and clearly

'This space for rent'

Driven to the arms of Heineken by the wife
modified on Saturday, February 27, 2010 11:33 PM

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 
GeneralRe: Advice for product manager Pin
jgrogan17-Dec-10 3:34
jgrogan17-Dec-10 3:34 

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.