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 8824455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:38:21+00:00 2026-06-14T06:38:21+00:00

I am fairly new to SQL and I am stuck on a query. I

  • 0

I am fairly new to SQL and I am stuck on a query. I have the following table called Appointment

NAME    SERVICE DATE
-----------------------------    
John    Abc 2012-11-10
Michael Xyz 2012-11-10
Denis   Xyz 2012-11-10
Denis   Pqr 2012-11-06
John    Abc 2012-11-06
Michael Abc 2012-11-05
Sofy    Abc 2012-11-04
John    Pqr 2012-11-01
Michael Pqr 2012-11-01
Michael Abc 2012-10-21
John    Abc 2012-10-23
John    HFD 2012-10-23
John    BHU 2012-11-09
Michael KSD 2012-11-08

What I want to do is count the services for individuals between the dates 2012-11-01 and 2012-11-10.

Following is the required output

NAME    Abc Pqr Xyz
--------------------------------    
John    2   1   0
Michael 1   1   1
Denis   0   1   1
Sofy    1   0   0

This is what I’ve tried:

SELECT  Name, COUNT(Distinct Service) 
FROM    [Appointment] 
WHERE   [DATE] between '2012-11-01 00:00:00.000' and '2012-11-10 23:59:59.999' 
AND     (Service = 'Abc' or Service = 'Pqr' or Service = 'Xyz') 
GROUP BY Name

Thank you in advance.

  • 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-14T06:38:22+00:00Added an answer on June 14, 2026 at 6:38 am
    SELECT Name,
           SUM(CASE WHEN Service = 'Abc' THEN 1 ELSE 0 END) Abc,
           SUM(CASE WHEN Service = 'Pqr' THEN 1 ELSE 0 END) Pqr,
           SUM(CASE WHEN Service = 'Xyz' THEN 1 ELSE 0 END) Xyz
    FROM   TableName
    WHERE [Date] BETWEEN CAST('2012-11-01' AS DATE) AND 
                         CAST('2012-11-10' AS DATE)
    GROUP BY Name
    
    • SQLFiddle Demo

    or

    SELECT Name, Abc, Pqr, Xyz
    FROM 
      (
          SELECT [Name], [Service]
          FROM TableName
          WHERE [Date] BETWEEN CAST('2012-11-01' AS DATE) AND 
                               CAST('2012-11-10' AS DATE)
      ) p
      PIVOT
      (
          COUNT ([service])
          FOR [Service] IN ( [Abc], [Pqr], [Xyz] )
      ) AS s
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following SQL statement which returns Not unique table/alias . I'm fairly
I'm fairly new to SQL, but trying to write quite a complicated query, but
I'm fairly new at SQL and have run into an issue that has me
I'm fairly new to WCF but I have a WCF Service hosted in IIS
I'm fairly new to SQL so please be gentle. I've got a table that
I am fairly new to SQL joins, but I have a tricky issue here.
I have a table with columns Category, Date, Monthly_Revenue . I need a query
I'm fairly new to SQL, and i'm trying to make a query with simple
Oracle's PL/SQL is fairly new to me, so I need some help understanding if
Alright, so I'm fairly new to PHP and SQL/MySQL so any help is appreciated.

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.