Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Max and Where

Max and Where

Scheduled Pinned Locked Moved Database
databasehelpquestionannouncement
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OmegaSupreme
    wrote on last edited by
    #1

    I'm using max and where in my select statement but the problem is I want the max of all the dates for a given id but only want to return those rows that fall between certain dates. A simplified version of the query is something like this : SELECT MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate FROM Booking WHERE (Booking.fkChannelId = ? ) AND (BookingDate.bookingDate > ?) AND (BookingDate.bookingDate < ?) But I'd like the max and min dates for all the bookings for a given channel id but only want to return rows that fall within the dates. Life is hard :confused: Try not! Do or do not, there is no try. - Master Yoda

    M W 2 Replies Last reply
    0
    • O OmegaSupreme

      I'm using max and where in my select statement but the problem is I want the max of all the dates for a given id but only want to return those rows that fall between certain dates. A simplified version of the query is something like this : SELECT MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate FROM Booking WHERE (Booking.fkChannelId = ? ) AND (BookingDate.bookingDate > ?) AND (BookingDate.bookingDate < ?) But I'd like the max and min dates for all the bookings for a given channel id but only want to return rows that fall within the dates. Life is hard :confused: Try not! Do or do not, there is no try. - Master Yoda

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      Without knowing your data schema (you refer to two tables Booking and BookingDate in your example, but you don't join them) it's a bit difficult to know how to help. GROUP BY and HAVING clauses may be helpful. GROUP BY allows data to be aggregated across groups, and HAVING allows you to select from the aggregated data. If WHERE applies to the data in the table, HAVING applies to the aggregated data. Stability. What an interesting concept. -- Chris Maunder

      1 Reply Last reply
      0
      • O OmegaSupreme

        I'm using max and where in my select statement but the problem is I want the max of all the dates for a given id but only want to return those rows that fall between certain dates. A simplified version of the query is something like this : SELECT MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate FROM Booking WHERE (Booking.fkChannelId = ? ) AND (BookingDate.bookingDate > ?) AND (BookingDate.bookingDate < ?) But I'd like the max and min dates for all the bookings for a given channel id but only want to return rows that fall within the dates. Life is hard :confused: Try not! Do or do not, there is no try. - Master Yoda

        W Offline
        W Offline
        WoutL
        wrote on last edited by
        #3

        Just try: SELECT MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate FROM Booking HAVING (BookingDate.bookingDate > ?) AND (BookingDate.bookingDate < ?) WHERE (Booking.fkChannelId = ? ) Maybe the HAVING part belongs after WHERE... Wout Louwers

        O 1 Reply Last reply
        0
        • W WoutL

          Just try: SELECT MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate FROM Booking HAVING (BookingDate.bookingDate > ?) AND (BookingDate.bookingDate < ?) WHERE (Booking.fkChannelId = ? ) Maybe the HAVING part belongs after WHERE... Wout Louwers

          O Offline
          O Offline
          OmegaSupreme
          wrote on last edited by
          #4

          Thank you both for your help. I tried adding the HAVING clause instead of WHERE and got the following errors: Column 'BookingDate.bookingDate' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. Column 'Booking.fkChannelId' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. The tables I have are as follows each with a one to many relationship with the following table:

          Ad
          pkAdId
          Booking
          pkBookingId
          fkAdId
          BookingDate
          pkBookingDateId
          fkBookingId
          BookingHour
          pkBookingHourId
          fkBookingDateId

          and heres the query as it stands :

          SELECT DISTINCT 
          		                      Ad.pkAdId, Ad.fileName, Ad.name,
           Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, 
          		                      AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots
          		FROM         Ad INNER JOIN
          		                      Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN
          		                      BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN
          		                      BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId
          		
          		WHERE  (BookingDate.bookingDate >= @startDate) 
          		AND (BookingDate.bookingDate <= @endDate) 
          		AND (Booking.fkChannelId = @channelId) AND
          		GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated
          		ORDER BY Ad.fileName
          

          Try not! Do or do not, there is no try. - Master Yoda

          W 1 Reply Last reply
          0
          • O OmegaSupreme

            Thank you both for your help. I tried adding the HAVING clause instead of WHERE and got the following errors: Column 'BookingDate.bookingDate' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. Column 'Booking.fkChannelId' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause. The tables I have are as follows each with a one to many relationship with the following table:

            Ad
            pkAdId
            Booking
            pkBookingId
            fkAdId
            BookingDate
            pkBookingDateId
            fkBookingId
            BookingHour
            pkBookingHourId
            fkBookingDateId

            and heres the query as it stands :

            SELECT DISTINCT 
            		                      Ad.pkAdId, Ad.fileName, Ad.name,
             Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, 
            		                      AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots
            		FROM         Ad INNER JOIN
            		                      Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN
            		                      BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN
            		                      BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId
            		
            		WHERE  (BookingDate.bookingDate >= @startDate) 
            		AND (BookingDate.bookingDate <= @endDate) 
            		AND (Booking.fkChannelId = @channelId) AND
            		GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated
            		ORDER BY Ad.fileName
            

            Try not! Do or do not, there is no try. - Master Yoda

            W Offline
            W Offline
            WoutL
            wrote on last edited by
            #5

            Try: SELECT DISTINCT Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots FROM Ad INNER JOIN Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId WHERE (Booking.fkChannelId = @channelId) GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated Having MinDate >= @startDate) AND MaxDate <= @endDate) ORDER BY Ad.fileName Wout Louwers

            O 1 Reply Last reply
            0
            • W WoutL

              Try: SELECT DISTINCT Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots FROM Ad INNER JOIN Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId WHERE (Booking.fkChannelId = @channelId) GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated Having MinDate >= @startDate) AND MaxDate <= @endDate) ORDER BY Ad.fileName Wout Louwers

              O Offline
              O Offline
              OmegaSupreme
              wrote on last edited by
              #6

              Hi Wout, Thanks for your reply, unfortunatley Query Analyser complains : Invalid column name 'MinDate'. Invalid column name 'MaxDate'. Also I want those bookings between the dates not whose max and min booking dates fall between the dates. Sorry to be a pain and thanks again. Try not! Do or do not, there is no try. - Master Yoda

              W 1 Reply Last reply
              0
              • O OmegaSupreme

                Hi Wout, Thanks for your reply, unfortunatley Query Analyser complains : Invalid column name 'MinDate'. Invalid column name 'MaxDate'. Also I want those bookings between the dates not whose max and min booking dates fall between the dates. Sorry to be a pain and thanks again. Try not! Do or do not, there is no try. - Master Yoda

                W Offline
                W Offline
                WoutL
                wrote on last edited by
                #7

                SELECT DISTINCT Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots FROM Ad INNER JOIN Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId WHERE (Booking.fkChannelId = @channelId) GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated Having MAX(BookingDate.bookingDate) >= @startDate AND MIN(BookingDate.bookingDate) <= @endDate) ORDER BY Ad.fileName ?? Wout Louwers

                O 1 Reply Last reply
                0
                • W WoutL

                  SELECT DISTINCT Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated, MAX(BookingDate.bookingDate) AS MaxDate, MIN(BookingDate.bookingDate) AS MinDate, AVG(BookingHour.bookedSlots + BookingHour.bonusSlots) AS AverageSlots FROM Ad INNER JOIN Booking ON Ad.pkAdId = Booking.fkAdId INNER JOIN BookingDate ON Booking.pkBookingId = BookingDate.fkBookingId INNER JOIN BookingHour ON BookingDate.pkBookingDateId = BookingHour.fkBookingDateId WHERE (Booking.fkChannelId = @channelId) GROUP BY Ad.pkAdId, Ad.fileName, Ad.name, Ad.length, Ad.lastUpdated Having MAX(BookingDate.bookingDate) >= @startDate AND MIN(BookingDate.bookingDate) <= @endDate) ORDER BY Ad.fileName ?? Wout Louwers

                  O Offline
                  O Offline
                  OmegaSupreme
                  wrote on last edited by
                  #8

                  Worked like a charm, thank you very much :-D:-D:-D Try not! Do or do not, there is no try. - Master Yoda

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups