我正在尝试将查询结果插入到表中,但使用'Insert Into' xxx(col1, col2, col3...) values(....)
但出现语法错误'关键字'with'附近的语法不正确。如果此语句是公用表表达式、xmlnamespaces 子句或更改跟踪上下文子句,则前面的语句必须以分号结束。
知道如何在这样的复杂查询中使用“插入”吗?请注意“with Rownumbers..”查询有效并返回准确的结果,当我尝试插入该结果时只是错误。
INSERT INTO Cost(Name, [Status], [Hours]) values(
with RowNumbers (RowNum, name, [status], [DateTime])
as
(
select
ROW_NUMBER() over (partition by name order by [DateTime]),
name,
[status],
[DateTime]
from @T
)
select
T1.name,
case T1.[status]
when 0 then 'In'
when 1 then 'Out'
when 2 then 'Absent'
end as [status],
sum(datediff(MINUTE, T1.[DateTime], T2.[DateTime]) / 60.0) as [hours]
from RowNumbers T1
inner join RowNumbers T2
on T1.RowNum = T2.RowNum - 1 -- joins the current row to the next one
and T1.name = T2.name
group by T1.name, T1.[status]
order by T1.Name, T1.[status]);