Help With Sql Query

Discussion in 'Databases' started by Supe, Feb 3, 2013.

  1. #1
    Ok, I am definitely no guru when it comes to SQL, I will preface this by saying that. Also, hopefully the info I post will be enough for those out there to figure out what I need to do. That being said here is my query:
    use GM001
    SELECT    tblCalendarTemp.strSessionName AS Mission, CASE tblcalendartemp.lngslot
    WHEN 1 THEN '11:10 AM' WHEN 2 THEN '11:30 AM' WHEN 3 THEN '11:50 AM' WHEN 4 THEN '12:10 PM' WHEN 5 THEN '12:30 PM' WHEN 6 THEN '12:50 PM' WHEN 7 THEN '1:10 PM' WHEN 8 THEN '1:30 PM'
    WHEN 9 THEN '1:50 PM' WHEN 10 THEN '2:10 PM'
    WHEN 11 THEN '2:30 PM' WHEN 12 THEN '2:50 PM'
    WHEN 13 THEN '3:10 PM' WHEN 14 THEN '3:30 PM' WHEN 15 THEN '3:50 PM' WHEN 16 THEN '4:10 PM' WHEN 17 THEN '4:30 PM' WHEN 18 THEN '4:50 PM'
    WHEN 19 THEN '5:10 PM' WHEN 20 THEN '5:30 PM' WHEN 21 THEN '5:50 PM' WHEN 22 THEN '6:10 PM' WHEN 23 THEN '6:30 PM' WHEN 24 THEN '6:50 PM' WHEN 25 THEN '7:10 PM' WHEN 26 THEN '7:30 PM' WHEN 27 THEN '7:50 PM' WHEN 28 THEN '8:10 PM' WHEN 29 THEN '8:30 PM' WHEN 30 THEN '8:50 PM' WHEN 31 THEN '9:10 PM' WHEN 32 THEN '9:30 PM' WHEN 33 THEN '9:50 PM' WHEN 34 THEN '10:10 PM' WHEN 35 THEN '10:30 PM' WHEN 36 THEN '10:50 PM' WHEN 37 THEN '11:10 PM' WHEN 38 THEN '11:30 PM' WHEN 39 THEN '11:50 PM' WHEN 40 THEN '12:10 AM' END AS [Time of Session], tblCalendarTemp.lngSold AS Total, COUNT(tblItemReserve.lngQuantity) AS Reserved, tblCalendarTemp.lngSold - COUNT(tblItemReserve.lngQuantity) AS Tickets
    FROM tblCalendarTemp LEFT OUTER JOIN tblItemReserve ON tblCalendarTemp.dateStart = tblItemReserve.dateCalendar
    WHERE (tblCalendarTemp.lngMerchItemID = 1) AND (tblItemReserve.lngMerchItemID = 1)
    GROUP BY tblCalendarTemp.lngSold, tblCalendarTemp.lngSlot, tblCalendarTemp.strSessionName
    Code (markup):
    The Results I am getting are in the picture below. Obviously what it is doing is counting the lngquantity column in its entirety. I have tried sum instead of count with similar results. I am ok with either being used in the final result. Obviously what I would like it to say is (using only a couple rows for examples):

    Neptune : 1:10PM : 29 : 20 : 9
    Pluto : 1:30PM : 13 : 13 : 0
    Mercury : 1:50PM : 0 : 0 : 0
    Venus : 2:10PM : 13 : 13 : 0

    What am I missing here? Can anyone help me out?
     

    Attached Files:

    Supe, Feb 3, 2013 IP