我有一个项目,我在其中动态创建查询字符串。不是太复杂,就是复杂。我特别在将选择添加到组合中时遇到问题。当我保存视图时,Studio 添加了一个“as”。
SELECT TOP (100) PERCENT dbo.Users.ID AS UserId, dbo.Users_Info.Home_Area_Code, dbo.Users_Info.Work_Area_Code, dbo.Users_Info.State AS HomeState,
(CASE WHEN (dbo.Users_Info.Address1 = '' OR
dbo.Users_Info.Address1 IS NULL) THEN 0 ELSE 1 END) AS has_home_address, (CASE WHEN dbo.users.ID NOT IN
(SELECT UserID
FROM Users_Client_Companies) THEN 1 ELSE 0 END) AS Eliminate_TSI_Clients, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_ES) THEN 1 ELSE 0 END) AS Presented_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_ES
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_ES, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPresented_CS) THEN 1 ELSE 0 END) AS Presented_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM qUsersPlaced_CS
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_CS, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel
WHERE isnull(date_of_placement, '') > '') THEN 1 ELSE 0 END) AS Placed_EJA, (CASE WHEN dbo.users.ID IN
(SELECT individual_number
FROM QEliteAdCandidatesPanel) THEN 1 ELSE 0 END) AS Presented_EJA, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_TTMember) THEN 1 ELSE 0 END) AS TT_Member, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasHomeEmail) THEN 1 ELSE 0 END) AS Has_Home_Email, (CASE WHEN dbo.users.id IN
(SELECT ID
FROM qUsers_HasWorkEmail) THEN 1 ELSE 0 END) AS Has_Work_Email, dbo.Companies.Sales_Level, dbo.Users_Salary.ISIPCT AS Individual_Letter_Per,
dbo.Users_Salary.ISCPCT AS Corporate_Letter_Per, (CASE WHEN (dbo.Users_Info.Basic_JobAd = 1 OR
dbo.Users_Info.Custom_JobAd = 1 OR
dbo.Users_Info.Elite_JobAd = 1 OR
dbo.Users_Info.EJA_Candidate = 1) THEN 1 ELSE 0 END) AS job_board, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Adv) THEN 1 ELSE 0 END) AS Comp_Adv, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_BJA) THEN 1 ELSE 0 END) AS Comp_BJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CJA) THEN 1 ELSE 0 END) AS Comp_CJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_CS) THEN 1 ELSE 0 END) AS Comp_CS, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_EJA) THEN 1 ELSE 0 END) AS Comp_EJA, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_ES) THEN 1 ELSE 0 END) AS Comp_ES, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_Intern) THEN 1 ELSE 0 END) AS Comp_Intern, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_OrgAssess) THEN 1 ELSE 0 END) AS Comp_OrgAssess, (CASE WHEN dbo.Users_Info.Current_Company_Number IN
(SELECT company_id
FROM Companies_SB) THEN 1 ELSE 0 END) AS Comp_SB,
(SELECT MAX(date_purchased) AS EJA_Date_Purchased
FROM dbo.QEliteContacts
WHERE (contact_id = dbo.Users.ID)) AS Date_Purchased_EJA,
dbo.Users_Salary.ISTEC1 AS Technical_Federal_Per,
dbo.Users_Salary.ISTEC2 AS Technical_StateLocal_Per,
QEliteContacts 视图如下:
SELECT bd.date_purchased, bd.ID, csc.contact_id
FROM dbo.Board_Details AS bd LEFT OUTER JOIN
dbo.Client_Services_Contacts AS csc ON csc.service_id = bd.ID
WHERE (bd.ad_type LIKE '%Elite%')
靠近第一块代码的底部是语句“(SELECT MAX(date_purchased)AS EJA_Date_Purchased”。这是我添加到视图中的新代码。我得到并错误“'as'附近的无效语法”。如果我从 max(date_purchased) 工作室中删除别名将在我保存视图时添加“as expr1”。如果我从选择中删除别名,工作室将在我保存视图时添加“as expr1”。
说实话,我不确定我是否解决了正确获取最大日期的问题。