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

  • SEARCH
  • Home
  • 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 6898003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:14:28+00:00 2026-05-27T07:14:28+00:00

I work with SQL Server 2008. There is table with projects, their status and

  • 0

I work with SQL Server 2008. There is table with projects, their status and date:

ID    Status      Date
1     Gone        21-10-2011
2     Cancelled   01-10-2011
3     Won         02-10-2011
4     Won         07-10-2011
5     Gone        02-01-2010
6     Won         24-01-2010

For statistics purposes I need some T-SQL query that in result will return table like this:

Month    Year    Total    Lost   Won
10       2011    4        2      2
01       2010    2        1      1   

Where:

Total means total entries amount for unique Month-Year pair.

Lost means total entries amount for unique Month-Year pair with status ‘Gone’ or ‘Cancelled’

Won means total entries amount for unique Month-Year pair with status ‘Won’.

I understand how can I get total amount for unique Month-Year, but don’t know how to add in result set Lost and Won columns and get entries amount. And of course if there are no any ‘Won’ projects for Month-Year pair and only ‘Lost’ it should be 0 value for Won column.

  • 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-05-27T07:14:29+00:00Added an answer on May 27, 2026 at 7:14 am
    SELECT
       MONTH([Date]) [Month],
       YEAR([Date]) [Year],
       COUNT(*) [Total],
       COUNT(CASE WHEN Status IN ('Gone', 'Cancelled') THEN 1 END) [Lost],
       COUNT(CASE WHEN Status= 'Won' THEN 1 END) [Won]
    FROM
       MyTable
    GROUP BY
       MONTH([Date]),
       YEAR([Date])
    

    This works because the silent ELSE in the CASE gives NULL, and COUNT will ignore this

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in Sql Server 2008 and there is a change of table
How does INSERT, UPDATE & DELETE work on a SQL Server table partition? Technical
I have a SQL Server 2008 database table that uses uniqueidentifier as a primary
In SQL Server 2008, is there a way to insert rows while omitting those
I have a SQL Server 2008 R2 Database with twenty tables and each table
I'm using SQL Server 2008. I have a database table that looks like this
I have a brain-teaser for all you SQL Server 2008 Reporting gurus out there
Database : SQL Server 2008 Is there a way to have an insert statement
I have a simple database table (SQL Server 2008 R2 Express), which has a
I'm using SQL Server 2008; Suppose I have a table 'X' with columns 'Date1',

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.