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 query data base with 3 different conditions

How to query data base with 3 different conditions

Scheduled Pinned Locked Moved Database
databasebeta-testingperformancehelptutorial
6 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.
  • V Offline
    V Offline
    VishwaKL
    wrote on last edited by
    #1

    Hi Guys, I have a condition that, I have to display data for all states with 3 different conditions like i have a state column and i have to calculate count for judicial, non-judicial , and redemption these columns i have to display for each state how can i make it happen in a single query with good performance. I have 3 different conditions to get count, so please help me ex

                State
            
    
            
    
                Judicial
            
    
            
    
                Non-Judicial
            
    
            
    
                Redumtion
            
    
        
    
        
    
            
    
                AZ
            
    
            
    
                10
            
    
            
    
                01
            
    
            
    
                0
            
    
        
    
        
    
            
    
                CA
            
    
            
    
                12
            
    
            
    
                94
            
    
            
    
                04
            
    
        
    
        
    
            
    
                NV
            
    
            
    
                32
            
    
            
    
                04
            
    
            
    
                04
            
    
        
    
        
    
            
    
                QA
            
    
            
    
                32
            
    
            
    
                49
            
    
            
    
                04
    
    L Richard DeemingR 3 Replies Last reply
    0
    • V VishwaKL

      Hi Guys, I have a condition that, I have to display data for all states with 3 different conditions like i have a state column and i have to calculate count for judicial, non-judicial , and redemption these columns i have to display for each state how can i make it happen in a single query with good performance. I have 3 different conditions to get count, so please help me ex

                  State
              
      
              
      
                  Judicial
              
      
              
      
                  Non-Judicial
              
      
              
      
                  Redumtion
              
      
          
      
          
      
              
      
                  AZ
              
      
              
      
                  10
              
      
              
      
                  01
              
      
              
      
                  0
              
      
          
      
          
      
              
      
                  CA
              
      
              
      
                  12
              
      
              
      
                  94
              
      
              
      
                  04
              
      
          
      
          
      
              
      
                  NV
              
      
              
      
                  32
              
      
              
      
                  04
              
      
              
      
                  04
              
      
          
      
          
      
              
      
                  QA
              
      
              
      
                  32
              
      
              
      
                  49
              
      
              
      
                  04
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Fix your formatting. What is your table structure? In general, you should be able to do that using GROUP BY clause and SUM() aggregate function.

      1 Reply Last reply
      0
      • V VishwaKL

        Hi Guys, I have a condition that, I have to display data for all states with 3 different conditions like i have a state column and i have to calculate count for judicial, non-judicial , and redemption these columns i have to display for each state how can i make it happen in a single query with good performance. I have 3 different conditions to get count, so please help me ex

                    State
                
        
                
        
                    Judicial
                
        
                
        
                    Non-Judicial
                
        
                
        
                    Redumtion
                
        
            
        
            
        
                
        
                    AZ
                
        
                
        
                    10
                
        
                
        
                    01
                
        
                
        
                    0
                
        
            
        
            
        
                
        
                    CA
                
        
                
        
                    12
                
        
                
        
                    94
                
        
                
        
                    04
                
        
            
        
            
        
                
        
                    NV
                
        
                
        
                    32
                
        
                
        
                    04
                
        
                
        
                    04
                
        
            
        
            
        
                
        
                    QA
                
        
                
        
                    32
                
        
                
        
                    49
                
        
                
        
                    04
        
        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        As Shameel said, something like this should work:

        SELECT
        State,
        Sum(Case When TheColumn = 'Judicial' Then 1 Else 0 End) As JudicialCount,
        Sum(Case When TheColumn = 'Non-Judicial' Then 1 Else 0 End) As NonJudicialCount,
        Sum(Case When TheColumn = 'Redemption' Then 1 Else 0 End) As RedemptionCount
        FROM
        YourTable
        GROUP BY
        State


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        V 1 Reply Last reply
        0
        • V VishwaKL

          Hi Guys, I have a condition that, I have to display data for all states with 3 different conditions like i have a state column and i have to calculate count for judicial, non-judicial , and redemption these columns i have to display for each state how can i make it happen in a single query with good performance. I have 3 different conditions to get count, so please help me ex

                      State
                  
          
                  
          
                      Judicial
                  
          
                  
          
                      Non-Judicial
                  
          
                  
          
                      Redumtion
                  
          
              
          
              
          
                  
          
                      AZ
                  
          
                  
          
                      10
                  
          
                  
          
                      01
                  
          
                  
          
                      0
                  
          
              
          
              
          
                  
          
                      CA
                  
          
                  
          
                      12
                  
          
                  
          
                      94
                  
          
                  
          
                      04
                  
          
              
          
              
          
                  
          
                      NV
                  
          
                  
          
                      32
                  
          
                  
          
                      04
                  
          
                  
          
                      04
                  
          
              
          
              
          
                  
          
                      QA
                  
          
                  
          
                      32
                  
          
                  
          
                      49
                  
          
                  
          
                      04
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Welcome to the database forum; as you have noticed, it focuses on SQL and databases. Are you by any chance looking for an XPath-query? :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            As Shameel said, something like this should work:

            SELECT
            State,
            Sum(Case When TheColumn = 'Judicial' Then 1 Else 0 End) As JudicialCount,
            Sum(Case When TheColumn = 'Non-Judicial' Then 1 Else 0 End) As NonJudicialCount,
            Sum(Case When TheColumn = 'Redemption' Then 1 Else 0 End) As RedemptionCount
            FROM
            YourTable
            GROUP BY
            State


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            V Offline
            V Offline
            VishwaKL
            wrote on last edited by
            #5

            Thank you Richard Deeming. i written query like what you have written, is this style of query takes less execution time ?

            Richard DeemingR 1 Reply Last reply
            0
            • V VishwaKL

              Thank you Richard Deeming. i written query like what you have written, is this style of query takes less execution time ?

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              VishwaKL wrote:

              is this style of query takes less execution time ?

              Less than what? You'd need to measure the performance of this query against the query you want to compare it to.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              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