获取最多和最少2列SQL Server 2008 R2

时间:2017-06-06 22:06:31

标签: sql sql-server

此查询是存储过程的最后一部分,它以这种方式向我显示结果:

;with final_comparativo as (
                    select s.CeEmplazamiento, avg(s.Totales) TotalGeneral , s2.TotGral, @Mes as Mes
                    from tmpSemanas s
                        cross join ( select avg(Totales) TotGral from tmpSemanas s2 where Totales >0) s2
                    group by s.CeEmplazamiento , s2.TotGral
            union all
                    select ss.CeEmplazamiento, avg(ss.Totales) TotalGeneral , ss2.TotGral, @Mes_comparar as Mes
                    from tmpSemanas_comparar ss
                        cross join ( select avg(Totales) TotGral from tmpSemanas_comparar ss2 where Totales >0) ss2
                    group by ss.CeEmplazamiento , ss2.TotGral
        )                       


        --insert #tmpDatos
        , maximo_minimo as (
            select CeEmplazamiento,
                max(case when Mes  = @Mes then TotalGeneral else 0 end) as Mes_actual,
                max(case when Mes  = @Mes_comparar then TotalGeneral else 0 end) as Mes_comparar
            from final_comparativo
            where TotalGeneral <> 0
            group by CeEmplazamiento    
            )

            select * from maximo_minimo
            drop table #tmpDatos

这是当前的结果表

CeEmplazamiento Month_current Month_compare
Celaya          76.500000       75.600000
Coecillo        79.000000       79.800000
Irapuato        77.500000       75.400000
León Sur        85.750000       87.600000
Oriente León    86.250000       85.200000

存储过程会询问您对应于month_current(原产月)month_compare的2个参数(与您想要比较month_current的月份相比),因此它已经是最终计算。但我的问题在于以下内容,我希望这个表格取当前月份的最大值和最小值,同样对于列#34; Month_compare&#34;,我希望我已经解释过了。更简单的说,我希望它保持这种状态。

CeEmplazamiento       Month_current Month_compare
Leon Sur              85.75        87.60
Celaya                76.50         null
Irapuato               null        75.40

就你所知,它与所有CeEsmplazamiento的数量不同,那些不在当前月份的,不会显示,但会被考虑用于正在完成的图形。我希望你能帮助我

1 个答案:

答案 0 :(得分:1)

像这样的Smth:

    declare @t table(CeEmplazamiento varchar(100),  Month_current money, Month_compare money);
    insert into @t values
    ('Celaya',          76.500000,       75.600000),
    ('Coecillo',        79.000000,       79.800000),
    ('Irapuato',        77.500000,       75.400000),
    ('León Sur',        85.750000,       87.600000),
    ('Oriente León',    86.250000,       85.200000);

    with cte1 as
    (
    select top 1 CeEmplazamiento, Month_current, null as Month_compare
    from @t 
    order by  Month_current asc
    )

    ,cte2 as
    (
    select top 1 CeEmplazamiento, Month_current, null as Month_compare
    from @t 
    order by  Month_current desc
    )

    ,cte3 as
    (
    select top 1 CeEmplazamiento, null as Month_current, Month_compare
    from @t 
    order by  Month_compare asc
    )

    ,cte4 as
    (
    select top 1 CeEmplazamiento, null as Month_current, Month_compare
    from @t 
    order by  Month_compare desc
    )

    select *
    from cte1 
    union all
    select *
    from cte2
    union all
    select *
    from cte3 
    union all
    select *
    from cte4