我试图理解交叉应用并解决一个特定的问题-参照下表,找到不使用“ b”的产品“ a”的唯一用户,在下面的示例中该用户仅是“ u2” :
USE [tempdb]
GO
IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[test]') AND type IN (N'U'))
BEGIN
DROP TABLE [test]
END
GO
CREATE TABLE [test](
[user] [varchar](8) NOT NULL,
[product] [varchar](8) NOT NULL,
) ON [PRIMARY]
INSERT [test] ([user], [product])
VALUES ('u1', 'a')
INSERT [test] ([user], [product])
VALUES ('u1', 'b')
INSERT [test] ([user], [product])
VALUES ('u2', 'a')
INSERT [test] ([user], [product])
VALUES ('u3', 'b')
select asuers.user
from (select distinct user from test where product = 'a') ausers
cross apply (select distinct user from test) busers
,出现以下错误:
Msg 156, Level 15, State 1, Line 31
Incorrect syntax near the keyword 'user'.
Msg 102, Level 15, State 1, Line 32
Incorrect syntax near 'ausers'.
Msg 102, Level 15, State 1, Line 33
Incorrect syntax near 'busers'.
谢谢
答案 0 :(得分:0)
您必须用user
引述[]
,否则将其视为函数调用:
select asuers.[user]
from (select distinct [user] from test where product = 'a') ausers
cross apply (select distinct [user] from test) busers