SQL group by issue

时间:2011-06-12 23:47:13

标签: sql sql-server

这是我遇到的问题。这是一个例子:

DateTime               Campaign
2011-06-01 08:55:00    Campaign1
2011-06-01 08:56:00    Campaign1
2011-06-01 08:57:00    Campaign2
2011-06-01 08:58:00    Campaign1
2011-06-01 08:59:00    Campaign1

我想按广告系列分组,但只要它是相同的。 我期望的结果是:

DateTime               Campaign    Count
2011-06-01 08:55:00    Campaign1       2
2011-06-01 08:57:00    Campaign2       1
2011-06-01 08:58:00    Campaign1       2

每当我对广告系列进行分组时,我只获得一行广告系列

Select *, Count(Campaign) as Count from myTable group by Campaign

结果

DateTime               Campaign    Count
2011-06-01 08:55:00    Campaign1       4
2011-06-01 08:57:00    Campaign2       1

您是否知道如何获得理想的结果?

谢谢!

4 个答案:

答案 0 :(得分:4)

没有办法在标准ANSI SQL语句中执行您要查找的内容。上面提到的关于存储过程或使用游标的内容可以完成 - 但您也可以使用游标将语句编写到T-SQL或PL / SQL中,以确定每个从开始时间和计数开始的数量 - 如下所示T-SQL(Microsoft SQL服务器):

USE [<database name here>]
DECLARE @tbl_result TABLE (Result_DT DATETIME, Campaign VARCHAR(25), unit_count int);
DECLARE @start_dt DATETIME,
        @current_campaign VARCHAR(25), --This can be any length needed - if your campaigns are over 25 chars long
        @record_dt DATETIME,
        @campaign VARCHAR(25),
        @cur_records CURSOR,
        @Record_Count INT;
SET @current_campaign = 'Start'
SET @cur_records = CURSOR
FOR SELECT DateTime, Campaign FROM myTable order by 1,2

OPEN @cur_records

FETCH NEXT FROM @cur_records INTO @record_dt, @campaign
WHILE @@FETCH_STATUS = 0
    BEGIN
       -- CHECK THE record to the Current Campaign for grouping
        IF @campaign <> @current_campaign
            BEGIN
              If @current_campaign <> 'Start'
                BEGIN
                    INSERT INTO @tbl_result 
                    VALUES (@start_dt, @current_campaign, @Record_Count)
                END
            SET @current_campaign = @campaign
            SET @start_dt = @record_dt
            SET @Record_Count = 1
            END
        ELSE
            BEGIN
                SET @Record_Count = @Record_Count + 1
            END
    END
    INSERT INTO @tbl_result  -- Inserting the last of the Records
    VALUES (@start_dt, @current_campaign, @Record_Count)
-- Now to display the results
SELECT Result_DT, Campaign, unit_count FROM @tbl_result order by 1, 2

现在请原谅我,如果这个错误 - 但它应该是你需要的基本结构,如果它是一个Microsoft Box

答案 1 :(得分:2)

这是一个在SQL中很难解决的问题,并且在其他方​​面很容易解决。我甚至认为这不能在单个原始SQL查询中完成。问题是group by不是各列属性的集合,但也依赖于其他列。你最好用光标编写一个sproc或者在其他地方以编程方式编写它。

答案 2 :(得分:0)

您似乎希望按日期分组,然后按照广告系列进行分组:

SELECT DateTime, Campaign, Count(Campaign) as Count 
FROM myTable 
GROUP BY DateTime, Campaign

答案 3 :(得分:0)

我知道这不是你要求的,但是如果“Campaign”列不是唯一的,我会添加另一个具有每个Campaign的唯一标识符的表,例如自动递增的数据类型。这样,如果您有两个具有相同名称的广告系列,则无关紧要。

FirstTable

UniqueCampaign      CampaignName
1                   Campaign1
2                   Campaign2
3                   Campaign1

SecondTable

DateTime               UniqueCampaign      
2011-06-01 08:55:00    1
2011-06-01 08:56:00    1
2011-06-01 08:57:00    2
2011-06-01 08:58:00    3
2011-06-01 08:59:00    3

然后你可以轻松地使用像

这样的小组
SELECT MIN(DateTime) [DateTime], T1.UniqueCampaign, COUNT(*) CampaignCount
FROM SecondTable T1
INNER JOIN FirstTable T2
     ON T1.UniqueCampaign = T2.UniqueCampaign
Group By T1.UniqueCampaign

哪会给你

DateTime                    UniqueCampaign  CampaignCount
2011-06-01 08:55:00.000     1               2
2011-06-01 08:57:00.000     2               1
2011-06-01 08:58:00.000     3               2