robthomas09
asked on
SQL - return top 10 rows by category
Hello experts,
I have a script here, that seems to be working and returning me good data, but I only want the top 10 rows by pm.description column. When I add the rownumber column, I can't seem to use an effective where <= 10 on it.:
DECLARE @MONTH1 VARCHAR(20),
@MONTH2 VARCHAR(20),
@MONTH3 VARCHAR(20),
@MONTH4 VARCHAR(20),
@MONTH5 VARCHAR(20),
@MONTH6 VARCHAR(20),
@MONTH7 VARCHAR(20),
@MONTH8 VARCHAR(20),
@MONTH9 VARCHAR(20),
@MONTH10 VARCHAR(20),
@MONTH11 VARCHAR(20),
@MONTH12 VARCHAR(20)
set @MONTH1 = DATENAME(month, getdate())
set @MONTH2 = DATENAME(month, getdate()-30)
set @MONTH3 = DATENAME(month, getdate()-60)
set @MONTH4 = DATENAME(month, getdate()-90)
set @MONTH5 = DATENAME(month, getdate()-120)
set @MONTH6 = DATENAME(month, getdate()-150)
set @MONTH7 = DATENAME(month, getdate()-180)
set @MONTH8 = DATENAME(month, getdate()-210)
set @MONTH9 = DATENAME(month, getdate()-240)
set @MONTH10 = DATENAME(month, getdate()-270)
set @MONTH11 = DATENAME(month, getdate()-300)
set @MONTH12 = DATENAME(month, getdate()-330)
exec('select
ROW_NUMBER() OVER ( PARTITION BY pm.description ORDER BY count( pd.diagnosis_code_id) DESC ) AS RowNumber,
pm.description
, pd.diagnosis_code_id
, count( pe.enc_timestamp ) as count_orders
, sum(case when pd.create_timestamp >= dateadd(day,-30,cast(getda te() as datetime))
then 1 else 0 end)
as [' + @MONTH1 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-60,cast(getda te() as datetime))
and pd.create_timestamp < dateadd(day,-30,cast(getda te() as datetime))
then 1 else 0 end)
as [' + @MONTH2 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-90,cast(getda te() as datetime))
and pd.create_timestamp < dateadd(day,-60,cast(getda te() as datetime))
then 1 else 0 end)
as [' + @MONTH3 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-120,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-90,cast(getda te() as datetime))
then 1 else 0 end)
as [' + @MONTH4 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-150,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-120,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH5 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-180,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-150,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH6 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-210,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-180,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH7 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-240,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-210,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH8 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-270,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-240,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH9 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-300,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-270,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH10 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-330,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-300,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH11 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-360,cast(getd ate() as datetime))
and pd.create_timestamp < dateadd(day,-330,cast(getd ate() as datetime))
then 1 else 0 end)
as [' + @MONTH12 + ']
from patient_diagnosis pd
join patient_encounter pe
on pe.enc_id = pd.enc_id and pe.person_id = pd.person_id
join provider_mstr pm
on pd.provider_id = pm.provider_id
where
pd.diagnosis_code_id is not null
and RowNumber <= 10
group by
pm.description
, pd.diagnosis_code_id
order by pm.description, count( pd.diagnosis_code_id) desc, pd.diagnosis_code_id')
I get an invalid column name Row Number on the "and RowNumber <= 10" at the bottom.
Thoughts?
Thanks!
I have a script here, that seems to be working and returning me good data, but I only want the top 10 rows by pm.description column. When I add the rownumber column, I can't seem to use an effective where <= 10 on it.:
DECLARE @MONTH1 VARCHAR(20),
@MONTH2 VARCHAR(20),
@MONTH3 VARCHAR(20),
@MONTH4 VARCHAR(20),
@MONTH5 VARCHAR(20),
@MONTH6 VARCHAR(20),
@MONTH7 VARCHAR(20),
@MONTH8 VARCHAR(20),
@MONTH9 VARCHAR(20),
@MONTH10 VARCHAR(20),
@MONTH11 VARCHAR(20),
@MONTH12 VARCHAR(20)
set @MONTH1 = DATENAME(month, getdate())
set @MONTH2 = DATENAME(month, getdate()-30)
set @MONTH3 = DATENAME(month, getdate()-60)
set @MONTH4 = DATENAME(month, getdate()-90)
set @MONTH5 = DATENAME(month, getdate()-120)
set @MONTH6 = DATENAME(month, getdate()-150)
set @MONTH7 = DATENAME(month, getdate()-180)
set @MONTH8 = DATENAME(month, getdate()-210)
set @MONTH9 = DATENAME(month, getdate()-240)
set @MONTH10 = DATENAME(month, getdate()-270)
set @MONTH11 = DATENAME(month, getdate()-300)
set @MONTH12 = DATENAME(month, getdate()-330)
exec('select
ROW_NUMBER() OVER ( PARTITION BY pm.description ORDER BY count( pd.diagnosis_code_id) DESC ) AS RowNumber,
pm.description
, pd.diagnosis_code_id
, count( pe.enc_timestamp ) as count_orders
, sum(case when pd.create_timestamp >= dateadd(day,-30,cast(getda
then 1 else 0 end)
as [' + @MONTH1 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-60,cast(getda
and pd.create_timestamp < dateadd(day,-30,cast(getda
then 1 else 0 end)
as [' + @MONTH2 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-90,cast(getda
and pd.create_timestamp < dateadd(day,-60,cast(getda
then 1 else 0 end)
as [' + @MONTH3 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-120,cast(getd
and pd.create_timestamp < dateadd(day,-90,cast(getda
then 1 else 0 end)
as [' + @MONTH4 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-150,cast(getd
and pd.create_timestamp < dateadd(day,-120,cast(getd
then 1 else 0 end)
as [' + @MONTH5 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-180,cast(getd
and pd.create_timestamp < dateadd(day,-150,cast(getd
then 1 else 0 end)
as [' + @MONTH6 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-210,cast(getd
and pd.create_timestamp < dateadd(day,-180,cast(getd
then 1 else 0 end)
as [' + @MONTH7 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-240,cast(getd
and pd.create_timestamp < dateadd(day,-210,cast(getd
then 1 else 0 end)
as [' + @MONTH8 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-270,cast(getd
and pd.create_timestamp < dateadd(day,-240,cast(getd
then 1 else 0 end)
as [' + @MONTH9 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-300,cast(getd
and pd.create_timestamp < dateadd(day,-270,cast(getd
then 1 else 0 end)
as [' + @MONTH10 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-330,cast(getd
and pd.create_timestamp < dateadd(day,-300,cast(getd
then 1 else 0 end)
as [' + @MONTH11 + ']
, sum(case when pd.create_timestamp >= dateadd(day,-360,cast(getd
and pd.create_timestamp < dateadd(day,-330,cast(getd
then 1 else 0 end)
as [' + @MONTH12 + ']
from patient_diagnosis pd
join patient_encounter pe
on pe.enc_id = pd.enc_id and pe.person_id = pd.person_id
join provider_mstr pm
on pd.provider_id = pm.provider_id
where
pd.diagnosis_code_id is not null
and RowNumber <= 10
group by
pm.description
, pd.diagnosis_code_id
order by pm.description, count( pd.diagnosis_code_id) desc, pd.diagnosis_code_id')
I get an invalid column name Row Number on the "and RowNumber <= 10" at the bottom.
Thoughts?
Thanks!
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER