MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2012, 06:26 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
SQL Query help

Hi, all. Hope you can assist with another of my confusing SQL statements.

I have a list of questions for a particular package. For package there are 2 (possible more in future) processes, and for each process I need to mark whether or not that quality assurance check has been performed. So my front end looks like this:

PackageID = 53

QA Check.............Process1...............Process2
Check 1...............[checkbox]............[checkbox]
Check2................[checkbox]............[checkbox]


At the back-end, I have 4 tables:

Process - list of processes for each package

ProcessID........Process Name
1....................Process1
2....................Process2

Package - list of packages

PackageID......Package Name
1...................Package1
2...................Package2

QAChecks - list of QA checks

QACheckID......QACheckQuestion
1...................Check1
2...................Check2

PackageQAChecks - the results of the QA check per package/process

PackageQACheckID.......PackageID.....ProcessID......QACheckID......Checked
1.......1.......1........1........1
2.......1.......2........1........0


My SQL statement is this:
Code:
SELECT [qaChecks].qaCheckID,[qaChecks].qaCheckQuestion
      ,(CASE WHEN [packageQAChecks].[ProcessID] = 1 Then [checked] 
      WHEN [packageQAChecks].[ProcessID] IS NULL Then 0      
      END) As Process1,
      (CASE WHEN [packageQAChecks].[ProcessID] = 2 Then [checked] 
      WHEN [packageQAChecks].[ProcessID] IS NULL Then 0 END) As Process2
  FROM [ApplicationTracking].[dbo].[qaChecks]
  LEFT JOIN [ApplicationTracking].[dbo].[packageQAChecks]
  ON [packageQAChecks].[qaCheckID] = [qaChecks].[qaCheckID]


The reason I use the LEFT JOIN is because if the checks havent been filled out yet, I still need the questions to display with checkboxes.

Anyway, the results I get is this:

qaCheckID.....qaCheckQuestion....Process1.....Process2
1..................Check1..................1................NULL
1..................Check1...................NULL.........0

But I want:

qaCheckID.....qaCheckQuestion....Process1.....Process2
1..................Check1..................1................0


How can I change my SQL statement to get this result?

Thanks....
__________________
Captain Planet.

Reply With Quote
  #2  
Old July 26th, 2012, 08:10 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,370 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 21 m 40 sec
Reputation Power: 4140
Code:
SELECT qaChecks.qaCheckID
     , qaChecks.qaCheckQuestion
     , MAX( CASE WHEN packageQAChecks.ProcessID = 1 THEN checked 
                 WHEN packageQAChecks.ProcessID IS NULL THEN 0      
             END ) As Process1
     , MAX( CASE WHEN packageQAChecks.ProcessID = 2 THEN checked 
                 WHEN packageQAChecks.ProcessID IS NULL THEN 0 
             END ) As Process2
  FROM ApplicationTracking.dbo.qaChecks
LEFT 
  JOIN ApplicationTracking.dbo.packageQAChecks
    ON packageQAChecks.qaCheckID = qaChecks.qaCheckID
GROUP
    BY qaChecks.qaCheckID
     , qaChecks.qaCheckQuestion  
i'm not sure if this will work because of the two lines that say THEN 0 -- i think you should let those go NULL instead, and then use COALESCE on the MAX
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old July 26th, 2012, 08:32 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
Yeah, it appears to work a treat. Thanks....

I tried 'GROUP BY' but obvisouly got the 'is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause'....

Am i correct in saying that because you used MAX (an aggregate) it then allows you to use GROUP BY on the non-aggregated columns?

I guess it confused my that we're using 'MAX' when we only ever return one value for that column.....is this a kind of cheat workaround? Fine by me, but just wondered....

Reply With Quote
  #4  
Old July 26th, 2012, 09:01 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,370 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 21 m 40 sec
Reputation Power: 4140
Quote:
Originally Posted by Captain Planet
Am i correct in saying that because you used MAX (an aggregate) it then allows you to use GROUP BY on the non-aggregated columns?
exactly

and it's not a cheat

you had multiple rows...

qaCheckID.....qaCheckQuestion....Process1.....Process2
1..................Check1..................1................NULL
1..................Check1...................NULL.........0

and GROUP BY collapsed them to one ...

qaCheckID.....qaCheckQuestion....Process1.....Process2
1..................Check1..................1................0

Reply With Quote
  #5  
Old July 26th, 2012, 09:38 AM
Captain Planet's Avatar
Captain Planet Captain Planet is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 418 Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level)Captain Planet User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 13 h 42 m 8 sec
Reputation Power: 10
Ahh ok gotcha..... Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > SQL Query help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap