我需要一个条件为Target_L * X / Y
的列。
需要x和y的地方应自动来自查询。
Y =每月工作日总数,不包括星期日 即1月,27天是工作日
X =工作日直到昨天的日期
即23(减去星期日,1至26 = 26-3 = 23)
今天是2018年1月27日
final = Target_L * 23/27
OR
如果这是很长的方法,那么第二个也没关系
从这两种方法对我都有好处,我需要q查询..我有日期列,月份列。 使用SQL SERVER
答案 0 :(得分:0)
我不确定你在问什么。但试试这个。
DECLARE @FirstDay DATETIME
SET @FirstDay = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
DECLARE @LastDay DATETIME
SET @LastDay = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE())+1,0))
DECLARE @Yesterday DATETIME
SET @Yesterday = dateadd(day,datediff(day,1,GETDATE()),0)
DECLARE @DaysInMonth int
SET @DaysInMonth = DAY(EOMONTH(GETDATE()))
DECLARE @NoOFSundaysInMonth int
SET @NoOFSundaysInMonth = DATEDIFF(WK, DAteAdd(DD, -1,@FirstDay), DAteAdd(DD, 1, @LastDay))
DECLARE @NoOfDaysWithoutSunInMonth int
SET @NoOfDaysWithoutSunInMonth = @DaysInMonth - @NoOFSundaysInMonth
DECLARE @NoOFDaysTillYes int
SET @NoOFDaysTillYes = DATEDIFF(d, @FirstDay, GETDATE())
DECLARE @NoOFSunTillYes int
SET @NoOFSunTillYes = DATEDIFF(WK, DAteAdd(DD, -1,@FirstDay), DAteAdd(DD, 1, @Yesterday))
DECLARE @NoOfDaysWithoutSunTillYes int
SET @NoOfDaysWithoutSunTillYes = @NoOFDaysTillYes - @NoOFSunTillYes
要检查这些值,请将其添加到查询
SELECT
@FirstDay AS 'First_Day',
@LastDay AS 'Last_Day',
@Yesterday AS 'Yesterday',
@DaysInMonth AS 'Days_In_Month',
@NoOFSundaysInMonth AS 'No_Of_Sundays',
@NoOfDaysWithoutSunInMonth AS 'No_OF_Days_Without_Sun',
@NoOFDaysTillYes AS 'No_OF_Days_Till_Yesterday',
@NoOFSunTillYes AS 'No_OF_Sundays_Till_Yesterday',
@NoOfDaysWithoutSunTillYes AS 'No_OF_Days_Without_Sunday_Till_Yesterday'
如果'Y'表示本月没有星期日的所有天数,如果'X'表示直到昨天没有星期日的所有日子,你可以使用它。
final =Target_L * X/Y
final =Target_L * @NoOfDaysWithoutSunTillYes / @NoOfDaysWithoutSunInMonth
NoOfDaysWithoutSunTillYes = 23
NoOfDaysWithoutSunInMonth = 27