Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8577191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:06:16+00:00 2026-06-11T20:06:16+00:00

I have table ID State District StationCode Status ————————————————————— 1 Gujarat Banaskantha 12345 0

  • 0

I have table

ID     State        District        StationCode        Status
---------------------------------------------------------------
 1    Gujarat       Banaskantha      12345               0 
 2    Gujarat       Banaskantha      12345               0
 3    M.P.          Bhopal           22315               1
 4    Gujarat       Banaskantha      12349               0
 5    Gujarat       Banaskantha      12345               1

I need result like

State      District       Active       InActive
-----------------------------------------------
Gujarat     Banaskantha      2            1
M.P.        Bhopal           0            1

Here , Active and Inactive fields are sum of Status fields based on 0 or 1

That means here State for Gujarat, There three times 0 occured , but two duplicate rows for StationCode - 12345. It means it will be considered as One.

I have query like below

select distinct 
    state,
    District,
    SUM(
        Case 
        when Status=0 then 1 
        else 0 end
        ) AS Active,
    SUM(
        Case 
            when Status=1 then 1 
            else 0 
        end
        ) AS InActive 
from 
    Station_Master 
group by state, District

But I am unable to count duplicate StationCode row as Single.

How can I do that ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:06:18+00:00Added an answer on June 11, 2026 at 8:06 pm

    I think this would probably address your problem.
    I am not 100% sure of the syntax but if you can try it out and let me know if it doesnt work, i can try tweak it for you. The idea is to create a derived table using Group By to eliminate the duplicates that you dont want first and then the outer query is same as your original query – just on the derived table instead.

    SELECT Distinct 
      X.State, 
      X.District, 
      SUM(Case when X.Status=0 then 1 else 0 end) AS Active,
      SUM(Case when X.Status=1 then 1 else 0 end) AS InActive
    FROM 
      (Select State, District, StationCode, Status
       From Station_Master 
       Group By State,District, StationCode, Status) as X
    GROUP BY X.State, X.District
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please check below table : Id Code Name State District Status ----------------------------------------------------------- 1 10001
I have a table with addresses... street address, city, state & zip I need
I need help with a SQL query. I have a table with a 'state'
I have a (somewhat) large truth table / state machine that I need to
I have a table Create table Country_State_Mapping ( Country nvarchar(max), State nvarchar(max) ) With
Lets say I have a table with columns such as: ID Name City State
I have the following script to create a table: -- Create State table. DROP
I have a table of locations with latitude, longitude and the U.S. state fields.
I have tblPatient with the county name and state name in the table. I
Silly question for the masses: Assume you have a table that handles the state

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.