使用SQL复制Excel模式功能?

时间:2013-08-07 11:06:26

标签: sql-server-2005 mode

如何使用 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

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

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 我2

1 个答案:

答案 0 :(得分: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 sample

要在(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 sample