SQL 我如何根据条件对记录进行分组?
SQL How would I group records based on conditions?
我正在尝试查找总共拥有至少 4 个帐户且至少有 1 个帐户在过去 6 个月内开设的所有客户。
到目前为止,我能够获得客户和总帐户数,但我不确定如何为过去 6 个月内开设的新帐户数添加一列。
SELECT CustomerID
,COUNT(AccountID) as 'Total Accounts'
FROM Customer
GROUP BY CustomerID
HAVING COUNT(AccountID) >= 3`
这是我的表格:
和
我的最终结果应该是这样的。到目前为止,我有 CustomerID 和 Total Accounts。
CustomerID | Total Accounts | New Accounts |
-----------+----------------+------------- +
Ben | 3 | 1 |
编辑:我将在其中导入此查询的程序不允许 case 语句。
此查询将为您提供帐户总数和新帐户数。
SELECT C.CustomerID,
COUNT(C.AccountID) as Total_Acc,
SUM(CASE WHEN DATEADD(MONTH,-6,GETDATE()) <= A.StarDate) THEN 1 ELSE 0 END) as New_Acct
FROM Customer C
LEFT JOIN Account A ON C.AccountID = A.AccountID
GROUP BY CustomerID
然后您可以在子查询中使用它来查找您想要的内容
例如
SELECT *
FROM (
SELECT C.CustomerID,
COUNT(C.AccountID) as Total_Acc,
SUM(CASE WHEN DATEADD(MONTH,-6,GETDATE()) <= A.StarDate) THEN 1 ELSE 0 END) as New_Acct
FROM Customer C
LEFT JOIN Account A ON C.AccountID = A.AccountID
GROUP BY CustomerID
) SUB
WHERE Total_Acc >= 4 and New_Acct >= 1
Count
,像大多数聚合函数一样,忽略 null
s。我将通过连接两个表并使用 case
表达式来使用此 属性 仅 return 新帐户:
SELECT CustomerId,
COUNT(*) AS TotalAccounts,
COUNT(CASE WHEN DATEDIFF(MONTH, StartDate, GETDATE()) <= 6 THEN 1 END)
AS NewAccounts
FROM Customer c
JOIN Accounts a ON c.AccountId = a.AccountId
GROUP BY CustomerId
HAVING COUNT(*) >= 4 AND
COUNT(CASE WHEN DATEDIFF(MONTH, StartDate, GETDATE()) <= 6 THEN 1 END) > 0
The program I'll be importing this query in doesn't allow case
statements.
对于您的既定目标,即寻找至少拥有 4 个帐户且至少有 1 个帐户在过去 6 个月内开设的客户,您无论如何都不需要最近帐户的数量。获取最新的开户日期并查看是否在最近 6 个月内就足够了。
SELECT c.CustomerId
FROM Customer c
JOIN Accounts a
ON c.AccountId = a.AccountId
GROUP BY c.CustomerId
HAVING COUNT(*) >= 4
AND MAX(a.StartDate) >= DATEADD(MONTH, -6, GETDATE())
您可以离开加入新帐户。在连接条件中包含 startdate
的过滤器。对于不满足该条件的帐户,不会加入来自 account
的记录。因此帐户 ID 对他们来说是空的,不会被计算在内。
SELECT c.customerid,
count(c.accountid) "Total Accounts",
count(a.accountid) "New Accounts"
FROM customer c
LEFT JOIN account a
ON a.accountid = c.accountid
AND a.startdate >= dateadd(month, -6, getdate())
GROUP BY c.customerid
HAVING count(c.accountid) >= 4
AND count(a.accountid) >= 1;
我正在尝试查找总共拥有至少 4 个帐户且至少有 1 个帐户在过去 6 个月内开设的所有客户。
到目前为止,我能够获得客户和总帐户数,但我不确定如何为过去 6 个月内开设的新帐户数添加一列。
SELECT CustomerID
,COUNT(AccountID) as 'Total Accounts'
FROM Customer
GROUP BY CustomerID
HAVING COUNT(AccountID) >= 3`
这是我的表格:
和
我的最终结果应该是这样的。到目前为止,我有 CustomerID 和 Total Accounts。
CustomerID | Total Accounts | New Accounts |
-----------+----------------+------------- +
Ben | 3 | 1 |
编辑:我将在其中导入此查询的程序不允许 case 语句。
此查询将为您提供帐户总数和新帐户数。
SELECT C.CustomerID,
COUNT(C.AccountID) as Total_Acc,
SUM(CASE WHEN DATEADD(MONTH,-6,GETDATE()) <= A.StarDate) THEN 1 ELSE 0 END) as New_Acct
FROM Customer C
LEFT JOIN Account A ON C.AccountID = A.AccountID
GROUP BY CustomerID
然后您可以在子查询中使用它来查找您想要的内容
例如
SELECT *
FROM (
SELECT C.CustomerID,
COUNT(C.AccountID) as Total_Acc,
SUM(CASE WHEN DATEADD(MONTH,-6,GETDATE()) <= A.StarDate) THEN 1 ELSE 0 END) as New_Acct
FROM Customer C
LEFT JOIN Account A ON C.AccountID = A.AccountID
GROUP BY CustomerID
) SUB
WHERE Total_Acc >= 4 and New_Acct >= 1
Count
,像大多数聚合函数一样,忽略 null
s。我将通过连接两个表并使用 case
表达式来使用此 属性 仅 return 新帐户:
SELECT CustomerId,
COUNT(*) AS TotalAccounts,
COUNT(CASE WHEN DATEDIFF(MONTH, StartDate, GETDATE()) <= 6 THEN 1 END)
AS NewAccounts
FROM Customer c
JOIN Accounts a ON c.AccountId = a.AccountId
GROUP BY CustomerId
HAVING COUNT(*) >= 4 AND
COUNT(CASE WHEN DATEDIFF(MONTH, StartDate, GETDATE()) <= 6 THEN 1 END) > 0
The program I'll be importing this query in doesn't allow case statements.
对于您的既定目标,即寻找至少拥有 4 个帐户且至少有 1 个帐户在过去 6 个月内开设的客户,您无论如何都不需要最近帐户的数量。获取最新的开户日期并查看是否在最近 6 个月内就足够了。
SELECT c.CustomerId
FROM Customer c
JOIN Accounts a
ON c.AccountId = a.AccountId
GROUP BY c.CustomerId
HAVING COUNT(*) >= 4
AND MAX(a.StartDate) >= DATEADD(MONTH, -6, GETDATE())
您可以离开加入新帐户。在连接条件中包含 startdate
的过滤器。对于不满足该条件的帐户,不会加入来自 account
的记录。因此帐户 ID 对他们来说是空的,不会被计算在内。
SELECT c.customerid,
count(c.accountid) "Total Accounts",
count(a.accountid) "New Accounts"
FROM customer c
LEFT JOIN account a
ON a.accountid = c.accountid
AND a.startdate >= dateadd(month, -6, getdate())
GROUP BY c.customerid
HAVING count(c.accountid) >= 4
AND count(a.accountid) >= 1;