1

如何使用SQL复制Excel 的模式功能?

如果我在 Excel 中对一组数字运行模式函数,即使有多个模式值,它也会返回一个模式值。我需要一些SQL才能以相同的方式工作。

对于这一系列数字,Excel 返回模式 8。这可能是因为 8 是第一个出现的模式数字。

6
7
8
3
3
8
0
2
2

如果没有模式示例,所有数字都是唯一的,它应该返回 NA。

这是我到目前为止的代码。

如何使用SQL复制Excel 的模式功能?如果我在 Excel 中对一组数字运行模式函数,即使有多个模式值,它也会返回一个模式值。我需要一些SQL才能以相同的方式工作。

这就是我到目前为止所拥有的。删除occurences=1要处理没有模式的系列的行。

 --I wanted to use CTE for Mode, but it won't work as part of a union query
select RIC,Period,InputFile,Occurrences,amount into #Mode1 from
(SELECT aa.RIC,aa.Period,aa.inputfile,aa.amount,COUNT(*) AS occurrences
FROM tempPivotTable aa
--where aa.ric='USTRDAP' and aa.period='2006' and aa.inputfile='C:\FalconIngest\Input\US April 2006.xls'
GROUP BY aa.RIC,aa.Period,aa.inputfile,aa.amount) as A

Select RIC,vendor,Period,Filedate,inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,MaxAmount into #Mode2 from
(
select t.Ric,'O' as vendor,t.Period,Filedate,t.inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,max(occurrences) as MaxAmount
from TempPivotTable t
inner join #Mode1 A
on t.ric=a.ric and t.period=a.period and t.inputfile=a.inputfile
group by t.Ric,t.Period,Filedate,t.inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate
)as A

Select RIC,vendor,Period,Filedate,inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,Amount,occurrences into #Mode3 from
(
select a.RIC, 'O' as vendor,a.period,Filedate,a.inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,Amount,occurrences
from #Mode1 A
inner join #Mode2 M
on A.ric=M.ric and A.period=M.period and A.inputfile=M.Inputfile
where occurrences=maxamount 
) as A

--deal with cases where there is no mode
select ric,vendor,period,Filedate,inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,Amount
into #mode4 
from(
select   ric,'O' as vendor,period,Filedate,inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,0 as Amount from #mode3
where occurrences=1 
group by ric,period,Filedate,inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate
having count(*)>1
) as A
delete from #mode3 where occurrences=1 

select a.RIC, 'O' as vendor,a.period,Filedate,a.inputfile,YearlyOrQuarterly,sortablePeriod,NumericFiledate,Amount
from #Mode1 A
inner join #Mode2 M
on A.ric=M.ric and A.period=M.period and A.inputfile=M.Inputfile
where occurrences=maxamount and maxamount>1
union select * from #mode4  --Put series with no mode as NA

drop table #mode1
drop table #mode2
drop table #mode3
drop table #mode4

随后,我想出了这个简化的代码。

select Code,inputfile,period,Amount,count(*) as Amountcount,
Ranking=dense_Rank() over (partition by Code,period,inputfile order by count(*) desc)
from TempPivotTable
group by Code,inputfile,period,Amountmore of those amount is the modal amount.  

有一个众数值是可以的。在下面的示例中,3 和 8 是众数值。如果有多个模式值,它必须选择 8,因为它首先出现在供应商的字母列表中。

供应商金额 A 6 B 7 C 8 D 3 E 3 F 8 G 0 H 2 I 2

4

1 回答 1

0

下面的语句返回模式值,如果它是单一的,第一个(按字母顺序)供应商的模式值如果设置是多模式的,如果所有值都是唯一的(不存在模式),则返回 NULL。

;with m as (
    select *,
        aCnt = count(1) over (partition by amount),
        tCnt = count(1) over ()
    from TableName
)
select top (1)
    case
        when acnt = 1 and tCnt > 1 then NULL
        else amount
    end as mode
from m
order by acnt desc, vendor;

SQLFiddle 示例

要在元组中查找模式,(inputfile, code, period)您可以尝试:

;with r1 as (
    select inputfile, code, period, vendor, amount,
        acnt = count(1) over (partition by inputfile, code, period, amount),
        tcnt = count(1) over (partition by inputfile, code, period)
    from TableName
),
r2 as (
    select inputfile, code, period, amount, acnt, tcnt,
        rn = row_number() over (partition by inputfile, code, period order by acnt desc, vendor)
    from r1
)
select inputfile, code, period,
    mode = case when acnt = 1 and tcnt > 1 then NULL else amount end
from r2
where rn = 1

SQLFiddle 示例

于 2013-08-08T18:28:27.670 回答