[英]DB2 SELECT statement using COUNT and GROUP BY
I have a table in a DB2 database containing customer information that I need to retrieve along with a count of how many times a specific column in the table is duplicated.我在 DB2 数据库中有一个表,其中包含我需要检索的客户信息以及表中特定列重复次数的计数。 Here is an example of the data.
这是数据的示例。
CUSTOMERID | CUSTOMERGROUP | PRODUCTID | PRODUCTNAME | ALERTNAME | ALERTLEVEL | XXX | YYY | ZZZ
12345 ABC 987654 ProductA Alert1 4 More Data Here
A customer is identified by the CustomerID and CustomerGroup columns.客户由 CustomerID 和 CustomerGroup 列标识。 They can have any number of products and these products get different types of alerts (ProductA, ProductC and ProductQ could all get Alert1).
他们可以拥有任意数量的产品,这些产品会收到不同类型的警报(ProductA、ProductC 和 ProductQ 都可以收到 Alert1)。 I need to retrieve the customer information for each row along with a count of how many times that customer got a specific alertname.
我需要检索每一行的客户信息以及该客户获得特定警报名称的次数。
In our old MySQL database, this was not too difficult as I would do something like this在我们旧的 MySQL 数据库中,这并不太难,因为我会做这样的事情
SELECT customerID, customerGroup, ProductID, ProductName, AlertName, count(AlertName), AlertLevel, more data....
FROM TABLE
WHERE customerID = XXX and customerGroup = YYY
GROUP BY alertname
ORDER BY AlertLevel, AlertName, ProductName
The group by did not include every column in the select statement so I would get back rows for the customer that included the customer information and a count of the number of times they received a specific alert. group by 没有包含 select 语句中的每一列,因此我会为客户获取包含客户信息和他们收到特定警报次数的计数的行。
Now that we have migrated to DB2, I am required to put every column from the SELECT into the GROUP BY and this (obviously) makes each row distinct and therefore the count is now returning 1 for every row regardless of whether an alert name matches another row for this customer.现在我们已经迁移到 DB2,我需要将 SELECT 中的每一列都放入 GROUP BY 中,这(显然)使每一行都不同,因此无论警报名称是否与另一个名称匹配,现在每行的计数都返回 1此客户的行。
Is there a way to recreate this functionality that will not require a significant overhaul of the way data is retrieved?有没有一种方法可以重新创建此功能,而无需对检索数据的方式进行重大修改? As the developer of the front end, I have the ability to change the way I manipulate data on the PHP side and I can write SQL statements but I have no option for changing the way the data is stored.
作为前端的开发人员,我有能力改变我在 PHP 端操作数据的方式,我可以编写 SQL 语句,但我没有改变数据存储方式的选项。
You can do what you want with analytic functions: 您可以使用解析函数执行所需的操作:
SELECT customerID, customerGroup, ProductID, ProductName, AlertName, AlertCount,
AlertLevel, more data....
FROM (SELECT t.*,
COUNT(*) OVER (PARTITION BY AlertName) as AlertCount,
ROW_NUMBER() OVER (PARTITION BY AlertName ORDER BY customerID) as seqnum
FROM TABLE t
WHERE customerID = XXX and customerGroup = YYY
) t
WHERE seqnum = 1
ORDER BY AlertLevel, AlertName, ProductName;
You can compute counts in separate query and then join it to the original query: 您可以在单独的查询中计算计数,然后将其加入到原始查询中:
SELECT customerID, customerGroup, ProductID, ProductName, AlertName, t2.alert_count as AlertName, AlertLevel, more data....
FROM TABLE t1 JOIN (
SELECT customerid, customergroup, count(AlertName) alert_count
FROM table
GROUP BY alertname) t2
ON t1.customerid = t2.customerid
AND t1.customergroup = t2.customergroup
WHERE customerID = XXX
AND customerGroup = YYY
ORDER BY AlertLevel, AlertName, ProductName
In DB2SQL, group by columns should be in select as well .在 DB2SQL 中,按列分组也应该在 select 中。 However, there is Query/400 that can be used to achieve such goal.
但是,可以使用 Query/400 来实现这样的目标。 In order to do that, ensure
为此,请确保
are properly selected.被正确选择。 Save this query and call it, like runqry for example.
保存此查询并调用它,例如 runqry。
OR try this:或者试试这个:
With CTE1 (customerid, customergroup, Nbr_of_rows) as (
SELECT t1.customerid, t1.customergroup, count(t1.AlertName)
FROM table t1
WHERE t1.customerID = selected_Id
AND t1.customerGroup = Selected_grp)
select * from CTE1
join table t2 on customerid = t2.customerid and customergroup = t2.customergroup
Order by columns_of_your_choice;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.