我正在尝试按以下格式检索数据:
Story | Story Type | Creation Date | Company Role | Tag 1 | Tag 2 | Tag 3
negative iii | Negative | 21/02/2011 | Business Analyst | Project Duration | Team Size | Process
negative ccc | Negative | 22/02/2011 | Admin | Workspace Layout | Organisational | Process
而不是:
Story | StoryCategoryID | CreationDate | CompanyRole | Name
negative iii | 1 | 21/02/2011 | Business Analyst | Project Duration
negative iii | 1 | 21/02/2011 | Business Analyst | Team Size
negative iii | 1 | 21/02/2011 | Business Analyst | Process
negative ccc | 1 | 22/02/2011 | Admin | Workspace Layout
negative ccc | 1 | 22/02/2011 | Admin | Organisational and Reporting Structure
negative ccc | 1 | 22/02/2011 | Admin | Process
但是我收到了以下错误:
1)关键字AS附近的语法不正确,
2)Z
附近的语法不正确这是我的SQL,请帮忙!
SELECT
Story,
CASE StoryCategoryID
WHEN 1 THEN 'Negative'
WHEN 0 THEN 'Positive'
ELSE CAST(StoryCategoryID as varchar(10)) as StoryCategoryID,
CreationDate,
CompanyRole,
MAX(CASE WHEN Z.MinName = af2.Name THEN af2.Name ELSE '' END) as Tag1 ,
MAX(CASE WHEN Z.MinName <> af2.Name AND Z.MaxName <> af2.Name THEN af2.Name ELSE '' END) as Tag2,
MAX(CASE WHEN Z.MaxName = af2.Name THEN af2.Name ELSE '' END) as Tag3
FROM ( SELECT s.Story , s.StoryCategoryID ,
CONVERT(VARCHAR(10), s.CreationDate, 103) AS CreationDate ,
m.CompanyRole , af.AgileFactorID , MIN(af.Name) MinName ,
MAX(af.Name) MaxName
FROM Story s
INNER JOIN ProjectIterationMember pm
ON pm.ProjectIterationMemberID = s.ProjectIterationMemberID
INNER JOIN Iterations i
ON i.ProjectIterationID = pm.ProjectIterationID
INNER JOIN Member m ON m.MemberID = pm.MemberID
INNER JOIN ProjectStoryFactors psf ON psf.StoryID = s.StoryID
INNER JOIN AgileFactors af ON af.AgileFactorID = psf.AgileFactorID
WHERE i.ProjectID = '" + proj_id + "'"
GROUP BY s.Story,
s.StoryCategoryID,
CONVERT(VARCHAR(10), s.CreationDate, 103),
m.CompanyRole, af.AgileFactorID ) Z
INNER JOIN AgileFactors af2 ON af2.AgileFactorID = Z.AgileFactorID
GROUP BY Story, StoryCategoryID, CreationDate, CompanyRole
答案 0 :(得分:4)
SQL无法读取:
SELECT
Story,
CASE StoryCategoryID WHEN 1 THEN 'Negative' WHEN 0 THEN 'Positive'
ELSE CAST(StoryCategoryID as varchar(10)) as StoryCategoryID,
CreationDate,
CompanyRole,
MAX(CASE WHEN Z.MinName = af2.Name THEN af2.Name ELSE '' END) as Tag1 ,
MAX(CASE WHEN Z.MinName <> af2.Name AND Z.MaxName <> af2.Name THEN af2.Name
ELSE '' END) as Tag2,
MAX(CASE WHEN Z.MaxName = af2.Name THEN af2.Name ELSE '' END) as Tag3
FROM ( SELECT s.Story , s.StoryCategoryID , CONVERT(VARCHAR(10), s.CreationDate,
103) AS CreationDate ,
m.CompanyRole , af.AgileFactorID , MIN(af.Name) MinName ,
MAX(af.Name) MaxName FROM Story s
INNER JOIN ProjectIterationMember pm ON pm.ProjectIterationMemberID =
s.ProjectIterationMemberID INNER JOIN Iterations i
ON i.ProjectIterationID = pm.ProjectIterationID INNER JOIN Member m
ON m.MemberID = pm.MemberID INNER JOIN ProjectStoryFactors psf
ON psf.StoryID = s.StoryID INNER JOIN AgileFactors af
ON af.AgileFactorID = psf.AgileFactorID WHERE i.ProjectID = '" + proj_id + "'"
GROUP BY s.Story, s.StoryCategoryID, CONVERT(VARCHAR(10), s.CreationDate, 103),
m.CompanyRole, af.AgileFactorID ) Z INNER JOIN AgileFactors af2
ON af2.AgileFactorID = Z.AgileFactorID
GROUP BY Story, StoryCategoryID, CreationDate, CompanyRole;
至少组织它以便结构关键字可见:
SELECT Story,
CASE StoryCategoryID WHEN 1 THEN 'Negative' WHEN 0 THEN 'Positive'
ELSE CAST(StoryCategoryID as varchar(10)) as StoryCategoryID,
CreationDate,
CompanyRole,
MAX(CASE WHEN Z.MinName = af2.Name THEN af2.Name ELSE '' END) as Tag1,
MAX(CASE WHEN Z.MinName <> af2.Name AND Z.MaxName <> af2.Name
THEN af2.Name ELSE '' END) as Tag2,
MAX(CASE WHEN Z.MaxName = af2.Name THEN af2.Name ELSE '' END) as Tag3
FROM (SELECT s.Story, s.StoryCategoryID,
CONVERT(VARCHAR(10), s.CreationDate, 103) AS CreationDate,
m.CompanyRole, af.AgileFactorID, MIN(af.Name) MinName,
MAX(af.Name) MaxName
FROM Story s
JOIN ProjectIterationMember pm
ON pm.ProjectIterationMemberID = s.ProjectIterationMemberID
JOIN Iterations i ON i.ProjectIterationID = pm.ProjectIterationID
JOIN Member m ON m.MemberID = pm.MemberID
JOIN ProjectStoryFactors psf ON psf.StoryID = s.StoryID
JOIN AgileFactors af ON af.AgileFactorID = psf.AgileFactorID
WHERE i.ProjectID = '" + proj_id + "'"
GROUP BY s.Story, s.StoryCategoryID,
CONVERT(VARCHAR(10), s.CreationDate, 103),
m.CompanyRole, af.AgileFactorID) Z
JOIN AgileFactors af2 ON af2.AgileFactorID = Z.AgileFactorID
GROUP BY Story, StoryCategoryID, CreationDate, CompanyRole
符号'" + proj_id + "'"
表明这是从编程语言中的字符串中提取的;这是一个SQL注入的公开邀请。在纯SQL上下文中,它在语法上是无效的;你最后有一个迷路双引号。
答案 1 :(得分:1)
和
MAX(af.Name)MaxName 关闭mac并上床的时间:-o