我有一个CTE表,没有。计算的工作日数,以及一个单独的查询,我希望将此CTE表联接并在wkdaysinmonth,wkdaystodate中读取,但是我有点迷失了如何将这两个联接在一起并获得结果:
with dates as(
select dateadd(d,-day(getdate())+1,convert(date,getdate())) as startofmonth,
dateadd(d,-1,dateadd(m,1,dateadd(d,-day(getdate())+1,convert(date,getdate())))) as endofmonth,
convert(date,getdate()) as today
), holidays as (
select *
from (values ('20181224'),('20181225'),('20181231')) x (holiday)
)
,holidaycount as (
select count(*) as holidaysinmonth,
sum(case when holiday<=today then 1 else 0 end) as holidaystodate
from dates
join holidays on holiday between startofmonth and endofmonth
)
,daycounts as(
select dates.*,
(DATEDIFF(dd, startofmonth, endofmonth) + 1)
-(DATEDIFF(wk, startofmonth, endofmonth) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, endofmonth) = 'Saturday' THEN 1 ELSE 0 END)
-isnull(holidaysinmonth,0) as wkdaysinmonth,
(DATEDIFF(dd, startofmonth, today) + 1)
-(DATEDIFF(wk, startofmonth, today) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, today) = 'Saturday' THEN 1 ELSE 0 END)
-isnull(holidaystodate,0) as wkdaystodate
from dates
cross join holidaycount
)
-------------代码2 -------------
select i.inv_date, c.SALES_ID, s.CL_KEY,
sum(i.MERCH - i.COGS) as GP
from invoice i
left join vw_MomOrdDetail c on c.orderno = i.ORDERNO and c.INPART = i.INPART
left join CMS s on s.ORDERNO = i.ORDERNO
where i.INV_DATE = '2019-01-02 00:00:00.000'
group by i.INV_DATE, CL_KEY, c.SALES_ID
我尝试了类似的方法,但是没有用:
with dates as(
select dateadd(d,-day(getdate())+1,convert(date,getdate())) as startofmonth,
dateadd(d,-1,dateadd(m,1,dateadd(d,-day(getdate())+1,convert(date,getdate())))) as endofmonth,
convert(date,getdate()) as today
), holidays as (
select *
from (values ('20181224'),('20181225'),('20181231')) x (holiday)
)
,holidaycount as (
select count(*) as holidaysinmonth,
sum(case when holiday<=today then 1 else 0 end) as holidaystodate
from dates
join holidays on holiday between startofmonth and endofmonth
)
,daycounts as(
select dates.*,
(DATEDIFF(dd, startofmonth, endofmonth) + 1)
-(DATEDIFF(wk, startofmonth, endofmonth) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, endofmonth) = 'Saturday' THEN 1 ELSE 0 END)
-isnull(holidaysinmonth,0) as wkdaysinmonth,
(DATEDIFF(dd, startofmonth, today) + 1)
-(DATEDIFF(wk, startofmonth, today) * 2)
-(CASE WHEN DATENAME(dw, startofmonth) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, today) = 'Saturday' THEN 1 ELSE 0 END)
-isnull(holidaystodate,0) as wkdaystodate
from dates
cross join holidaycount
)
SELECT o1.inv_date,o1.SALES_ID,o1.CL_KEY,o1.weekdaysinmonth
FROM
(select i.inv_date, c.SALES_ID, s.CL_KEY,
(select wkdaysinmonth from daycounts) as weekdaysinmonth,
(select wkdaystodate from daycounts) as weekdaystodate,
sum(i.MERCH - i.COGS) as GP
from invoice i
left join vw_MomOrdDetail c on c.orderno = i.ORDERNO and c.INPART = i.INPART
left join CMS s on s.ORDERNO = i.ORDERNO
where i.INV_DATE = '2019-01-02 00:00:00.000'
group by i.INV_DATE, CL_KEY, c.SALES_ID) AS o1 on o1.SALES_ID=c.SALES_ID
错误:关键字“ on”附近的语法不正确。
所需的输出列为:
inv_date,SALES_ID,CL_KEY,weekdaysinmonth
感谢您的帮助。 谢谢
答案 0 :(得分:0)
错误消息说
关键字“ on”附近的语法不正确
因为您的联接引用了在括号生成表o1
中声明的表别名
我建议您创建一个calendar table,而不是动态生成日期。您可以有假期字段,wkdaysinmonth
和wkdaystodate
可以预先计算,并且只有一个表要联接。
ALL_CAPS
用于表名和字段名,而有时all_lower
则交替使用。
我在SQL Server上看到的常见样式是:
Capitalised_Table_Names.all_lower_field_names
即使您不喜欢该样式,也请定义自己的样式,但要保持一致。
答案 1 :(得分:0)
发生错误的原因是您试图在“外部”查询中引用“内部”表。您只能做相反的事情。您也有一个ON
,而没有JOIN
。
简而言之,您拥有的是:
SELECT ... FROM (Derived Table) o1 ON o1.Column=c.Column
不仅该ON子句没有JOIN,而且外部查询中不存在别名c
。
您需要将c
与派生表本身而不是外部查询中的其他表进行所有关联。