sql - Group by unlike values -
i'm trying create report shows line counts based on report type. i'm grouping on report type, report types have specific names want combined under 1 listing.
for example: urgent care
reports can have type of urgent care
, sd494
, sd510
, sd546
, multiple others. sd###
show under urgent care
rather separate listing. same op notes
, can op note
, sd805
, polysomnography
, etc. under op notes
, in 1 report. have 20 different reports can run individually each report type.
how can accomplish task?
declare @officeid int = 93; declare @startdate datetime = '6/01/2015'; declare @enddate datetime = '07/01/2015'; select (r.reporttype), sum(cast(round(r.transcriptionlinecount,2) decimal(8,2))) "bill lines", (cast(round(sum(r.transcriptionlinecount) * .13,2) decimal(8,2))) "amount" rptlinecountinfo r join dictation d on d.dictationid = r.dictationid d.officeid = @officeid , r.finishedtime between @startdate , @enddate , (d.dictationstatus != 'd' , d.dictationstatus != 'q') group r.reporttype
here results: (i've added numbering column isn't in paragraph form)
- asc pre-op consult 1848.96 240.36
- audiology report 4403.73 572.48
- cardiac cath 452.26 58.79
- colon 539 op note 117.49 15.27
- colonoscopy - op note 118.11 15.35
- colonoscopy-op note 54.88 7.13
- consult 3219.01 418.47
- cpap titration 1802.61 234.34
- discharge summary 5259.79 683.77
- egd - op note 316.66 41.17
- egd 537 op note 178.71 23.23
- egd-op note 194.75 25.32
- electrophysiology report 1679.79 218.37
- er admit 39402.45 5122.32
- er format 150642.53 19583.53
- ercp-op note 108.88 14.15
- hospital course 5344.91 694.84
- multidisciplinary oncology 272.38 35.41
- op note 32437.7 4216.9
- op note - sd698 57.66 7.5
- pain clinic letter 32.71 4.25
- pain clinic report 6.71 0.87
- pain clinic sd1020 37.75 4.91
- php discharge summary 336.81 43.79
- pm inpatient consultation 1602.96 208.38
- polysomnography 2466.16 320.6
- pre-op consult 30812.45 4005.62
- sd288 initial injury 6398.55 831.81
- sd289 follow 2893.45 376.15
- sd289 follow-up 2498.36 324.79
- sd488 op note 48.86 6.35
- sd496 - urgent care 32.34 4.2
- sd503 - urgent care 40.17 5.22
- sd504 - urgent care 40.54 5.27
- sd509 - urgent care 75.95 9.87
- sd541 - urgent care 193.14 25.11
- sd594 - urgent care 42.89 5.58
- sd606 - urgent care 43.31 5.63
- sleep - mslt 85.53 11.12
- sleep letter 4505.21 585.68
- sleep report 25243.47 3281.65
- speech report 357.73 46.5
- stat discharge summary 1148.06 149.25
- surgical hospitalist clinic note 1961.71 255.02
- urgent care 30221.61 3928.81
- urgent care - sd494 227.37 29.56
- urgent care - sd496 37.17 4.83
- urgent care - sd503 202.07 26.27
- urgent care - sd504 117.74 15.31
- urgent care - sd508 67.97 8.84
- urgent care - sd509 60.1 7.81
- urgent care - sd510 33.57 4.36
- urgent care - sd541 47.4 6.16
- urgent care - sd546 27.77 3.61
- urgent care - sd594 40.17 5.22
in databases (you didn't specify yours), use case
statement group values way want them, , use same case
statement in both select
, group by
clauses.
to started:
select case when r.reporttype 'sd%' 'urgent care' when r.reporttype in ('op note','sd805','polysomnography') 'op notes' else r.reporttype end reporttype, sum(cast(round(r.transcriptionlinecount,2) decimal(8,2))) "bill lines", (cast(round(sum(r.transcriptionlinecount) * .13,2) decimal(8,2))) "amount" rptlinecountinfo r join dictation d on d.dictationid = r.dictationid d.officeid = @officeid , r.finishedtime between @startdate , @enddate , (d.dictationstatus != 'd' , d.dictationstatus != 'q') group case when r.reporttype 'sd%' 'urgent care' when r.reporttype in ('op note','sd805','polysomnography') 'op notes' else r.reporttype end
i should warn above logic in case
statement broken. reason there inconsistency in requirement.
on 1 hand, report types follow pattern sd###
should mapped urgent care
. sd805
? want 1 mapped op notes
, it's valid urgent care
per specification. you'll have figure want in case. if have 1 exception, maybe need reorder conditions exceptional case checked first.
also, represent sd###
requirement, translated standard like 'sd%'
, may match patterns sd5
, or sd
, or sddfgdfg
. if not want, appropriate regex statement database, , change that.
Comments
Post a Comment