我有一份报告,显示了前两周的数据。我希望将周末发生的任何活动包括在星期五,将假期中的任何活动包括在商务日之前。
此查询是Power BI文件的来源,因此我可以在BI中应用过滤器以排除假日和周末,这很棒。但是,我希望这些天发生的任何活动都包括在其他日子中。
这是我的代码。 IsHoliday和IsWeekday字段是二进制0,1字段,可用于过滤Power BI中的结果:
DECLARE @currenttime as DATE, @currentmonthyear as varchar (50)
SET @currenttime = getutcdate() at time zone 'UTC' at time zone 'Central
Standard Time'
SET @currentmonthyear =
CONVERT(char(3),@currenttime,0)+''+DATENAME(YEAR,@currenttime)
SELECT
---Dates
CONVERT(CHAR(3),l.originearliest,0)+'-'+DATENAME(YEAR,l.originearliest)
as PresentationMonth
,cal.Date as CalDate
,concat(month(cal.date),'-',day(cal.date),'-',year(cal.date)) as Date
,cal.IsHoliday
,cal.IsWeekday
---BizDays
,mv.BizDays
,bz.BizDaysMTD
---Metrics
,r.office
,c.Name as Customer_Name
,l.customercode as Customer_Code
,CASE
WHEN lx.BookedLoadCustomerRep is NULL THEN r.KeypointUserCode
ELSE BookedLoadCustomerRep
END AS Customer_Rep
,l.Dispatcher as Carrier_Rep
---Revenue
,SUM(CUSTOMERRATE) as Revenue
,SUM(CUSTOMERRATE)/COUNT(l.LoadNumber) as RPL
---Cost
,SUM(CarrierCost) as Cost
---Spread
,(SUM(CUSTOMERRATE)-SUM(CarrierCost)) as Spread
,(SUM(CUSTOMERRATE)-SUM(CarrierCost))/COUNT(l.LoadNumber)as SPL
---Loads
,COUNT(l.LoadNumber) as Loads
FROM kpo.Load l
left join ops.Customer c on c.customerid=l.customerid
left join rpt.reportuser r on r.userid=c.SalespersonUserId
left join kpo.LoadExtension lx on lx.LoadNumber=l.LoadNumber
left join rpt.MonthView mv on
CONVERT(char(3),l.originearliest,0)+''+DATENAME(YEAR,l.originearliest) = MonthYear
left join com.Calendar cal on convert(date,cal.Date) = convert(date,l.OriginEarliest)
INNER JOIN
(SELECT
MonthYear,
CASE WHEN
CONVERT(char(3),@currenttime,0)+''+DATENAME(YEAR,@currenttime) = MonthYear
THEN (BizDaysMTD)-1
ELSE BizDaysMTD
END as BizDaysMTD
from rpt.MonthView) bz on bz.MonthYear = mv.MonthYear
WHERE
CONVERT(DATE,l.OriginEarliest) <=
(CASE
when DATENAME(WEEKDAY,@currenttime)='Saturday'
THEN DATEADD(DY,-1,@currenttime)
WHEN DATENAME(WEEKDAY,@currenttime)='Sunday'
THEN DATEADD(DY,-2,@currenttime)
WHEN DATENAME(WEEKDAY,@currenttime)='Monday'
THEN DATEADD(DY,-3,@currenttime)
ELSE DATEADD(DY,-1,@currenttime)
END)
AND
YEAR(l.originearliest)>='2014'
AND
l.OrderStatus not in ('void','available')
AND
DATEDIFF(DAY,l.originearliest,@currenttime) between 1 and 14
GROUP BY
r.office
,c.Name
,DATEPART(WEEK,l.originearliest)
,Month(l.originearliest)
,Year(l.originearliest)
,Year(l.originearliest)+'-'+Month(l.originearliest)
,mv.MonthYear
,BizDays
,bz.BizDaysMTD
,l.CustomerCode
,l.OriginEarliest
,lx.BookedLoadCustomerRep
,r.KeypointUserCode
,l.dispatcher
,cal.IsHoliday
,cal.IsWeekday
,cal.Date
ORDER BY
Year(l.originearliest) desc
,Month(l.originearliest) desc
任何帮助将不胜感激!