0

I am trying to query a database that has these params:

Transaction Date, User Email Address

What I have done is use this query:

SELECT [User Email Address],  COUNT(*) AS 'count'
    FROM
      [DATABASE].[TABLE]
    GROUP BY [User Email Address]

which displays a table with params:

Email Address, count

In this case the count column shows the number of occurrences of the user email in the original table.

What I am trying to do next is look at the Transaction Date column for the last year up to today, and compare the count column for this subset with the count column of the orginal (which goes back some 3 years). Specifically, I want my end resultant table to be:

User User Email Address, countDiff

where countDiff is the difference in counts from the one year subset and the original subset.

I have tried:

SELECT [User Email Address], [Transaction Date], [count - COUNT(*)] AS 'countdDifference'

FROM (

    SELECT [User Email Address],  COUNT(*) AS 'count'
    FROM
      [DATABASE].[TABLE]
    GROUP BY [User Email Address]


) a

WHERE a.[Transaction Date] >= '2011-08-07 00:00:00.000'


ORDER BY [count] DESC

But I get the error that [Transaction Date] is not in the Group By clause or aggregate. If I put it in the Group By next to [User Email Address], it messes up the data.

This is actually a common problem I've had. Any ways to circumvent this?

4

4 回答 4

3

您需要使用两个不同的子查询:一个计算完整条目,另一个计算去年的条目。

也许这会帮助你:

SELECT a.*, a.[count] - Coalesce(b.[count], 0) as 'countDif' 
FROM 
    (
        SELECT [User Email Address],  COUNT(*) AS 'count'
        FROM [DATABASE].[TABLE]
        GROUP BY [User Email Address]
    ) AS a
    LEFT JOIN (
        SELECT [User Email Address],  COUNT(*) AS 'count'
        FROM [DATABASE].[TABLE]
        WHERE [Transaction Date] >= '2011-08-07 00:00:00.000'
        GROUP BY [User Email Address]
    ) AS b ON a.[User Email Address] = b.[User Email Address]
于 2013-08-07T19:22:45.717 回答
2

你可以使用这个:

SELECT [User Email Address], 
       [Transaction Date], 
       count1.count - isnull(count2.count,0) as  [countdDifference]
FROM    (SELECT [User Email Address],  COUNT(*) AS 'count'
        FROM    [DATABASE].[TABLE] t1
        GROUP BY t1.[User Email Address]) as count1
LEFT JOIN (SELECT [User Email Address],  COUNT(*) AS 'count'
        FROM    [DATABASE].[TABLE] t2
        GROUP BY [User Email Address] 
        WHERE t2.[Transaction Date] >= '2011-08-07 00:00:00.000') as count2
ON      count2.[User Email Address] = count1.[User Email Address]
ORDER BY 3 DESC

您还应该开始考虑 1.) 不使用count,因为它已经使用了一堆(它几乎是一个保留字,但不完全是);2.) 不要在字段名称中使用空格;3.) 找到一种易于阅读的方式来组织你的 SQL :)

于 2013-08-07T19:26:34.053 回答
2

您可以在一个 SELECT 中完成这两个计数:

SELECT [User Email Address],  
    SUM(CASE WHEN [Transaction Date] >= '2011-08-07' THEN 1 ELSE 0 END) AS 'FilteredCount',
    COUNT(*) AS 'TotalCount',
    COUNT(*) 
       - SUM(CASE WHEN [Transaction Date] >= '2011-08-07' THEN 1 ELSE 0 END)
         AS 'CountDifference'
    FROM
      [DATABASE].[TABLE]
    GROUP BY [User Email Address]
于 2013-08-07T19:22:39.210 回答
1

,这样的事情怎么样?

SELECT
    [User Email Address],
    count(*) AS Total,
    sum(CASE 
            WHEN [Transaction Date] >= '2011/08/07 00:00:00.000' THEN 1
            ELSE 0 
         END) AS WithinDateRange,
    count(*) 
        - sum(CASE 
                  WHEN [Transaction Date] >= '2011/08/07 00:00:00.000' THEN 1
                  ELSE 0 
              END) AS Difference.
FROM [DATABASE].[TABLE]
GROUP BY [User Email Address]
于 2013-08-07T19:26:50.060 回答