SQL - 创建要在案例中使用但不希望按其分组的列

时间:2017-09-27 20:18:46

标签: sql group-by teradata transpose calculated-columns

我是SQL的初学者。我在Teradata中使用它。

我想按用餐时间分解食物和饮料数据。 基本上,首先我要按照他们的风格对餐馆进行分类。然后,我使用该风格和订单时间按用餐时间对订单数量进行分类。然后我为每个时间段创建单独的列(尝试取消数据透视)。

以下是我的代码的简化版本:

SELECT year AS year
, week AS week
, case
    when type like('Q%') then 'Q'
    when type like('T%') then 'T'
    else 'Other'
    end 
    AS style
, restaurant_id AS rest_id
, case WHEN style = 'T' then (
        case
            when cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '11:30:00' then 'Breakfast'
            when cast(order_time as time format 'HH:MI:SS') between '11:30:01' and '16:30:00' then 'Lunch'
            else 'Dinner'
            end
        )
        else (
        case
    when cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '10:00:00' then 'Breakfast'
    when cast(order_time as time format 'HH:MI:SS') between '10:00:01' and '14:59:00' then 'Lunch'
    else 'Dinner'
    end)
    end
    as meal_period
, count(distinct case when meal_period = 'Breakfast' then order_number else null end) as food_count_b
, count(distinct case when meal_period = 'Lunch' then order_number else null end) as food_count_l
, count(distinct case when meal_period = 'Dinner' then order_number else null end) as food_count_d
FROM table1
group by 1,2,3,4,5
order by 1,2,3,4,5

它产生一个这样的表:

year  week  style rest_id  meal_period  food_count_b  food_count_l  food_count_d
2017  1     T     1234     Breakfast    5
2017  1     T     1234     Lunch                      18
2017  1     T     1234     Dinner                                    17
2017  1     Q     9955     Breakfast    8
2017  1     Q     9955     Lunch                      21
2017  1     Q     9955     Dinner                                    24
2017  2     T     1234     Breakfast    4
2017  2     T     1234     Lunch                      20
2017  2     T     1234     Dinner                                    18
2017  2     Q     9955     Breakfast    6
2017  2     Q     9955     Lunch                      29
2017  2     Q     9955     Dinner                                    31

我真正喜欢的是没有meal_period列,我只是创建了它,以便更容易按用餐时间划分他们自己的列(food_count_b,food_count_l和food_count_d)。

理想情况下,我喜欢这样的表:

year  week  style rest_id  food_count_b  food_count_l  food_count_d
2017  1     T     1234     5             18            17
2017  1     Q     9955     8             21            24
2017  2     T     1234     4             20            18
2017  2     Q     9955     6             29            31

有关如何做到这一点的任何想法?谢谢!

2 个答案:

答案 0 :(得分:2)

你有两个选择..

1。)完全取出count(distinct case when meal_period = 'Breakfast' then order_number else null end) as food_count_b ,然后直接在您希望保留的其他列中使用该列中的逻辑。

例如,更改此内容:

count(distinct case when style = 'T' and cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '11:30:00' 
                    then order_number
                    when style <> 'T' and cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '10:00:00'
                    then order_number
                    else null end) as food_count_b

对于这样的事情(我不太了解你的逻辑,但你应该明白这一点):

SELECT year, week, style, rest_id, MAX(Food_Count_b), MAX(Food_Count_l), MAX(Food_Count_d)
FROM (
SELECT year AS year
, week AS week
, case
    when type like('Q%') then 'Q'
    when type like('T%') then 'T'
    else 'Other'
    end 
    AS style
, restaurant_id AS rest_id
, case WHEN style = 'T' then (
        case
            when cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '11:30:00' then 'Breakfast'
            when cast(order_time as time format 'HH:MI:SS') between '11:30:01' and '16:30:00' then 'Lunch'
            else 'Dinner'
            end
        )
        else (
        case
    when cast(order_time as time format 'HH:MI:SS') between '03:00:01' and '10:00:00' then 'Breakfast'
    when cast(order_time as time format 'HH:MI:SS') between '10:00:01' and '14:59:00' then 'Lunch'
    else 'Dinner'
    end)
    end
    as meal_period
, count(distinct case when meal_period = 'Breakfast' then order_number else null end) as food_count_b
, count(distinct case when meal_period = 'Lunch' then order_number else null end) as food_count_l
, count(distinct case when meal_period = 'Dinner' then order_number else null end) as food_count_d
FROM table1
group by 1,2,3,4,5
) a
GROUP BY year, week, style, rest_id

2.。)使您的整个查询成为派生表,然后更改外部选择:

open

答案 1 :(得分:0)

DECLARE @TMP_TABLE TABLE
(
    idx INT IDENTITY(1, 1) PRIMARY KEY,
    Column1 NVARCHAR(50),
    Column2 NVARCHAR(50),
    Column3 NVARCHAR(50)
)

INSERT INTO @TMP_TABLE (
                           Column1,
                           Column2,
                           Column3
                       )
VALUES (   N'Value1', -- Column1 - nvarchar(50)
           N'Value2', -- Column2 - nvarchar(50)
           N'Value 3'   -- Column3 - nvarchar(50)
       )

SELECT  [idx] = T.idx,
        [Column1] = T.Column1,
        [Column2] = T.Column2,
        [Column3] = T.Column3
FROM @TMP_TABLE T