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. Accumulation with multiple conditions

Accumulation with multiple conditions

Scheduled Pinned Locked Moved Database
databasetutorialquestion
4 Posts 2 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.
  • V Offline
    V Offline
    VentsyV
    wrote on last edited by
    #1

    I'm trying to sum up a column twice, each with a separate condition For example, if we have the following table: Visitors --------

    MeetingID LocationID NumVisitors


    1 1 3
    1 1 5
    1 3 7
    2 1 9
    2 5 3
    2 5 4
    4 3 10

    I want to specify 2 Meeting IDs (lets say 1 & 2) and have the query return the following:

    LocationID TotalNumVisitors1 TotalNumVisitors2


    1 8 9
    3 7 0
    5 0 7

    I tried using a full outer join on the table with itself, but that accumulates everything (both ids) together.

    Select MeetingID, Sum(Visitors.NumVisitors) as NumVisitors1, Sum(Visitors1.NumVisitors) NumVisitors1
    from Visitors
    full outer join ...

    where Visitors.MeetingId = 1 or Visitors1.MeetingId = 2
    Group By ...

    I then tried using sub-queries - that results in dis-joined return set - half of the columns are populated, the other half are null, then it switches

    Select Visitors.MeetingId, TotalNumVisitors = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 1),
    TotalNumVisitors1 = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 2)
    where ...

    LocationID TotalNumVisitors1 TotalNumVisitors2


    1 8 NULL
    1 NULL 9
    3 7 NULL
    5 NULL 7

    Any ideas?

    J 1 Reply Last reply
    0
    • V VentsyV

      I'm trying to sum up a column twice, each with a separate condition For example, if we have the following table: Visitors --------

      MeetingID LocationID NumVisitors


      1 1 3
      1 1 5
      1 3 7
      2 1 9
      2 5 3
      2 5 4
      4 3 10

      I want to specify 2 Meeting IDs (lets say 1 & 2) and have the query return the following:

      LocationID TotalNumVisitors1 TotalNumVisitors2


      1 8 9
      3 7 0
      5 0 7

      I tried using a full outer join on the table with itself, but that accumulates everything (both ids) together.

      Select MeetingID, Sum(Visitors.NumVisitors) as NumVisitors1, Sum(Visitors1.NumVisitors) NumVisitors1
      from Visitors
      full outer join ...

      where Visitors.MeetingId = 1 or Visitors1.MeetingId = 2
      Group By ...

      I then tried using sub-queries - that results in dis-joined return set - half of the columns are populated, the other half are null, then it switches

      Select Visitors.MeetingId, TotalNumVisitors = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 1),
      TotalNumVisitors1 = (select Sum(Visitors.NumVisitors) from Visitors where Visitors.MeetingId = 2)
      where ...

      LocationID TotalNumVisitors1 TotalNumVisitors2


      1 8 NULL
      1 NULL 9
      3 7 NULL
      5 NULL 7

      Any ideas?

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #2

      What database are you using? Some have Pivot builtin. Otherwise you can do it as a variation of this query:

      Select LocationID
      ,Sum(Case when MeetingID = 1 then Numvisitors else 0) as TotalNumVisitors1
      ,Sum(Case when MeetingID = 2 then Numvisitors else 0) as TotalNumVisitors2
      From Visitors
      Group By LocationID

      Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

      V 1 Reply Last reply
      0
      • J Jorgen Andersson

        What database are you using? Some have Pivot builtin. Otherwise you can do it as a variation of this query:

        Select LocationID
        ,Sum(Case when MeetingID = 1 then Numvisitors else 0) as TotalNumVisitors1
        ,Sum(Case when MeetingID = 2 then Numvisitors else 0) as TotalNumVisitors2
        From Visitors
        Group By LocationID

        Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

        V Offline
        V Offline
        VentsyV
        wrote on last edited by
        #3

        It's actually a query used to populate an rdl report. Would that work if the calculation is more complex? It's actually adding the rows together, then multiplying by a number from a different table. Something like this maybe:

        Select LocationID, Groups.GroupSize
        ,Sum(Case when MeetingID = 1 then (Group)*GroupSize + Numvisitors else 0) as TotalNumVisitors1
        ,Sum(Case when MeetingID = 2 then (Group)*GroupSize + Numvisitors else 0) as TotalNumVisitors2
        From Visitors
        join Groups on Visitors.SomeID = Groups.SomeID
        Group By LocationID, GroupSize

        I could probably work around this by adding some logic in the report, but it seems a bit cleaner to do my data handling in the query and have the report only display the data. And I figured there got to be an easy way to do that :rolleyes: I'll give it a try later, but it should probably work right?

        J 1 Reply Last reply
        0
        • V VentsyV

          It's actually a query used to populate an rdl report. Would that work if the calculation is more complex? It's actually adding the rows together, then multiplying by a number from a different table. Something like this maybe:

          Select LocationID, Groups.GroupSize
          ,Sum(Case when MeetingID = 1 then (Group)*GroupSize + Numvisitors else 0) as TotalNumVisitors1
          ,Sum(Case when MeetingID = 2 then (Group)*GroupSize + Numvisitors else 0) as TotalNumVisitors2
          From Visitors
          join Groups on Visitors.SomeID = Groups.SomeID
          Group By LocationID, GroupSize

          I could probably work around this by adding some logic in the report, but it seems a bit cleaner to do my data handling in the query and have the report only display the data. And I figured there got to be an easy way to do that :rolleyes: I'll give it a try later, but it should probably work right?

          J Offline
          J Offline
          Jorgen Andersson
          wrote on last edited by
          #4

          Probably. But personally I'm reluctant to use SQL for presentation logics, you'll find that it isn't very flexible. I would simply get the data you need:

          Select LocationID
          ,MeetingID
          ,Sum(Numvisitors) as TotalNumVisitors
          From Visitors
          where MeetingID IN (1,2)
          Group By LocationID,MeetingID

          And then do the Pivot in Reporting Services where it's called "Matrix control". (Here I assume that your "rdl report" means you're using SSRS)

          Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

          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