ORIGINAL QUERY:
select DISTINCT a.UniversalMemberID, a.Name, a.DOB, a.[First Service Date],
(select count(b.UniversalMemberID)
from WCV b
where b.[First Service Date] <= a.[First Service Date]
and a.UniversalMemberID = b.UniversalMemberID)
AS Visit_CNT
from WCV as a
ORDER by a.UniversalMemberID, a.[First Service Date]
需要将上述查询的结果插入临时表#TEMP
不断返回此错误:
关键字选择
附近的语法不正确
答案 0 :(得分:3)
This SqlFiddle似乎工作得很好......这是你正在尝试做的吗?
SELECT DISTINCT a.UniversalMemberID, a.Name, a.DOB, a.[First Service Date],
(SELECT count(b.UniversalMemberID)
FROM WCV b
WHERE b.[First Service Date] <= a.[First Service Date]
AND a.UniversalMemberID = b.UniversalMemberID)
AS Visit_CNT
into #MyTempTable
FROM WCV AS a
ORDER BY a.UniversalMemberID, a.[First Service Date]
Select * from #MyTempTable
答案 1 :(得分:0)
当我遇到查询问题时,我总是尝试将其拆分并查看是否有问题,如果您尝试分开查询,从SELECT *
这样的泛型开始到更多过滤查询,它也会有所帮助。
SELECT
DISTINCT a.UniversalMemberID, a.Name, a.DOB, a.[First Service Date],
(
SELECT count(b.UniversalMemberID)
FROM
WCV AS b
WHERE
b.[First Service Date] <= a.[First Service Date] and a.UniversalMemberID = b.UniversalMemberID
) AS Visit_CNT
FROM
WCV as a
ORDER by a.UniversalMemberID, a.[First Service Date]
我将首先尝试从括号中进行查询,然后开始添加内容。希望能帮助到你。
给我们反馈。