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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:29:13+00:00 2026-06-13T08:29:13+00:00

my query returns a dataset that looks like this: +———–+——–+———–+——-+——–+———–+———+——–+———–+———+——–+———–+———+——–+———–+———+——–+———–+———+——–+———–+———+——–+———–+———+——–+———–+———+———+————+——–+———+————+———+———+————+——–+ | CLIENT_ID | count1

  • 0

my query returns a dataset that looks like this:

+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+
| CLIENT_ID | count1 | TestFreq1 | stdv1 | count2 | TestFreq2 |  stdv2  | count3 | TestFreq3 |  stdv3  | count4 | TestFreq4 |  stdv4  | count5 | TestFreq5 |  stdv5  | count6 | TestFreq6 |  stdv6  | count7 | TestFreq7 |  stdv7  | count8 | TestFreq8 |  stdv8  | count9 | TestFreq9 |  stdv9  | count10 | TestFreq10 | stdv10 | count11 | TestFreq11 | stdv11  | count12 | TestFreq12 | stdv12 |
+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+
|    210893 |    136 |         0 |     0 |     81 |        41 | 79.2685 |     19 |        63 | 58.321  |     24 |        21 | 20.4896 |      5 |        25 | 8.228   |      6 |        24 | 24.0638 |      4 |        25 | 24.6103 | 2      | 25        | 2.12132 |      2 |        23 | 21.9203 | 1       | 33         | NULL   |       2 |         29 | 7.77817 | 1       | 38         | NULL   |
|    123321 |     50 |         0 |     0 |      5 |        26 | 7.87401 |     14 |        45 | 51.8002 |      3 |        25 | 14.7422 |      2 |        22 | 17.6777 |      4 |        36 | 21.4942 |      3 |        36 | 22.2711 | NULL   | NULL      | NULL    |      4 |        35 | 9.30949 | NULL    | NULL       | NULL   |       1 |         31 | NULL    | NULL    | NULL       | NULL   |
|    454322 |    232 |         0 |     0 |    173 |        10 | 33.8487 |     36 |        36 | 36.6602 |     32 |        15 | 17.485  |     10 |        38 | 22.4809 |     13 |        23 | 20.0477 |      7 |        18 | 11.4143 | 3      | 32        | 24.5425 |      6 |        25 | 16.8602 | 3       | 28         | 21.166 |       2 |         25 | 4.94975 | 1       | 34         | NULL   |
+-----------+--------+-----------+-------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+--------+-----------+---------+---------+------------+--------+---------+------------+---------+---------+------------+--------+

instead of forcing the data to go out to count13, stdv13, testfreq13, ..14..14..14, 15.15.15 how can i aggregate all the values 12 and over in the same field?

here’s my query, and thank you so much for your guidance:

;WITH counted AS (
  SELECT
    client_id,
    COUNT(*) AS TimesTested,
    (datediff(day,MIN(received_date),max(received_date)))
  /COUNT(*) as TestFreq
  FROM f_accession_daily
  GROUP BY
    client_id,
    patient_id
),
counted2 as (
  SELECT
    client_id,
    TimesTested,
    CAST(COUNT(*) AS varchar(30)) AS count,
    CAST(AVG(testfreq) as varchar(30)) as TestFreq,
    CAST(STDEV(TestFreq) as varchar(30)) Stdv
  FROM counted
  GROUP BY
    client_id,
    TimesTested
    )
    ,
unpivoted AS (
  SELECT
    client_id,
    ColumnName + CAST(TimesTested AS varchar(10)) AS ColumnName,
    ColumnValue
  FROM counted2
  UNPIVOT (
    ColumnValue FOR ColumnName IN (count, TestFreq,stdv)
  ) u
),
pivoted AS (
  SELECT
    client_id clientid,
    count1, TestFreq1,stdv1,
    count2, TestFreq2,stdv2,
    count3, TestFreq3,stdv3,
    count4, TestFreq4,stdv4,
    count5, TestFreq5,stdv5,
    count6, TestFreq6,stdv6,
    count7, TestFreq7,stdv7,
    count8, TestFreq8,stdv8,
    count9, TestFreq9,stdv9,
    count10, TestFreq10,stdv10,
    count11, TestFreq11,stdv11,
    count12, TestFreq12,stdv12
  FROM unpivoted
  PIVOT (
    MAX(ColumnValue) FOR ColumnName IN (
      count1,TestFreq1,stdv1,
      count2,TestFreq2,stdv2,
      count3,TestFreq3,stdv3,
      count4,TestFreq4,stdv4,
      count5,TestFreq5,stdv5,
      count6,TestFreq6,stdv6,
      count7,TestFreq7,stdv7,
    count8, TestFreq8,   stdv8,
    count9, TestFreq9,   stdv9,
    count10, TestFreq10,stdv10,
    count11, TestFreq11,stdv11,
    count12, TestFreq12,stdv12
    )
  ) p
)
select * from pivoted

just to clarify i want to return the same exact results, it’s just that for the last column i want to aggregate all values that fall into the 12+ bucket. all the fields are going to be the same except the last three, which are going to be:

+----------+-------------+---------+
| count12+ | TestFreq12+ | stdv12+ |
+----------+-------------+---------+
| 353      | 32423       | NULL    |
| NULL     | NULL        | NULL    |
| 342      | 25324       | NULL    |
+----------+-------------+---------+

please note the much greater numbers above compared to the rest because the 12+ have been aggregated.

thank you so much for your guidance!

  • 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-13T08:29:14+00:00Added an answer on June 13, 2026 at 8:29 am

    It seems that the fastest way to do what you want would be to change your counted2 CTE, so the column TimesTested take your logic into account. So it should be:

    counted2 as (
      SELECT
        client_id,
        CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END TimesTested,
        CAST(COUNT(*) AS varchar(30)) AS count,
        CAST(AVG(testfreq) as varchar(30)) as TestFreq,
        CAST(STDEV(TestFreq) as varchar(30)) Stdv
      FROM counted
      GROUP BY
        client_id,
        CASE WHEN TimesTested >= 12 THEN 12 ELSE TimesTested END
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query which returns a result set which looks like this: A
I have a query that looks like this SELECT J.JobID,T.Title FROM JobsTagMap J Left
Mysql query returns result like this collector amount name 1 0.00 gihan 1 2000.00
I have a table of results that looks like this: score_id, uid, af_id, level,
I have a table code_prices that looks something like this: CODE | DATE |
I have two queries. The first query looks like this : SELECT subject_id, period,
The prepared query returns the following resultset, when $this->show is set to saved-by-the-bell :
Can anyone explain why this query returns an empty result. SELECT * FROM (`bookmarks`)
When doing query optimization, the SHOW STATUS query returns values that are easy to
I'm gettintg crazy with the result of this query This query returns 29.970 rows

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.