我的SQL表设计是:
id,
name,
department,
status,
value
问题是从每个部门获取相同数量的记录。每次提取允许的最大行数限制为100行。假设表中有4个不同的部门(A,B,C和D)。每个部门都有几百条记录。因此,查询应该只为每个部门提取25条记录。如果不同的部门规模是三个,那么拆分应该是100/3。
我使用了以下查询。但它不是动态计算每个部门的行数。目前我使用了25个恒定值。
SELECT *
FROM (
SELECT dept.*,
ROW_NUMBER() OVER( PARTITION BY dept.department
ORDER BY dept.department, dept.id ) deptEntry
FROM TABLE_NAME dept
) dept
WHERE dept.deptEntry <=25
AND dept.status='ACTIVE'
AND rownum <=100;
答案 0 :(得分:0)
计算不同部门的数量,然后将行数除以该值:
splot 'free_energy.dat' using 1:2:(($1>0.9*$2 && $1<1.1*$2)?3:1/0) with pm3d
<强>更新强>:
SELECT *
FROM (
SELECT dept.*,
ROW_NUMBER() OVER( PARTITION BY department
ORDER BY department, id ) AS deptEntry,
COUNT( DISTINCT department ) OVER () AS num_dept
FROM TABLE_NAME dept
) dept
WHERE dept.deptEntry <= 100 / num_dept
AND dept.status='ACTIVE'
AND rownum <=100;