简体   繁体   English

我如何嵌套这些SQL语句而不会出现group by子句错误?

[英]How can i nest these SQL statements without a group by clause error?

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). 我接下来要做的是查看直到今天的最后一年的“交易日期”列,并将此子集的计数列与原始记录的计数列(可追溯到3年)进行比较。 Specifically, I want my end resultant table to be: 具体来说,我希望最终结果表为:

User User Email Address, countDiff 用户用户电子邮件地址countDiff

where countDiff is the difference in counts from the one year subset and the original subset. 其中countDiff是一年子集和原始子集的计数差。

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. 但是我收到[Transaction Date]不在Group By子句或聚合中的错误。 If I put it in the Group By next to [User Email Address] , it messes up the data. 如果我将其放在[User Email Address]旁边的分组依据中,则会弄乱数据。

This is actually a common problem I've had. 这实际上是我遇到的一个常见问题。 Any ways to circumvent this? 有什么办法可以避免这种情况?

You need to use two different subqueries: One that counts the full entries and another one that counts the entries of the last year. 您需要使用两个不同的子查询:一个对完整条目进行计数,另一个对上一年的条目进行计数。

Maybe this will help you: 也许这会帮助您:

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]

You can do both counts in one SELECT: 您可以在一个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]

You can use this: 您可以使用此:

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

You should also start thinking about 1.) not using count as it's used a bunch (it's almost a reserved word but not quite); 您还应该开始考虑1.)不使用count因为它使用了一堆(几乎是保留字,但不完全是); 2.) don't use spaces in your field names; 2.)请勿在字段名称中使用空格; 3.) finding a easy-to-read way to organize your SQL :) 3.)寻找一种易于阅读的方式来组织您的SQL :)

,How about something like this? ,这样的事情怎么样?

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM