[英]mysql sql query to avoid week-end day in a sum of date
我正在嘗試得出任何雇主工作的天數的總和,但在這筆總和中,mysql算是工作日,所以結果不准確。 如何避免在sum()函數中獲得工作日?
我嘗試過:
where mydate NOT IN ('Saturday', 'Sunday')
如我在類似問題的其他答案中所見,但是在這里我使用了一個組函數,這意味着總和將在調用where子句之前進行求值。
這是我的查詢:
select pp.person_name,sum(ss.spread_sheet_date) as days
from spread_sheet ss
left join activity ac on ss.activity_id = ac.activity_id
left join person pp on pp.person_id = ss.person_id
where ac.activity_id is not null
group by ac.activity_id,pp.person_id ;
這是我的NOT IN查詢:
select pp.person_name,sum(ss.spread_sheet_date) as days
from spread_sheet ss
left join activity ac on ss.activity_id = ac.activity_id
left join person pp on pp.person_id = ss.person_id
where ac.activity_id is not null and ss.spread_sheet_date NOT IN ('Saturday', 'Sunday')
group by ac.activity_id,pp.person_id ;
我也在另一個問題中找到了這個答案,我嘗試使用它
SELECT *
FROM your_table
WHERE ((DATEPART(dw, date_created) + @@DATEFIRST) % 7) NOT IN (0, 1)
但它不起作用,我想這與mysql不兼容。
有人知道如何解決嗎?
嗯:在查詢的一部分中,您將ss.spread_sheet_date
視為可以求和的數字(也許是要計數?),在其他部分,您將其視為日期。 如果確實是約會,請更改:
where ac.activity_id is not null and ss.spread_sheet_date NOT IN ('Saturday', 'Sunday')
至:
where ac.activity_id is not null and DAYOFWEEK(ss.spread_sheet_date) NOT IN (1,7)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.