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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:51:24+00:00 2026-06-13T07:51:24+00:00

my pivot query generates: +———–+—-+—-+—+—+—+—+—+ | client_id | 1 | 2 | 3 |

  • 0

my pivot query generates:

+-----------+----+----+---+---+---+---+---+
| client_id | 1  | 2  | 3 | 4 | 5 | 6 | 7 |
+-----------+----+----+---+---+---+---+---+
|    216436 |  9 |  0 | 0 | 0 | 0 | 0 | 0 |
|    110522 | 76 |  3 | 0 | 0 | 0 | 0 | 0 |
|    214981 |  0 |  1 | 0 | 0 | 0 | 0 | 0 |
|    216360 | 52 |  1 | 0 | 0 | 0 | 0 | 0 |
|    102574 |  1 |  0 | 0 | 0 | 0 | 0 | 0 |
|    211754 | 97 | 14 | 2 | 0 | 0 | 0 | 0 |
|    210734 |  8 |  4 | 0 | 0 | 0 | 0 | 0 |
|    100123 |  1 |  0 | 0 | 0 | 0 | 0 | 0 |
|    101840 |  2 |  0 | 0 | 0 | 0 | 0 | 0 |
+-----------+----+----+---+---+---+---+---+

here’s the query:

   select client_id,
   [1],[2],[3],[4],[5],[6],[7] -- these are timestested (the amount of times tested)
   from
   (   SELECT DISTINCT CLIENT_ID
   , PATIENT_ID
   , count(*) over (partition by client_id, patient_id) AS patientcount

   from f_accession_daily) as SourceTable
   PIVOT
   (
   count(patient_id)
   for patientcount in ([1],[2],[3],[4],[5],[6],[7])
   ) as pivottable

I need to bring the max/min dates for every time tested, (for [1], [2], [3], etc) from this table:

+-----------+-------------+-------+------------+------------+
| client_id | TimesTested | count | maxRecDate | minRecDate |
+-----------+-------------+-------+------------+------------+
|    100034 |           2 |     1 | 6/25/2008  | 6/23/2008  |
|    100034 |           1 |    20 | 6/30/2008  | 6/19/2008  |
|    100038 |           3 |     1 | 7/25/2008  | 7/23/2008  |
|    100038 |           1 |     4 | 7/25/2008  | 7/1/2008   |
|    100050 |           1 |    15 | 8/11/2008  | 7/14/2008  |
|    100060 |           1 |     2 | 8/12/2008  | 7/29/2008  |
|    100070 |           1 |     3 | 8/15/2008  | 8/15/2008  |
|    100049 |           1 |     3 | 8/22/2008  | 7/11/2008  |
|    100029 |           3 |     2 | 8/25/2008  | 6/18/2008  |
+-----------+-------------+-------+------------+------------+

the above table is generated by:

SELECT a.client_id AS client_id
,a.patientcount TimesTested
   , count(a.patientcount)/a.patientcount AS count
   , max(f.received_date) AS maxRecDate
   , min(f.received_date) AS minRecDate
FROM
(
   SELECT DISTINCT CLIENT_ID
   , PATIENT_ID
   , count(*) over (partition by client_id, patient_id) AS patientcount

   from f_accession_daily

) AS a
JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
   AND a.PATIENT_ID = f.PATIENT_ID

GROUP BY a.CLIENT_ID, a.patientcount

the resulting table that i need to get:

+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+
| client_id | 1  | maxdate1 | mindate1  | 2  | maxdate2 | mindate2  | 3 | maxdate3 | mindate3  | 4 | maxdate4 | mindate4  |  5  |
+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+
|    216436 |  9 | 1/1/2011 | 1/23/1985 |  0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | etc |
|    110522 | 76 | 1/1/2011 | 1/23/1984 |  3 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 | 0 | 2/1/2011 | 1/23/1984 |     |
|    214981 |  0 | 1/1/2013 | 1/23/1985 |  1 | 1/1/2013 | 1/23/1985 | 0 | 1/1/2013 | 1/23/1985 | 0 | 1/1/2013 | 1/23/1985 |     |
|    216360 | 52 | 1/1/2011 | 1/23/1985 |  1 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 |     |
|    102574 |  1 | 1/1/2011 | 1/23/1985 |  0 | 1/1/2014 | 1/23/1980 | 0 | 2/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 |     |
|    211754 | 97 | 1/1/2012 | 1/23/1985 | 14 | 1/1/2012 | 1/23/1985 | 2 | 1/1/2012 | 1/23/1985 | 0 | 1/1/2012 | 1/23/1985 |     |
|    210734 |  8 | 1/1/2011 | 1/23/1984 |  4 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 | 0 | 1/1/2011 | 1/23/1984 |     |
|    100123 |  1 | 1/1/2011 | 1/23/1985 |  0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1987 |     |
|    101840 |  2 | 1/1/2011 | 1/23/1985 |  0 | 1/1/2011 | 1/23/1980 | 0 | 1/1/2011 | 1/23/1985 | 0 | 1/1/2011 | 1/23/1985 |     |
+-----------+----+----------+-----------+----+----------+-----------+---+----------+-----------+---+----------+-----------+-----+

how do i join the two tables? the speed does not matter! thank you very much for your kind help.

  • 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-13T07:51:25+00:00Added an answer on June 13, 2026 at 7:51 am

    Not the prettiest, but I left your original queries intact and put it all in one [big] statement:

    ;WITH 
    PivotQuery as (
        select client_id,
           [1],[2],[3],[4],[5],[6],[7]
           from
           (   SELECT DISTINCT CLIENT_ID
           , PATIENT_ID
           , count(*) over (partition by client_id, patient_id) AS patientcount
    
           from f_accession_daily) as SourceTable
           PIVOT
           (
           count(patient_id)
           for patientcount in ([1],[2],[3],[4],[5],[6],[7])
           ) as pivottable),
    
    MinMaxTimes as (
        SELECT a.client_id AS client_id
        ,a.patientcount TimesTested
           , count(a.patientcount)/a.patientcount AS count
           , max(f.received_date) AS maxRecDate
           , min(f.received_date) AS minRecDate
        FROM
        (
           SELECT DISTINCT CLIENT_ID
           , PATIENT_ID
           , count(*) over (partition by client_id, patient_id) AS patientcount
    
           from f_accession_daily
    
        ) AS a
        JOIN F_ACCESSION_DAILY AS f ON a.CLIENT_ID = f.CLIENT_ID
           AND a.PATIENT_ID = f.PATIENT_ID
    
        GROUP BY a.CLIENT_ID, a.patientcount),
    
    maxDates as (
    SELECT client_id, [1] maxdate1, [2] maxdate2, [3] maxdate3, [4] maxdate4, [5] maxdate5, [6] maxdate6, [7] maxdate7
    FROM MinMaxTimes t
    PIVOT (max(maxRecDate)
    for TimesTested IN ([1], [2], [3], [4], [5], [6], [7])
    ) as p),
    
    minDates as (
    SELECT client_id, [1] mindate1, [2] mindate2, [3] mindate3, [4] mindate4, [5] mindate5, [6] mindate6, [7] mindate7
    FROM MinMaxTimes t
    PIVOT (max(minRecDate)
    for TimesTested IN ([1], [2], [3], [4], [5], [6], [7])
    ) as p)
    
    SELECT p.client_id, 
        p.[1], max(maxdate1) maxdate1, max(mindate1) mindate1,
        p.[2], max(maxdate2) maxdate2, max(mindate2) mindate2,
        p.[3], max(maxdate3) maxdate3, max(mindate3) mindate3,
        p.[4], max(maxdate4) maxdate4, max(mindate4) mindate4,
        p.[5], max(maxdate5) maxdate5, max(mindate5) mindate5,
        p.[6], max(maxdate6) maxdate6, max(mindate6) mindate6,
        p.[7], max(maxdate7) maxdate7, max(mindate7) mindate7   
    FROM PivotQuery p
    LEFT OUTER JOIN maxDates a ON p.client_id = a.client_id
    LEFT OUTER JOIN mindates i ON a.client_id = i.client_id
    GROUP BY p.client_id, p.[1], p.[2], p.[3], p.[4], p.[5], p.[6], p.[7]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a pivot query in SQL as below: SELECT classification, [BSL] AS
I'm looking for doing a Pivot Table query in vb.net. I've found a lot
I want to use Access database with query for pivot table in Excel. How
I have query to show the table like this: but I want to PIVOT
I have a query where I'm trying pivot row values into column names and
Is there any way to create a pivot and/or cross-tab query using for SQLite
I am trying to do some complex pivot query... and I need to do
I'm trying to query a pivot table with Kohana's ORM and I'm wondering if
I'm working on a dynamic pivot query on a table that contains: OID -
I am trying to get a grip on the pivot query syntax. I have

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.