转换sql查询结果的位置

时间:2014-10-30 11:16:02

标签: sql-server select case

我知道这个问题似乎有点令人困惑。我有一个返回以下结果的查询:

enter image description here

有没有办法改变结果,所以我可以有类似的东西:所有空值将被下面的值替换?只有客房服务midi有一个空值,所有其余的都被移位。有没有办法取代它。

我的查询:

select pc.code, pc.description,
CASE 
                           WHEN g.description like 'NOURRITURE' and pc.description like 'PETIT DEJEUNER' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'BANQUET' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'CASABLANCA CAFE MIDI' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'CASABLANCA CAFE MATIN' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'CASABLANCA CAFE SOIR' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'CASABLANCA CAFE BAR' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'ROOM SERVICE NUIT' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'ROOM SERVICE MIDI' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'ROOM SERVICE SOIR' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'ROOM SERVICE PDJ' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'XO LOUNGE/MIDI' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'BANQUET LUNCH' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'BANQUET DINNER' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'TABAC' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'XO LOUNGE/SOIR' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'Terasse Bleue' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'SPA' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'Seven' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'MINI BAR' THEN COALESCE(sum(ta.price),0)
                           WHEN g.description like 'NOURRITURE' and pc.description like 'CHEZ DADA' THEN COALESCE(sum(ta.price),0)

                        END  as SumPriceFood,

                        CASE 
                           WHEN g.description like 'BOISSON' and pc.description like 'PETIT DEJEUNER' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'BANQUET' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'CASABLANCA CAFE MIDI' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'CASABLANCA CAFE MATIN' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'CASABLANCA CAFE SOIR' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'CASABLANCA CAFE BAR' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'ROOM SERVICE NUIT' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'ROOM SERVICE MIDI' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'ROOM SERVICE SOIR' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'ROOM SERVICE PDJ' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'XO LOUNGE/MIDI' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'BANQUET LUNCH' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'BANQUET DINNER' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'TABAC' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'XO LOUNGE/SOIR' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'Terasse Bleue' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'SPA' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'Seven' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'MINI BAR' THEN sum(ta.price)
                           WHEN g.description like 'BOISSON' and pc.description like 'CHEZ DADA' THEN sum(ta.price)

                        END  as SumPriceBoisson
                           from trans_articles ta, transactions t, profit_centers pc, article_causals ac, groups g, articles a
                           where t.id = ta.transaction_id
                           and pc.id = t.profit_center_id 
                           and ta.causal_id = ac.id
                           and g.id = a.group_a_id
                           and a.id = ta.article_id 
                           and ac.causal_type = 1                         
                           and ac.code in(103)
                           group by pc.code, pc.description, g.description

感谢您阅读

1 个答案:

答案 0 :(得分:0)

尝试以下方法;

SELECT   code
        ,description
        ,SUM(sumpricefood) sumpricefood
        ,SUM(sumpriceboisson) AS sumpriceboisson
GROUP BY code
        ,description

根据您需要group结果所需的代码和说明,将结果转换为一起。