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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:09:45+00:00 2026-05-20T01:09:45+00:00

I have a table that lists visits to a clinic. I’d like to get

  • 0

I have a table that lists visits to a clinic. I’d like to get a “histogram” of sorts showing how frequently patients visit the clinic along with totals. Here’s some sample code (tested under MS SQL Server 2005) to show what I’m talking about:

CREATE TABLE #test (
  visit_id int IDENTITY(1,1),
  patient_id int
);

DECLARE @num_patients int;
SELECT @num_patients = 1000 + ABS(CHECKSUM(NEWID())) % 250;

INSERT INTO #test (patient_id)
SELECT TOP 15 PERCENT ABS(CHECKSUM(NEWID())) % @num_patients
FROM sysobjects a, sysobjects b;

-- SELECT COUNT(*) AS total_visits FROM #test;

-- SELECT COUNT(DISTINCT patient_id) AS distinct_patients FROM #test;

SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
            ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
  COUNT(*) AS num_patients, num_pat_visits * COUNT(*) AS tot_pat_visit
FROM
  (SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
GROUP BY num_pat_visits WITH ROLLUP
ORDER BY CAST(num_pat_visits AS int) DESC;

This gets me almost to where I want:

num_pat_visits num_patients tot_pat_visit 
-------------- ------------ ------------- 
60                        1            60 
54                        2           108 
52                        2           104 
51                        4           204 
50                        3           150 
49                        3           147 
48                        7           336 
47                        7           329 
46                       15           690 
45                       15           675 
44                       29          1276 
43                       36          1548 
42                       45          1890 
41                       45          1845 
40                       59          2360 
39                       71          2769 
38                       51          1938 
37                       72          2664 
36                       77          2772 
35                       74          2590 
34                       72          2448 
33                       82          2706 
32                       90          2880 
31                       74          2294 
30                       69          2070 
29                       47          1363 
28                       30           840 
27                       27           729 
26                       26           676 
25                       21           525 
24                       13           312 
23                        4            92 
22                        5           110 
21                        4            84 
20                        2            40 
18                        2            36 
Total                  1186          NULL

However, I can’t seem to get SQL Server to display the total number of visits where it says NULL on the total row.

Any ideas?

  • 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-20T01:09:46+00:00Added an answer on May 20, 2026 at 1:09 am

    I think you can just do:

    sum(num_pat_visits) as tot_pat_visit

    SELECT CASE GROUPING(num_pat_visits) WHEN 1 THEN 'Total'
                ELSE CAST(num_pat_visits AS varchar(5)) END AS num_pat_visits,
      COUNT(*) AS num_patients, 
      --num_pat_visits * COUNT(*) AS tot_pat_visit
      sum(num_pat_visits) as tot_pat_visit
    FROM
      (SELECT patient_id, COUNT(*) AS num_pat_visits FROM #test GROUP BY patient_id) a
    GROUP BY num_pat_visits WITH ROLLUP
    ORDER BY CAST(num_pat_visits AS int) DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that lists number of comments from a particular site like
I have a table that lists the duration of different activities. It looks like
I have the following table that lists all awarded cups: Name: Cups Columns: Month,
So I have a list box that displays averages in a table like format
I have a table that lists users that are registered at my website. The
I have a table that lists a bunch of customers. The last cell is
I have a table that lists people and all their contact info. I want
I have a table that lists products as well as displays a quantity text
I have a table that lists items and has a quantity textbox column that
I have a simple table that is used to record events against specific visits:

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.