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. Complex SQL Query for Inventory. Need Help

Complex SQL Query for Inventory. Need Help

Scheduled Pinned Locked Moved Database
databasehelptutorial
5 Posts 5 Posters 1 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.
  • C Offline
    C Offline
    chichocojo
    wrote on last edited by
    #1

    Hi, I need some guidance on the following scenario: I need to create a historic inventory of number of rooms sold per hotel. A transactional table stores data in the following columns IdHotel, CheckInDate, CheckOutDate, NoRooms I need to generate a table with 365 records/days per year per hotel as follows: hotel1, 01/Jan/2014, 1 room hotel1,02/Jan/2014,2 rooms hotel1,03/Jan/2014,2 rooms hotel1,04/Jan/2014,0 rooms . . . . . . hotel1, 01/Nov/2014, 1 room hotel1, 02/Nov/2014, 1 room hotel1, 03/Nov/2014, 1 room . . . . . I am a bit lost as to how to approach this problem for a solution. I do not need the actual code. Just a hint on how to solve this problem.

    M K K Richard DeemingR 4 Replies Last reply
    0
    • C chichocojo

      Hi, I need some guidance on the following scenario: I need to create a historic inventory of number of rooms sold per hotel. A transactional table stores data in the following columns IdHotel, CheckInDate, CheckOutDate, NoRooms I need to generate a table with 365 records/days per year per hotel as follows: hotel1, 01/Jan/2014, 1 room hotel1,02/Jan/2014,2 rooms hotel1,03/Jan/2014,2 rooms hotel1,04/Jan/2014,0 rooms . . . . . . hotel1, 01/Nov/2014, 1 room hotel1, 02/Nov/2014, 1 room hotel1, 03/Nov/2014, 1 room . . . . . I am a bit lost as to how to approach this problem for a solution. I do not need the actual code. Just a hint on how to solve this problem.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Select hotel,date,sum(no rooms )from table where datepart(yy,datefied)=2013 group by( hotel,date) That should give you some ideas, get a book on tsql and work through it,you will find it invaluable .

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • C chichocojo

        Hi, I need some guidance on the following scenario: I need to create a historic inventory of number of rooms sold per hotel. A transactional table stores data in the following columns IdHotel, CheckInDate, CheckOutDate, NoRooms I need to generate a table with 365 records/days per year per hotel as follows: hotel1, 01/Jan/2014, 1 room hotel1,02/Jan/2014,2 rooms hotel1,03/Jan/2014,2 rooms hotel1,04/Jan/2014,0 rooms . . . . . . hotel1, 01/Nov/2014, 1 room hotel1, 02/Nov/2014, 1 room hotel1, 03/Nov/2014, 1 room . . . . . I am a bit lost as to how to approach this problem for a solution. I do not need the actual code. Just a hint on how to solve this problem.

        K Offline
        K Offline
        khun_panya
        wrote on last edited by
        #3

        I have some quite dirty solution 1. Create a dummy table of an int column stores 1...365 2. Select DATEADD 1/1/2014 with an int from table above. You'll get all "dates" in 2014. 3. Select COUNT on your transaction GROUP BY date from (2). If you want to count how many rooms had been sold each day, you might need to count by matching CheckInDate. If you want to count how many rooms were occupied each day, you might need to count by seeing if the "date" within CheckInDate & CheckOutDate.

        1 Reply Last reply
        0
        • C chichocojo

          Hi, I need some guidance on the following scenario: I need to create a historic inventory of number of rooms sold per hotel. A transactional table stores data in the following columns IdHotel, CheckInDate, CheckOutDate, NoRooms I need to generate a table with 365 records/days per year per hotel as follows: hotel1, 01/Jan/2014, 1 room hotel1,02/Jan/2014,2 rooms hotel1,03/Jan/2014,2 rooms hotel1,04/Jan/2014,0 rooms . . . . . . hotel1, 01/Nov/2014, 1 room hotel1, 02/Nov/2014, 1 room hotel1, 03/Nov/2014, 1 room . . . . . I am a bit lost as to how to approach this problem for a solution. I do not need the actual code. Just a hint on how to solve this problem.

          K Offline
          K Offline
          King Fisher
          wrote on last edited by
          #4

          so,the hotel name is same ?

          1 Reply Last reply
          0
          • C chichocojo

            Hi, I need some guidance on the following scenario: I need to create a historic inventory of number of rooms sold per hotel. A transactional table stores data in the following columns IdHotel, CheckInDate, CheckOutDate, NoRooms I need to generate a table with 365 records/days per year per hotel as follows: hotel1, 01/Jan/2014, 1 room hotel1,02/Jan/2014,2 rooms hotel1,03/Jan/2014,2 rooms hotel1,04/Jan/2014,0 rooms . . . . . . hotel1, 01/Nov/2014, 1 room hotel1, 02/Nov/2014, 1 room hotel1, 03/Nov/2014, 1 room . . . . . I am a bit lost as to how to approach this problem for a solution. I do not need the actual code. Just a hint on how to solve this problem.

            Richard DeemingR Online
            Richard DeemingR Online
            Richard Deeming
            wrote on last edited by
            #5

            chichocojo wrote:

            I need to generate a table with 365 records/days per year per hotel as follows:

            You do realise than some years have 366 days, right? :) You can generate a list of all days in a given year by using a tally-table (or a common table expression which generates a tally table), along with the DateAdd function:

            DECLARE @Yearint = 2014;

            -- Get 1st January in the specified year:
            DECLARE @StartDate date = Convert(date, Convert(char(4), @Year + '0101');

            WITH cte1 (N) As
            (
            SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            UNION ALL SELECT 1
            -- 8 rows
            ),
            cte2 (N) As
            (
            SELECT TOP 366
            ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) - 1
            FROM
            cte1 As A
            CROSS JOIN cte1 As B
            CROSS JOIN cte1 As C
            -- 8 * 8 * 8 = 512 rows
            ),
            cteDates (Value) As
            (
            SELECT
            DateAdd(day, N, @StartDate)
            FROM
            cte2 -- 366 rows
            WHERE
            -- If this isn't a leap-year, we only need 355 rows:
            Year(DateAdd(day, N, @StartDate)) = @Year
            )
            SELECT
            Value
            FROM
            cteDates
            ORDER BY
            Value
            ;


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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