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

The Archive Base Latest Questions

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

I have a rather large query which select data from a certain date range.

  • 0

I have a rather large query which select data from a certain date range.

I would like to group data based on year.

Right now I have:

;with mappingTable as
(
select b.CLIENT_ID,f.patient_id,f.received_date,f.SPECIMEN_ID
from F_ACCESSION_DAILY f
join
(
select f.client_id,a.patient_id
from
(
SELECT
       max(received_date) AS received_date
     , patient_id         AS Patient_ID
FROM F_ACCESSION_DAILY
group by PATIENT_ID
) a
join F_ACCESSION_DAILY f
on f.PATIENT_ID=a.Patient_ID
and f.RECEIVED_DATE=a.received_date
) b
on b.Patient_ID=f.PATIENT_ID
where f.RECEIVED_DATE between '20100101' and '20110101' - as you can see this data is only for 2010
) ---there's much more to this query

as you can see this data is only for 2010

but i would like to do union all for all years: 2008, 2009, 2010, 2011, 2012…

how do i do this without rewriting the query 5 times and doing a union all between all of them?

here’s the entire monster query:

;with mappingTable as
(
select b.CLIENT_ID,f.patient_id,f.received_date,f.SPECIMEN_ID
from F_ACCESSION_DAILY f
join
(
select f.client_id,a.patient_id
from
(
SELECT
       max(received_date) AS received_date
     , patient_id         AS Patient_ID
FROM F_ACCESSION_DAILY
group by PATIENT_ID
) a
join F_ACCESSION_DAILY f
on f.PATIENT_ID=a.Patient_ID
and f.RECEIVED_DATE=a.received_date
) b
on b.Patient_ID=f.PATIENT_ID
where f.RECEIVED_DATE between '20100101' and '20110101'
)
,
  counted as(
SELECT
PATIENT_ID,
    client_id,
    --case when COUNT(*)>=12 then 12 else COUNT(*) end  TimesTested, 
    COUNT(*) as timestested,
    case when count(*)>1 then
    (datediff(day,MIN(received_date),max(received_date)))
  /(COUNT(*)-1) 
 else
0
end as testfreq
  FROM mappingTable
  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
    ),







    counted3 as 
     (
  SELECT
  patient_id,
    client_id,
   TimesTested,
    CAST(COUNT(*) AS varchar(30)) AS count,
    AVG(testfreq) as TestFreq
  FROM counted
  GROUP BY
    client_id,
    TimesTested,
    PATIENT_ID
    )
    ,
    CountOver12 as
    (    select client_id,count(*) count
    from counted
    where timestested>12
    group by CLIENT_ID)
    ,
    TotalAvgTestFreq as (

    select client_id, SUM(testfreq) / count(testfreq) TotalAvgTestFreq
    from counted3
    group by client_id
    )



    ,
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
)
,
PatientStats as
(
select 
 distinct
f.client_id
,datediff(MM,c.mlis_date_established,GETDATE()) MonthsCustomer
,count(distinct patient_id) TotalPatients
,count(distinct specimen_id) TotalSpecimens
from mappingTable f
left join D_CLIENT c
on f.CLIENT_ID=c.CLIENT_ID
group by f.client_id,c.mlis_date_established
)
,
Over12
as
(
select client_id,SUM(c) sumcount from
(
select CLIENT_ID,COUNT(*) c
from mappingTable
group by CLIENT_ID,PATIENT_ID
having count(*)>12
) a
group by client_id
),
GetMedian as(

    select client_id, avg(timestested) median_testfreq
from
(
    select client_id,
           timestested,
           rn=row_number() over (partition by CLIENT_ID
                                 order by timestested),
           c=count(timestested) over (partition by CLIENT_ID)
    from counted3
    where timestested>1
) g
where rn in (round(c/2,0),c/2+1)
group by client_id
   )
, final as (
SELECT p.*,pivoted.*,c12.count [Count13+],Over12.sumcount SumofGreaterThan12,t.TotalAvgTestFreq,median.median_testfreq
FROM pivoted
left join PatientStats p
on p.CLIENT_ID=pivoted.CLIENTID
left join Over12
on over12.CLIENT_ID=p.CLIENT_ID
left join TotalAvgTestFreq t
on t.CLIENT_ID=p.CLIENT_ID
left join CountOver12 C12
on c12.CLIENT_ID=p.CLIENT_ID
left join GetMedian median
on median.CLIENT_ID=p.CLIENT_ID
where p.CLIENT_ID not in (select CLIENTid from SalesDWH..TestPractices)
)
/* Get the data into a temp table */
SELECT * INTO #TempTable
FROM final
/* Drop the cloumns that are not needed */
ALTER TABLE #TempTable
DROP COLUMN clientid
/* Get results and drop temp table */
SELECT * FROM #TempTable
DROP TABLE #TempTable
  • 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-14T16:38:33+00:00Added an answer on June 14, 2026 at 4:38 pm

    Create a table function:

    CREATE FUNCTION fn_get_data_by_date_range
    (
       @start AS VARCHAR(8),
       @end AS VARCHAR(8)
    )
    RETURNS TABLE
    AS 
    RETURN 
    (
       ...your query...
       WHERE f.RECEIVED_DATE BETWEEN @start AND @end
    )
    

    then you can UNION different call of that function:

    SELECT * FROM fn_get_data_by_date_range('20100101','20110101')
    UNION ALL
    SELECT * FROM fn_get_data_by_date_range('20090101','20100101')
    UNION ALL
    ....
    UNION ALL
    SELECT * FROM fn_get_data_by_date_range('20070101','20080101')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather large text corpus, of which I would like to check
I have a rather large table which I need to query for a reporting
I have a rather large diff of 2 GIT branches and would like to
I have data database containing some rather large strings, each of which holds a
I have a rather large SQL query which the majority of which has come
I have a situation in which a large amount of data output from Matlab
I have a rather large SQL file which starts with the byte order marker
We have a rather large and complex application written in Java which is running
I have a rather large amount of data (100 MB or so), that I
I have the following (simplified) query select P.peopleID, P.peopleName, ED.DataNumber from peopleTable P left

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.