[英]Why Is My Count Statement Returning 0 Rows
我有這個 postgresql 語法
CREATE TABLE Testing (
salesID integer,
datePO date,
empID char(10),
saleDate date
);
INSERT INTO Testing(salesID, datePO, empID, saleDate) VALUES
('11991111', NULL, '234510', '2020-08-20'),
('11992222', NULL, '234510', '2020-08-21'),
('11994444', NULL, '234510', '2020-08-21'),
('86432181', NULL, '841321', '2020-08-25');
我想運行這個語句
Select
salesID
,datePO
,empId
,saleDate
From Testing
Where saleDate between '2020-08-01' and '2020-08-30'
GROUP BY salesID, datePO, empId, saleDate
Having Count(empID) > 1
但我得到0
結果返回。 這讓我感到困惑,因為如您所見,應該返回Insert
前三行,因為它們符合標准。 我需要更改什么才能返回這些行?
如果有幫助,這里是我的https://www.db-fiddle.com/f/sFN1GEEhCh7f6wcFreukhS/0的小提琴
您有一個group by
,因此您的查詢正在檢查group by
中的四個鍵在哪里相同。
它們唯一地標識每一行,因此任何組都不會超過一行。
你並沒有真正解釋你想要實現的邏輯,所以很難提供建議。
嗯。 我猜你想要empId
出現不止一次。 為此,使用窗口函數:
Select t.*
from (select t.*, count(*) over (partition by cmpid) as cnt
from Testing t
where saleDate between '2020-08-01' and '2020-08-30'
) t
where cnt > 1;
如果您在 group by 之后寫入每個(任何)列,則無法對其進行分組。 如果您想要奇點,請嘗試使用您無法理解的查詢。
Select
empId
,Count(empId)
From Testing
Where saleDate between '2020-08-01' and '2020-08-30'
GROUP BY empId
Having Count(empID) > 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.