例如,如果我有这个查询:
select top 10 medication_name, count(1)
from DimMedical
group by medication_name
order by count(1) desc
我可以很容易地统计出最受欢迎的十个药方。
我可以像这样用 Union 添加“其他所有内容”:
select top 10 medication_name, count(1)
from DimMedical
group by medication_name
order by count(1) desc
union
select 'other', count(1)
from DimMedical
where medication_name not in (
select top 10 medication_name, count(1)
from DimMedical
group by medication_name
order by count(1) desc)
但这把我的桌子打了三遍!它也很难支持,因为相同的 where 子句必须在子查询中。下一个开发人员很容易更改一个而不是另一个。
https://www.brentozar.com/pastetheplan/?id=Bk4IqFo9m
问题:什么是获得前 N 项的最佳方法,然后是其他所有内容?
样本数据(神圣的空白!我有一些规范化要做!):
create table DimMedical (medication_name varchar(255))
insert into DimMedical(medication_name)
values ('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('METHOCARBAMOL '),
('IBUPROFEN '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('ZIPSOR '),
('CELEBREX '),
('GABAPENTIN '),
('MELOXICAM '),
('TRAMADOL HCL '),
('BACLOFEN '),
('XODOL '),
('NAPROXEN '),
('CELEBREX '),
('CLONAZEPAM '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('LYRICA '),
('LIDODERM '),
('AMOXICILLIN/CLAVULANATE POTASSIUM '),
('CIPROFLOXACIN '),
('CLINDAMYCIN HCL '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('LYRICA '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('CELEBREX '),
('DICLOFENAC SODIUM '),
('ACETAMINOPHEN-CODEINE PHOSPHATE '),
('VOLTAREN GEL '),
('ZIPSOR '),
('FENTANYL CITRATE '),
('OPANA ER '),
('CLARINEX '),
('NASONEX '),
('IPRATROPIUM BROMIDE-ALBUTEROL SULFA'),
('PROAIR HFA '),
('ADVAIR DISKUS 250/50 '),
('SPIRIVA '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('IBU '),
('GABAPENTIN '),
('NAPROXEN '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('TIZANIDINE HCL '),
('GABAPENTIN '),
('CYCLOBENZAPRINE HCL '),
('ENDOCET '),
('ENDOCET '),
('ZOLPIDEM TARTRATE '),
('KADIAN '),
('TRAMADOL HCL '),
('CYCLOBENZAPRINE HCL '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('TRAMADOL HCL '),
('GABAPENTIN '),
('TRAMADOL HCL '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('NAPROXEN '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('ZYPREXA '),
('LEXAPRO '),
('AMITRIPTYLINE HCL '),
('CYMBALTA '),
('MIRTAZAPINE '),
('LIDODERM '),
('KADIAN '),
('MELOXICAM '),
('CYCLOBENZAPRINE HCL '),
('GABAPENTIN '),
('TRAZODONE HYDROCHLORIDE '),
('DIAZEPAM '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('DEMEROL '),
('TRAMADOL HCL '),
('AMITRIPTYLINE HCL '),
('LITHIUM CARBONATE '),
('ENDOCET '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('Unknown'),
('NASONEX '),
('XOPENEX HFA '),
('ACETAMINOPHEN-CODEINE PHOSPHATE '),
('MELOXICAM '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('OXYCODONE HCL '),
('OXYCONTIN '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('CARISOPRODOL '),
('OXYCODONE HCL-ACETAMINOPHEN '),
('METHOCARBAMOL '),
('OXYCONTIN '),
('AMITRIPTYLINE HCL '),
('HYDROCODONE BITARTRATE-ACETAMINOPHE'),
('HYDROXYZINE PAMOATE '),
('ALPRAZOLAM '),
('CARISOPRODOL '),
('CITALOPRAM HYDROBROMIDE ')
这对你有用吗?
使用 Mathewb 关于 case 语句和 row_number() 函数的建议,我已经像这样重写了查询。它只打到桌子上一次: