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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Need help in query

Need help in query

Scheduled Pinned Locked Moved Database
databasehelpquestion
7 Posts 4 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.
  • S Offline
    S Offline
    Saiyed Alam
    wrote on last edited by
    #1

    I have a table which is some thing like below..

    Date ID

    2009-07-01 1
    2009-07-01 2
    2009-07-01 3
    2009-08-01 4
    2009-08-01 5
    2009-08-01 6
    2009-09-01 7
    2009-09-01 8
    2009-10-01 9
    2009-10-01 10
    2009-11-01 11

    .... Now I need to write a query which will show a output like below.

    Date Start End
    2009-07 1 3
    2009-08 4 6
    2009-09 7 8

    ... How can I do this.. Any help would be highly appreciated Thanking In Advance Johnny

    J W 2 Replies Last reply
    0
    • S Saiyed Alam

      I have a table which is some thing like below..

      Date ID

      2009-07-01 1
      2009-07-01 2
      2009-07-01 3
      2009-08-01 4
      2009-08-01 5
      2009-08-01 6
      2009-09-01 7
      2009-09-01 8
      2009-10-01 9
      2009-10-01 10
      2009-11-01 11

      .... Now I need to write a query which will show a output like below.

      Date Start End
      2009-07 1 3
      2009-08 4 6
      2009-09 7 8

      ... How can I do this.. Any help would be highly appreciated Thanking In Advance Johnny

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Thats about the easiest type of aggregate you can hope for. Simple MIN and MAX over a grouped set:

      SELECT [date],
      MIN([id]),
      MAX([id])
      FROM [YourTableName]
      GROUP BY date

      D 1 Reply Last reply
      0
      • S Saiyed Alam

        I have a table which is some thing like below..

        Date ID

        2009-07-01 1
        2009-07-01 2
        2009-07-01 3
        2009-08-01 4
        2009-08-01 5
        2009-08-01 6
        2009-09-01 7
        2009-09-01 8
        2009-10-01 9
        2009-10-01 10
        2009-11-01 11

        .... Now I need to write a query which will show a output like below.

        Date Start End
        2009-07 1 3
        2009-08 4 6
        2009-09 7 8

        ... How can I do this.. Any help would be highly appreciated Thanking In Advance Johnny

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        Hi, Try this

        SELECT DISTINCT d.Date1 as `ActionDate`, (SELECT MIN(ID) FROM dates WHERE Date1 = `ActionDate`) as `Start`, (SELECT MAX(ID) FROM dates WHERE Date1 = `ActionDate`) as `End` FROM dates d;

        Happy Coding This assumes table is called dates and date column is called date1

        J 1 Reply Last reply
        0
        • J J4amieC

          Thats about the easiest type of aggregate you can hope for. Simple MIN and MAX over a grouped set:

          SELECT [date],
          MIN([id]),
          MAX([id])
          FROM [YourTableName]
          GROUP BY date

          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          He doesn't want to group by date, he wants to group by month so it is a little more complicated. (And before you ask, no, I don't know what the solution is.)

          J 1 Reply Last reply
          0
          • D David Skelly

            He doesn't want to group by date, he wants to group by month so it is a little more complicated. (And before you ask, no, I don't know what the solution is.)

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            So he did, I missed that. Still easy though:

            SELECT MONTH(date),YEAR(date),
            MIN([id]),
            MAX([id])
            FROM [YourTableName]
            GROUP BY MONTH(date),YEAR(date)

            and if you really must mash that first column back to the format he wanted

            SELECT
            CAST(YEAR(date) AS VARCHAR(4)) + '-' + CAST(MONTH(date) AS VARCHAR(2)),
            MIN([id]),
            MAX([id])
            FROM [YourTableName]
            GROUP BY MONTH(date),YEAR(date)

            S 1 Reply Last reply
            0
            • W Wayne Gaylard

              Hi, Try this

              SELECT DISTINCT d.Date1 as `ActionDate`, (SELECT MIN(ID) FROM dates WHERE Date1 = `ActionDate`) as `Start`, (SELECT MAX(ID) FROM dates WHERE Date1 = `ActionDate`) as `End` FROM dates d;

              Happy Coding This assumes table is called dates and date column is called date1

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              This gives the same result as mine above, but is far less readable and far less efficient. Get friendly with the GROUP BY functionality of sql if you want to aggregate data.

              1 Reply Last reply
              0
              • J J4amieC

                So he did, I missed that. Still easy though:

                SELECT MONTH(date),YEAR(date),
                MIN([id]),
                MAX([id])
                FROM [YourTableName]
                GROUP BY MONTH(date),YEAR(date)

                and if you really must mash that first column back to the format he wanted

                SELECT
                CAST(YEAR(date) AS VARCHAR(4)) + '-' + CAST(MONTH(date) AS VARCHAR(2)),
                MIN([id]),
                MAX([id])
                FROM [YourTableName]
                GROUP BY MONTH(date),YEAR(date)

                S Offline
                S Offline
                Saiyed Alam
                wrote on last edited by
                #7

                Thanks It did work..

                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