简体   繁体   English

帮助DB2中的查询

[英]help with query in DB2

i would like your help with my query.I have a table employee.details with the following columns: branch_name, firstname,lastname, age_float. 我希望你的帮助我的查询。我有一个表employee.details与以下列:branch_name,firstname,lastname,age_float。

I want this query to list all the distinct values of the age_float attribute, one in each row of the result table, and beside each in the second field show the number of people in the details table who had ages less than or equal to that value. 我希望此查询列出age_float属性的所有不同值,结果表的每一行中的一个,并在第二个字段中的每一行旁边显示详细信息表中年龄小于或等于该值的人数。 Any ideas? 有任何想法吗? Thank you! 谢谢!

You can use OLAP functions: 您可以使用OLAP功能:

SELECT DISTINCT age_float, 
       COUNT(lastname) OVER(ORDER BY age_float) AS number
  FROM employee_details

COUNT(lastname) OVER(ORDER BY age_float) AS number orders rows by age, and returns employees count whose age <= current row age COUNT(lastname) OVER(ORDER BY age_float) AS number按年龄COUNT(lastname) OVER(ORDER BY age_float) AS number行,并返回年龄<=当前行年龄的员工数

or a simple join: 或简单的加入:

SELECT A.age_float, count(lastname)
  FROM (SELECT DISTINCT age_float FROM employee_details) A
JOIN employee_details AS ED ON ED.age_float <= A.age_float
GROUP BY A.age_float

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

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