日期条款的动态

发布于 2025-01-18 06:46:06 字数 1449 浏览 1 评论 0原文

现在我的代码有效,但这不是动态的。下面显示了2022年1月每日表的每日值的总和,而不是2022年1月的月表。

我试图弄清楚如何编码此内容而且我不必在每次子句中更改日期。

SELECT 
b.LocationDescription AS 'Location'
,CONVERT(varchar, cm.RecordDate, 1) AS 'Month Date'
,DATENAME(month,cm.RecordDate) AS 'Month'
,SUM(cm.Production) AS 'Monthly Prod'
,d.total AS 'Daily Prod'
,SUM(cm.Production)-d.total AS 'Diff'
,(CASE WHEN d.total = 0 THEN 0 ELSE (SUM(cm.Production) - d.total)/d.total*100 END) AS 'Percent'

FROM CompletionMonthlyTb cm

INNER JOIN CompletionTb c ON cm.SystemID = c.SystemID 
INNER JOIN LocationTb as b ON c.LocationID = b.LocationSystemID

JOIN (SELECT DISTINCT
    b.LocationDescription AS 'Location'
    ,sum(cd.[Production])  AS 'Total'    
           
    FROM [xxxxxxx].[dbo].[LocationTb] b

    JOIN xxxxxxxxx.dbo.CompletionTb c ON c.LocationID = b.LocationSystemID
    JOIN xxxxxxx.dbo.CompletionDailyTb cd ON cd.SystemID = c.SystemID

    WHERE cd.RecordDate BETWEEN '1-1-2022' AND '1-31-22'
    
    GROUP BY b.LocationDescription) AS d ON d.Location = b.LocationDescription

WHERE cm.RecordDate = '2022-01-01' 

GROUP BY b.LocationDescription, cm.RecordDate, d.total

ORDER BY cm.RecordDate, b.LocationDescription

结果:

Location  Month Date  Month    Monthly Prod   Daily Prod   Diff     Percent
Alpha     01/01/22    January   27663         25584        2079       8
Beta      01/01/22    January   25503         26104       -600       -2

Right now my code works, but it is not dynamic. Below shows the sum of daily values from a daily table in January 2022 compared to the monthly value from the monthly table for January 2022.

I am trying to figure out how to code this so the daily totals compared to the monthly can be pulled at anytime and I won't have to change the dates in the WHERE clauses everytime.

SELECT 
b.LocationDescription AS 'Location'
,CONVERT(varchar, cm.RecordDate, 1) AS 'Month Date'
,DATENAME(month,cm.RecordDate) AS 'Month'
,SUM(cm.Production) AS 'Monthly Prod'
,d.total AS 'Daily Prod'
,SUM(cm.Production)-d.total AS 'Diff'
,(CASE WHEN d.total = 0 THEN 0 ELSE (SUM(cm.Production) - d.total)/d.total*100 END) AS 'Percent'

FROM CompletionMonthlyTb cm

INNER JOIN CompletionTb c ON cm.SystemID = c.SystemID 
INNER JOIN LocationTb as b ON c.LocationID = b.LocationSystemID

JOIN (SELECT DISTINCT
    b.LocationDescription AS 'Location'
    ,sum(cd.[Production])  AS 'Total'    
           
    FROM [xxxxxxx].[dbo].[LocationTb] b

    JOIN xxxxxxxxx.dbo.CompletionTb c ON c.LocationID = b.LocationSystemID
    JOIN xxxxxxx.dbo.CompletionDailyTb cd ON cd.SystemID = c.SystemID

    WHERE cd.RecordDate BETWEEN '1-1-2022' AND '1-31-22'
    
    GROUP BY b.LocationDescription) AS d ON d.Location = b.LocationDescription

WHERE cm.RecordDate = '2022-01-01' 

GROUP BY b.LocationDescription, cm.RecordDate, d.total

ORDER BY cm.RecordDate, b.LocationDescription

RESULTS:

Location  Month Date  Month    Monthly Prod   Daily Prod   Diff     Percent
Alpha     01/01/22    January   27663         25584        2079       8
Beta      01/01/22    January   25503         26104       -600       -2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文