mysql:计算每个组中的值的数量

时间:2016-11-30 11:23:53

标签: mysql group-by pivot-table

我有一个问题:

SELECT 
    count(session_id_open) as opens,
    count(session_id_visit) as visits,
    date_intervals_open,
    group_concat(date_intervals_visit)
FROM
    bla
GROUP BY date_intervals_open

我得到了下表。我需要的是找出group_concat中出现的每个值出现的百分比。所以,基本上,我需要计算每个组中的值的数量(date_intervals_visit)(data_intervals_open)

opens   visits  date_intervals_open group_concat        
213    5        day (12-16)             evening (17-21),evening (17-21),day (12-16),day (12-16),day (12-16)
113    0        evening (17-21)         NULL
11     0        late evening (22-00)    NULL
396    12       morning (5-11)          morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11),morning (5-11)
9      0        night (1-4)              NULL

这大约是我需要获得的表格。在第一个记录的晚上有40个因为"晚上(17-21)"出现两次,所有出现次数为5. 2/5 * 100 = 40

opens   visits  date_intervals_open evening(17-21)  day(12-16)  morning (5-11)  
213    5        day (12-16)             40          60        0
113    0        evening (17-21)         NULL        NULL      NULL
11     0        late evening (22-00)    NULL        NULL     NULL
396    12       morning (5-11)          0        0     100
9      0        night (1-4)             NULL

PS:我使用group_concat只是为了可视化我在那里的值。我没有必要使用它,因为之后解析它会是额外的努力。

3 个答案:

答案 0 :(得分:1)

您基本上需要一个支点,并进行一些进一步的计算。我的回答的基础来自pivoting records in MySQL上的以下优秀SO主题。我假设您有一定数量的date_intervals_visit值,因为这些值似乎涵盖了整天,因此我使用具有固定计数的条件计数方法。我会在示例代码中添加2个类别,您可以将其扩展为涵盖所有date_intervals_visit值。

SELECT 
    count(session_id_open) as opens,
    count(session_id_visit) as visits,
    date_intervals_open,
    round(sum(if(date_intervals_visit='morning (5-11)',1,0)) / count(session_id_visit) * 100,2) as `morning (5-11)`,
    round(sum(if(date_intervals_visit='day (12-16)',1,0)) / count(session_id_visit) * 100,2) as `day (12-16)`
FROM
    bla
GROUP BY date_intervals_open

如果date_intervals_open值可能有0次访问,那么您需要在表达式中检查0:

if(count(session_id_visit)=0, 0, <above formula>)

答案 1 :(得分:0)

SELECT 
    count(session_id_open) as opens,
    @visits := count(session_id_visit) as visits,
    date_intervals_open,
    ROUND(100 * SUM(date_intervals_visit = 'evening(17-21)') / @visits) AS 'evening(17-21)',
    ROUND(100 * SUM(date_intervals_visit = 'day (12-16)') / @visits) AS 'day (12-16)',
    ROUND(100 * SUM(date_intervals_visit = 'morning (5-11)') / @visits)'morning (5-11)',
FROM
    bla
GROUP BY date_intervals_open

答案 2 :(得分:-1)

使用这样的函数:

CREATE FUNCTION [dbo].[fn_SplitString](
    @InputStr   varchar(Max),
    @Seperator  varchar(10))
RETURNS @OutStrings TABLE (ItemNo int identity(1,1), Item varchar(256))

AS
BEGIN

    DECLARE @Str varchar(2000),
            @Poz int, @cnt int

    --DECLARE @OutStrings TABLE (Item varchar(2000))

    SELECT @Poz = CHARINDEX (@Seperator, @InputStr), @cnt = 0
    WHILE @Poz > 0 AND @cnt <= 10000
    BEGIN
        SELECT @Str = SubString(@InputStr, 1, @Poz - 1)
        INSERT INTO @OutStrings(Item) VALUES(@Str)

        SELECT @InputStr = Right(@Inputstr, Len(@InputStr) - (len(@Str) + len(@Seperator)))
        SELECT @Poz = CHARINDEX (@Seperator, @InputStr), @cnt = @cnt + 1
    END
    IF @InputStr <> ''
    BEGIN
        INSERT INTO @OutStrings(Item) VALUES(@InputStr)
    END

    RETURN
END

以下列方式:

SELECT  opens,
        visits,
        date_intervals_open,
        [evening(17-21)]/[All]*100 AS [evening(17-21)],
        [day(12-16)]/[All]*100 AS [day(12-16)],
        [morning (5-11)]/[All]*100 AS [morning (5-11)]
FROM
    (   
    SELECT 
        count(session_id_open) as opens,
        count(session_id_visit) as visits,
        date_intervals_open,
        (SELECT COUNT(Item) FROM [dbo].[fn_SplitString](LTRIM(RTRIM(group_concat(date_intervals_visit))), ',') WHERE item  = 'evening(17-21)') AS [evening(17-21)],
        (SELECT COUNT(Item) FROM [dbo].[fn_SplitString](LTRIM(RTRIM(group_concat(date_intervals_visit))), ',') WHERE item = 'day(12-16)') AS [day(12-16)],
        (SELECT COUNT(Item) FROM [dbo].[fn_SplitString](LTRIM(RTRIM(group_concat(date_intervals_visit))), ',') WHERE item = 'morning (5-11)') AS [morning (5-11)],
        (SELECT COUNT(Item) FROM [dbo].[fn_SplitString](LTRIM(RTRIM(group_concat(date_intervals_visit))), ',')) AS [All]
    FROM
        bla
    GROUP BY date_intervals_open
    )blabla