Sql server 2005 复制Excel';使用SQL的s模式函数?

Sql server 2005 复制Excel';使用SQL的s模式函数?,sql-server-2005,mode,Sql Server 2005,Mode,如何使用SQL复制Excel的模式函数 如果我在Excel中的一组数字上运行mode函数,即使有多个mode值,它也会返回一个mode值。我需要一些SQL以同样的方式工作 对于这一系列数字,Excel返回的模式为8。这可能是因为8是第一个出现的模态数 6 7 8 3 3 8 0 2 2 如果没有模式示例,所有数字都是唯一的,则应返回NA 这是我目前掌握的代码 如何使用SQL复制Excel的模式函数? 如果我在Excel中的一组数字上运行mode函数,即使有多个mode值,它也会返回一个mode

如何使用SQL复制Excel的模式函数

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

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

6
7
8
3
3
8
0
2
2
如果没有模式示例,所有数字都是唯一的,则应返回NA

这是我目前掌握的代码

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

这就是我目前所拥有的。删除
occurrences=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 E3 F8 G0 h2
I 2下面的语句返回模式值(如果是单模式),如果设置为多模式,则返回第一个(按字母顺序排列)供应商的模式值,如果所有值都唯一(不存在模式),则返回NULL

要在
(输入文件、代码、句点)
元组中查找模式,您可以尝试:

;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

我需要输入文件、代码和句点的每个组合的模式。每个输入文件都有许多代码。每个代码都有很多句点。每个句点都有自己的行,因此一个输入文件可以有50行。感谢并抱歉延迟。我刚刚修改了我的首选项,以便向我发送电子邮件通知
;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