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. Records with default values

Records with default values

Scheduled Pinned Locked Moved Database
mysqlhelp
13 Posts 5 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.
  • C CodingLover

    Hi all I want to count records in a table respect to each date. select count(id), add_date from packages group by add_date this gives results only for dates which has exist. Say I want to find the count in a range of dates, and if records not found then the count as zero. Is it possible to do this with MySQL

    I appreciate your help all the time... CodingLover :)

    W Offline
    W Offline
    Wendelius
    wrote on last edited by
    #2

    Hi, Perhaps the easiest way could be that you create a new table, for example SingleDate and add all necessary dates to that table. After that, using outer join, join the data between the date table and your table. So something like:

    SELECT ...
    FROM SingleDate d LEFT OUTER JOIN Packages p
    ON d.Date = p.Add_Date
    GROUP BY d.Date

    The need to optimize rises from a bad design.My articles[^]

    C 1 Reply Last reply
    0
    • C CodingLover

      Hi all I want to count records in a table respect to each date. select count(id), add_date from packages group by add_date this gives results only for dates which has exist. Say I want to find the count in a range of dates, and if records not found then the count as zero. Is it possible to do this with MySQL

      I appreciate your help all the time... CodingLover :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Step 1: Create a table variable with a date column and fill the table with the date range that you need (probably the min and max dates from 'packages' table) Step 2: Use your query and do a union with the temp table created in step 1

      C 1 Reply Last reply
      0
      • W Wendelius

        Hi, Perhaps the easiest way could be that you create a new table, for example SingleDate and add all necessary dates to that table. After that, using outer join, join the data between the date table and your table. So something like:

        SELECT ...
        FROM SingleDate d LEFT OUTER JOIN Packages p
        ON d.Date = p.Add_Date
        GROUP BY d.Date

        The need to optimize rises from a bad design.My articles[^]

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #4

        Thanks for the comment. Should I do with a temporary table or create a table in the database permanently? I've tried the following.

        CREATE TEMPORARY TABLE date_col (add_dates nvarchar(50));
        INSERT INTO date_col (add_dates) VALUES('2011-04-06');
        SELECT * FROM date_col;

        But stuck with two things. 1. How can I loop the date range and add all the dates into the table 2. I think to be in safe side I want to drop the table after the query required data, isn't it?

        I appreciate your help all the time... CodingLover :)

        W 1 Reply Last reply
        0
        • L Lost User

          Step 1: Create a table variable with a date column and fill the table with the date range that you need (probably the min and max dates from 'packages' table) Step 2: Use your query and do a union with the temp table created in step 1

          C Offline
          C Offline
          CodingLover
          wrote on last edited by
          #5

          Shameel wrote:

          Step 1: Create a table variable with a date column and fill the table with the date range that you need (probably the min and max dates from 'packages' table)

          So I can move with a temporary table or a permanent one. But I'm stuck with how to insert range of dates into it. Something like this ...

          CREATE TEMPORARY TABLE date_col (add_dates nvarchar(50));
          INSERT INTO date_col (add_dates) VALUES ('2011-04-06' through '2011-04-01'); /* how can I do that */

          I appreciate your help all the time... CodingLover :)

          1 Reply Last reply
          0
          • C CodingLover

            Thanks for the comment. Should I do with a temporary table or create a table in the database permanently? I've tried the following.

            CREATE TEMPORARY TABLE date_col (add_dates nvarchar(50));
            INSERT INTO date_col (add_dates) VALUES('2011-04-06');
            SELECT * FROM date_col;

            But stuck with two things. 1. How can I loop the date range and add all the dates into the table 2. I think to be in safe side I want to drop the table after the query required data, isn't it?

            I appreciate your help all the time... CodingLover :)

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #6

            I would do it permanently since most likely this is not the only case when you need the dates. To fill the table. Why not create a stored procedure. If you need to fill for a specific period again then you can use the same procedure. Something like:

            CREATE PROCEDURE DateFill(d1 DATE, d2 DATE)
            BEGIN
            SET @d3 = d1;
            REPEAT
            INSERT INTO DateTable (date_co) VALUES (@d3);
            SET @d3 = DATE_ADD(@d3, INTERVAL 1 DAY);
            UNTIL @d3 > d2
            END;
            //

            For dropping the table, I wouldn't since if the table is created and permanent you can continue to use it later. Just add a long enough date range to the table (say, 500 years :)) Also it might be a good idea to add an index to the table to speed up the usage: http://dev.mysql.com/doc/refman/5.1/en/create-index.html[^]

            The need to optimize rises from a bad design.My articles[^]

            C 1 Reply Last reply
            0
            • W Wendelius

              I would do it permanently since most likely this is not the only case when you need the dates. To fill the table. Why not create a stored procedure. If you need to fill for a specific period again then you can use the same procedure. Something like:

              CREATE PROCEDURE DateFill(d1 DATE, d2 DATE)
              BEGIN
              SET @d3 = d1;
              REPEAT
              INSERT INTO DateTable (date_co) VALUES (@d3);
              SET @d3 = DATE_ADD(@d3, INTERVAL 1 DAY);
              UNTIL @d3 > d2
              END;
              //

              For dropping the table, I wouldn't since if the table is created and permanent you can continue to use it later. Just add a long enough date range to the table (say, 500 years :)) Also it might be a good idea to add an index to the table to speed up the usage: http://dev.mysql.com/doc/refman/5.1/en/create-index.html[^]

              The need to optimize rises from a bad design.My articles[^]

              C Offline
              C Offline
              CodingLover
              wrote on last edited by
              #7

              Actually I don't want to keep records for a long time. Only for an instance. The requirement is, for few sales rep I want to find the sales in a date range. That is for each sales rep I want to find the sales in a date range.

              I appreciate your help all the time... CodingLover :)

              W 1 Reply Last reply
              0
              • C CodingLover

                Actually I don't want to keep records for a long time. Only for an instance. The requirement is, for few sales rep I want to find the sales in a date range. That is for each sales rep I want to find the sales in a date range.

                I appreciate your help all the time... CodingLover :)

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #8

                CodingLover wrote:

                The requirement is, for few sales rep I want to find the sales in a date range

                This is a different thing. What I'm saying is that consider populating the table with long enough date range. And when you use the table take only the relevant portion of dates from it using proper WHERE condition. For example:

                SELECT ...
                FROM DateTable dt LEFT OUTER JOIN MyTable mt
                ON dt.DateColumn = mt.DateColumn
                WHERE dt.DateColumn BETWEEN @startdate AND @enddate

                This way you can reuse the same data every time you fetch data for a new date range without having to re-create the dates.

                The need to optimize rises from a bad design.My articles[^]

                C 1 Reply Last reply
                0
                • W Wendelius

                  CodingLover wrote:

                  The requirement is, for few sales rep I want to find the sales in a date range

                  This is a different thing. What I'm saying is that consider populating the table with long enough date range. And when you use the table take only the relevant portion of dates from it using proper WHERE condition. For example:

                  SELECT ...
                  FROM DateTable dt LEFT OUTER JOIN MyTable mt
                  ON dt.DateColumn = mt.DateColumn
                  WHERE dt.DateColumn BETWEEN @startdate AND @enddate

                  This way you can reuse the same data every time you fetch data for a new date range without having to re-create the dates.

                  The need to optimize rises from a bad design.My articles[^]

                  C Offline
                  C Offline
                  CodingLover
                  wrote on last edited by
                  #9

                  Oh, I think I got your point now. What you are saying is keep dates in a permanent table ( for a long dates, eg: 2000-01-01 to 2500-01-01) and use that table to join with the other table in the date range I want to.

                  I appreciate your help all the time... CodingLover :)

                  W 1 Reply Last reply
                  0
                  • C CodingLover

                    Oh, I think I got your point now. What you are saying is keep dates in a permanent table ( for a long dates, eg: 2000-01-01 to 2500-01-01) and use that table to join with the other table in the date range I want to.

                    I appreciate your help all the time... CodingLover :)

                    W Offline
                    W Offline
                    Wendelius
                    wrote on last edited by
                    #10

                    Exactly :)

                    The need to optimize rises from a bad design.My articles[^]

                    C 1 Reply Last reply
                    0
                    • W Wendelius

                      Exactly :)

                      The need to optimize rises from a bad design.My articles[^]

                      C Offline
                      C Offline
                      CodingLover
                      wrote on last edited by
                      #11

                      Thanks buddy. I'll give a try and let you know. So I'll keep this thread as it is. :)

                      I appreciate your help all the time... CodingLover :)

                      1 Reply Last reply
                      0
                      • C CodingLover

                        Hi all I want to count records in a table respect to each date. select count(id), add_date from packages group by add_date this gives results only for dates which has exist. Say I want to find the count in a range of dates, and if records not found then the count as zero. Is it possible to do this with MySQL

                        I appreciate your help all the time... CodingLover :)

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #12

                        I would suppose that there is some clever way to do this with an "integers" table. The following link shows an example used to create a range of dates. http://books.google.com/books?id=EoSNYVb5LAsC&pg=PA101&lpg=PA101&dq=%22integers+table%22+sql&source=bl&ots=-cOiNaw7DQ&sig=DKJMIdo4xUIIzepSw1icsnmCFkg&hl=en&ei=qrCcTez9DJOx0QGFv-nKAg&sa=X&oi=book_result&ct=result&resnum=6&ved=0CEEQ6AEwBQ#v=onepage&q=%22integers%20table%22%20sql&f=false[^]

                        1 Reply Last reply
                        0
                        • C CodingLover

                          Hi all I want to count records in a table respect to each date. select count(id), add_date from packages group by add_date this gives results only for dates which has exist. Say I want to find the count in a range of dates, and if records not found then the count as zero. Is it possible to do this with MySQL

                          I appreciate your help all the time... CodingLover :)

                          S Offline
                          S Offline
                          SilimSayo
                          wrote on last edited by
                          #13

                          This my help http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx[^]

                          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