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. grouping data in terms of time (weeks, months, year etc)

grouping data in terms of time (weeks, months, year etc)

Scheduled Pinned Locked Moved Database
database
7 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.
  • R Offline
    R Offline
    Rocky
    wrote on last edited by
    #1

    hi every buddy well I'm trying to make a few queries to make reports of my project here and I wana grup the data according to months, or weeks or years So I'm writing a query which is like select m.manname+' '+prod.name as Item, SUM(s.salePrice) Total_Sale, dateName(month, b.billdate)+', '+datename(year,b.billdate) as sale_Month from sale s inner join purch p on s.purchid = p.purchid inner join manufacturer on p.manid = m.manid inner join products prod on p.catid = prod.catid inner join bill b on s.billid = b.billid group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) So can u plz tell me how can we make groupings so that I can get seperate records for November 2007, December 2007 and so on u so forth u know. thanks in advance Rocky

    B M 2 Replies Last reply
    0
    • R Rocky

      hi every buddy well I'm trying to make a few queries to make reports of my project here and I wana grup the data according to months, or weeks or years So I'm writing a query which is like select m.manname+' '+prod.name as Item, SUM(s.salePrice) Total_Sale, dateName(month, b.billdate)+', '+datename(year,b.billdate) as sale_Month from sale s inner join purch p on s.purchid = p.purchid inner join manufacturer on p.manid = m.manid inner join products prod on p.catid = prod.catid inner join bill b on s.billid = b.billid group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) So can u plz tell me how can we make groupings so that I can get seperate records for November 2007, December 2007 and so on u so forth u know. thanks in advance Rocky

      B Offline
      B Offline
      Bassam Saoud
      wrote on last edited by
      #2

      You can use DatePart method[^] to group your data

      1 Reply Last reply
      0
      • R Rocky

        hi every buddy well I'm trying to make a few queries to make reports of my project here and I wana grup the data according to months, or weeks or years So I'm writing a query which is like select m.manname+' '+prod.name as Item, SUM(s.salePrice) Total_Sale, dateName(month, b.billdate)+', '+datename(year,b.billdate) as sale_Month from sale s inner join purch p on s.purchid = p.purchid inner join manufacturer on p.manid = m.manid inner join products prod on p.catid = prod.catid inner join bill b on s.billid = b.billid group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) So can u plz tell me how can we make groupings so that I can get seperate records for November 2007, December 2007 and so on u so forth u know. thanks in advance Rocky

        M Offline
        M Offline
        M H 1 2 3
        wrote on last edited by
        #3

        instead of .... group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) try .... group by dateName(month, b.billdate)+', '+datename(year,b.billdate), m.manname+' '+prod.name I'm not sure if the answer is that easy but it looks that way to me.

        R 2 Replies Last reply
        0
        • M M H 1 2 3

          instead of .... group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) try .... group by dateName(month, b.billdate)+', '+datename(year,b.billdate), m.manname+' '+prod.name I'm not sure if the answer is that easy but it looks that way to me.

          R Offline
          R Offline
          Rocky
          wrote on last edited by
          #4

          ok I"ll try that as well but I'm also looking in to date part too

          1 Reply Last reply
          0
          • M M H 1 2 3

            instead of .... group by m.manname+' '+prod.name, dateName(month, b.billdate)+', '+datename(year,b.billdate) try .... group by dateName(month, b.billdate)+', '+datename(year,b.billdate), m.manname+' '+prod.name I'm not sure if the answer is that easy but it looks that way to me.

            R Offline
            R Offline
            Rocky
            wrote on last edited by
            #5

            I got the grouping when i use date part and seperate the month and year now but still I cant get it like a single string 'November 2007' I have a bit awkward idea to make another view over it and concatenate those month and year columns in that Rocky

            M 1 Reply Last reply
            0
            • R Rocky

              I got the grouping when i use date part and seperate the month and year now but still I cant get it like a single string 'November 2007' I have a bit awkward idea to make another view over it and concatenate those month and year columns in that Rocky

              M Offline
              M Offline
              M H 1 2 3
              wrote on last edited by
              #6

              If you have it grouping correctly is the problem what is getting displayed? I did a query like below on an example table and the grouping seems to be working and so does the display: select count(1), max(dateName(month, invcDate)+', '+datename(year,invcDate)) from invc group by dateName(month, invcnbr)+', '+datename(year,invcnbr) I had to add the max around the month string since it wasn't an aggregate, but it displays and groups.

              R 1 Reply Last reply
              0
              • M M H 1 2 3

                If you have it grouping correctly is the problem what is getting displayed? I did a query like below on an example table and the grouping seems to be working and so does the display: select count(1), max(dateName(month, invcDate)+', '+datename(year,invcDate)) from invc group by dateName(month, invcnbr)+', '+datename(year,invcnbr) I had to add the max around the month string since it wasn't an aggregate, but it displays and groups.

                R Offline
                R Offline
                Rocky
                wrote on last edited by
                #7

                well I made grouping with another idea u knwo. I make a view like (name: my_view) select m.manname+' '+prod.name item ,SUM(s.saleprice) total_sale, dateName(month, billdate) mnth, datename(year,billdate) yr from sale inner join bill on s.billid = b.billid group by dateName(month, billdate), datename(year,billdate), m.manname+' '+prod.name etc and later I make a query on it like select Item, total_sale, mnth+', '+yr from myView Atfirst i used inline view for this but then I converted it into a seperate view. and now its groupin it correctly But do u have any ideas on grouping on a weekly basis ? Rocky

                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