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. How to get a static result from a table?

How to get a static result from a table?

Scheduled Pinned Locked Moved Database
tutorialdatabasesql-serversysadminquestion
5 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.
  • D Offline
    D Offline
    daiwuju
    wrote on last edited by
    #1

    I have a table in SQL Server 2008 like this: EventID UserID State 1 0987 A 2 0987 A 3 0987 B 4 0956 A 5 0987 A 6 0019 A 7 0019 B 8 1289 B 9 1289 B 10 0019 A I want to get some results from this table in such form: UserID StateCount_of_A StateCount_of_B 0987 3 1 0019 2 1 1289 0 2 0956 1 0 I have try a few days, but I could get a disappointed result for ony one UserID. My result likes this one, for example with UserID 0987: StateCount_of_A 3 StateCount_of_B 1 Thanks for anyone who could give me some tip.

    D L P 3 Replies Last reply
    0
    • D daiwuju

      I have a table in SQL Server 2008 like this: EventID UserID State 1 0987 A 2 0987 A 3 0987 B 4 0956 A 5 0987 A 6 0019 A 7 0019 B 8 1289 B 9 1289 B 10 0019 A I want to get some results from this table in such form: UserID StateCount_of_A StateCount_of_B 0987 3 1 0019 2 1 1289 0 2 0956 1 0 I have try a few days, but I could get a disappointed result for ony one UserID. My result likes this one, for example with UserID 0987: StateCount_of_A 3 StateCount_of_B 1 Thanks for anyone who could give me some tip.

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Try ... Select EventID,(select count(*) from E2 where E1.EventID = E2.EventID) As Count_of_A, (select count(*) from E3 where E1.EventID = E3.EventID) As Count_of_B, from E1 Remeber to Vote. :cool:

      M 1 Reply Last reply
      0
      • D daiwuju

        I have a table in SQL Server 2008 like this: EventID UserID State 1 0987 A 2 0987 A 3 0987 B 4 0956 A 5 0987 A 6 0019 A 7 0019 B 8 1289 B 9 1289 B 10 0019 A I want to get some results from this table in such form: UserID StateCount_of_A StateCount_of_B 0987 3 1 0019 2 1 1289 0 2 0956 1 0 I have try a few days, but I could get a disappointed result for ony one UserID. My result likes this one, for example with UserID 0987: StateCount_of_A 3 StateCount_of_B 1 Thanks for anyone who could give me some tip.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        I haven't tried this, however my first try would look like this:

        SELECT DISTINCT UserID, StateCount_of_A, StateCount_of_B FROM tablename AS T1
        LEFT JOIN (SELECT UserID, COUNT(*) AS StateCount_of_A FROM tablename AS TA WHERE State='A') ON TA.UserID = T1.UserID
        LEFT JOIN (SELECT UserID, COUNT(*) AS StateCount_of_B FROM tablename AS TB WHERE State='B') ON TB.UserID = T1.UserID
        ORDER BY UserID

        (Actually I would try a subset of it with a single state first, then expand for the exact requirements). The fundamental trick is to use the same table more than once, and give each "instance" a different name using the AS keyword. And then one might have to throw in a couple of COALESCE functions to replace null counts by zeroes. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        1 Reply Last reply
        0
        • D daiwuju

          I have a table in SQL Server 2008 like this: EventID UserID State 1 0987 A 2 0987 A 3 0987 B 4 0956 A 5 0987 A 6 0019 A 7 0019 B 8 1289 B 9 1289 B 10 0019 A I want to get some results from this table in such form: UserID StateCount_of_A StateCount_of_B 0987 3 1 0019 2 1 1289 0 2 0956 1 0 I have try a few days, but I could get a disappointed result for ony one UserID. My result likes this one, for example with UserID 0987: StateCount_of_A 3 StateCount_of_B 1 Thanks for anyone who could give me some tip.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          SELECT UserId
          ,SUM(StateA) StateCount_of_A
          ,SUM(StateB) StateCount_of_B
          FROM (
          SELECT UserId
          ,CASE WHEN [State]='A' THEN 1 ELSE 0 END StateA
          ,CASE WHEN [State]='B' THEN 1 ELSE 0 END StateB
          FROM Junk
          ) T
          GROUP BY UserID

          1 Reply Last reply
          0
          • D David Mujica

            Try ... Select EventID,(select count(*) from E2 where E1.EventID = E2.EventID) As Count_of_A, (select count(*) from E3 where E1.EventID = E3.EventID) As Count_of_B, from E1 Remeber to Vote. :cool:

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

            You seemed to have picked up a stalker or someone objects to the unformatted code or you are getting a reaction to your sig!

            Never underestimate the power of human stupidity RAH

            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