注册 登录
编程论坛 SQL Server论坛

有人能帮忙翻译这段SQL语法吗?

yz1025 发布于 2020-05-08 12:36, 2069 次点击
SELECT ROW_NUMBER() OVER (ORDER BY ProjectType, DepartmentNo, KickOffDate DESC, ProjectNo DESC) AS RowNo, * FROM
(SELECT A.ProjectNo, A.ProjectType, A.DevpType, A.KickOffDate, A.DeviceNo AS DeviceName, A.RequestService, A.Platform01, A.Platform02, A.Platform03, A.ProductType,
CASE WHEN ISNULL(A.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, A.SalesNo, A.EstFinishDate AS
PlanDate, A.Remark, A.Div, A.DepartmentNo, A.EngineerNo, A.ReleaseStatus, A.ReleaseStatusDate, A.PhaseDate, B.StatusDesc AS
ReleaseName, C.StatusDesc AS PhaseName, D.CustomerEngSName, D.CustomerChiSName, '' AS SubRev, NULL AS SubRevNo FROM tblProject AS A
INNER JOIN tblBasStatusDesc AS B ON A.ReleaseStatus = B.Status AND B.Category = 'ProjectReleaseStatus'
LEFT OUTER JOIN tblBasStatusDesc AS C ON A.Phase = C.Status AND C.Category = 'ProjectPhase'
LEFT OUTER JOIN tblMstCustomerPublic AS D ON A.CustomerNo = D.CustomerNo WHERE A.SubDevFlag = 'N' AND A.DevpType
IN ('TD开发', '客户开发') AND ((A.KickOffDate >= '2020/04/26' AND A.KickOffDate < '2020/05/03') OR (A.ReleaseStatusDate >= '2020/ 04/26' AND
A.ReleaseStatusDate < '2020/05/03') OR (A.KickOffDate < '2020/05/03' AND A.ReleaseStatus < 3) OR
(A.KickOffDate < '2020/04/26' AND A.ReleaseStatus >= 3 AND A.ReleaseStatusDate >= '2020/05/03' AND A.ReleaseStatusDate < '2020/05/09'))
UNION ALL
SELECT B.ProjectNo, B.ProjectType, B.DevpType, A.ItemDate AS KickOffDate, A.DeviceName, B.RequestService, B.Platform01, B.Platform02, B.Platform03, B.ProductType,
CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, B.SalesNo, B.EstFinishDate AS
PlanDate, B.Remark, B.Div, B.DepartmentNo, B.EngineerNo, A.ReleaseStatus, A.ReleaseStatusDate, A.PhaseDate, C.StatusDesc AS
ReleaseName, D.StatusDesc AS PhaseName, E.CustomerEngSName, E.CustomerChiSName, 'S' AS SubRev, A.ItemNo AS SubRevNo FROM tblProjectSubDevice AS A
INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo AND B.SubDevFlag = 'Y'
INNER JOIN tblBasStatusDesc AS C ON A.ReleaseStatus = C.Status AND C.Category = 'ProjectReleaseStatus'
LEFT OUTER JOIN tblBasStatusDesc AS D ON A.Phase = D.Status AND D.Category = 'ProjectPhase'
LEFT OUTER JOIN tblMstCustomerPublic AS E ON B.CustomerNo = E.CustomerNo WHERE B.DevpType IN ('TD开发', '客户开发') AND
((A.ItemDate >= '2020/04/26' AND A.ItemDate < '2020/05/03') OR (A.ReleaseStatusDate >= '2020/04/26' AND
A.ReleaseStatusDate < '2020/05/03') OR (A.ItemDate < '2020/05/03' AND A.ReleaseStatus < 3) OR (A.ItemDate < '2020/04/26' AND A.ReleaseStatus > = 3
AND A.ReleaseStatusDate >= '2020/05/03' AND A.ReleaseStatusDate < '2020/05/09'))
UNION ALL
SELECT B.ProjectNo, B.ProjectType, B.DevpType, A.IndexDate AS KickOffDate, A.DeviceName, B.RequestService, B.Platform01, B.Platform02, B.Platform03, B.ProductType,
CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, B.SalesNo, B.EstFinishDate AS
PlanDate, B.Remark, B.Div, B.DepartmentNo, B.EngineerNo, A.ReleaseStatus, A.ReleaseStatusDate, A.PhaseDate, C.StatusDesc AS
ReleaseName, D.StatusDesc AS PhaseName, E.CustomerEngSName, E.CustomerChiSName, 'R' AS SubRev, A.IndexNo AS SubRevNo FROM tblProjectRevision AS A
INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo
INNER JOIN tblBasStatusDesc AS C ON A.ReleaseStatus = C.Status AND C.Category = 'ProjectReleaseStatus'
LEFT OUTER JOIN tblBasStatusDesc AS D ON A.Phase = D.Status AND D.Category = 'ProjectPhase'
LEFT OUTER JOIN tblMstCustomerPublic AS E ON B.CustomerNo = E.CustomerNo WHERE B.DevpType IN ('TD开发', '客户开发') AND ((A.IndexDate >= '2020/04/26'
AND A.IndexDate < '2020/05/03') OR (A.ReleaseStatusDate >= '2020/04/26' AND A.ReleaseStatusDate < '2020/05/03') OR (A.IndexDate < '2020/05 /03' AND A.ReleaseStatus < 3) OR
(A.IndexDate < '2020/04/26' AND A.ReleaseStatus >= 3 AND A.ReleaseStatusDate >= '2020/05/03' AND A.ReleaseStatusDate < '2020/05/09')) ) AS myTable1

前人写的,太长实在看不懂,又很多一层又包一层的'()'搜寻指令,我已经稍微做些断行处理了,还是很难懂。
5 回复
#2
厨师王德榜2020-05-09 11:08
用UNION ALL分隔的,有三段,这三段拆开来看,每段都是一个独立的查询.
再看其中的一段, 分别以不同的条件,链接了tblBasStatusDesc\ tblMstCustomerPublic 2表
另外新生成的NPIType字段,用条件 CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END
作了一个限定,其它没什么可说的了,遇到长的查询语句不要慌,
尤其是用UNION ALL分隔的更不要慌乱,无非就是几个相同字段的查询联合起来,
把其中一段读懂了,后来就好办了.
说实话,你这个查询其实并不复杂.
#3
yz10252020-05-14 15:17
以下是引用厨师王德榜在2020-5-9 11:08:08的发言:

用UNION ALL分隔的,有三段,这三段拆开来看,每段都是一个独立的查询.
再看其中的一段, 分别以不同的条件,链接了tblBasStatusDesc\ tblMstCustomerPublic 2表
另外新生成的NPIType字段,用条件 CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END
作了一个限定,其它没什么可说的了,遇到长的查询语句不要慌,
尤其是用UNION ALL分隔的更不要慌乱,无非就是几个相同字段的查询联合起来,
把其中一段读懂了,后来就好办了.
说实话,你这个查询其实并不复杂.


虽然还是看不懂,不过感觉上好像很有道理。
#4
sssooosss2020-07-19 14:44
一段段分析着慢慢看就明白了
#5
oldfish962020-07-24 22:07
用sql prompt格式化下
SELECT ROW_NUMBER() OVER ( ORDER BY ProjectType, DepartmentNo, KickOffDate DESC, ProjectNo DESC ) AS RowNo, *
    FROM( SELECT A.ProjectNo, A.ProjectType, A.DevpType, A.KickOffDate, A.DeviceNo AS DeviceName, A.RequestService,
                 A.Platform01, A.Platform02, A.Platform03, A.ProductType,
                 CASE WHEN ISNULL(A.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, A.SalesNo,
                 A.EstFinishDate AS PlanDate, A.Remark, A.Div, A.DepartmentNo, A.EngineerNo, A.ReleaseStatus,
                 A.ReleaseStatusDate, A.PhaseDate, B.StatusDesc AS ReleaseName, C.StatusDesc AS PhaseName,
                 D.CustomerEngSName, D.CustomerChiSName, '' AS SubRev, NULL AS SubRevNo
              FROM tblProject AS A
                   INNER JOIN tblBasStatusDesc AS B ON A.ReleaseStatus = B.Status
                                                       AND B.Category = 'ProjectReleaseStatus'
                   LEFT OUTER JOIN tblBasStatusDesc AS C ON A.Phase = C.Status
                                                            AND C.Category = 'ProjectPhase'
                   LEFT OUTER JOIN tblMstCustomerPublic AS D ON A.CustomerNo = D.CustomerNo
              WHERE A.SubDevFlag = 'N'
                    AND A.DevpType IN ( 'TD开发', '客户开发' )
                    AND (( A.KickOffDate >= '2020/04/26' AND A.KickOffDate < '2020/05/03' )
                         OR ( A.ReleaseStatusDate >= '2020/ 04/26' AND A.ReleaseStatusDate < '2020/05/03' )
                         OR ( A.KickOffDate < '2020/05/03' AND A.ReleaseStatus < 3 )
                         OR ( A.KickOffDate < '2020/04/26'
                              AND A.ReleaseStatus >= 3
                              AND A.ReleaseStatusDate >= '2020/05/03'
                              AND A.ReleaseStatusDate < '2020/05/09' ))
          UNION ALL
          SELECT B.ProjectNo, B.ProjectType, B.DevpType, A.ItemDate AS KickOffDate, A.DeviceName, B.RequestService,
                 B.Platform01, B.Platform02, B.Platform03, B.ProductType,
                 CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, B.SalesNo,
                 B.EstFinishDate AS PlanDate, B.Remark, B.Div, B.DepartmentNo, B.EngineerNo, A.ReleaseStatus,
                 A.ReleaseStatusDate, A.PhaseDate, C.StatusDesc AS ReleaseName, D.StatusDesc AS PhaseName,
                 E.CustomerEngSName, E.CustomerChiSName, 'S' AS SubRev, A.ItemNo AS SubRevNo
              FROM tblProjectSubDevice AS A
                   INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo
                                                 AND B.SubDevFlag = 'Y'
                   INNER JOIN tblBasStatusDesc AS C ON A.ReleaseStatus = C.Status
                                                       AND C.Category = 'ProjectReleaseStatus'
                   LEFT OUTER JOIN tblBasStatusDesc AS D ON A.Phase = D.Status
                                                            AND D.Category = 'ProjectPhase'
                   LEFT OUTER JOIN tblMstCustomerPublic AS E ON B.CustomerNo = E.CustomerNo
              WHERE B.DevpType IN ( 'TD开发', '客户开发' )
                    AND (( A.ItemDate >= '2020/04/26' AND A.ItemDate < '2020/05/03' )
                         OR ( A.ReleaseStatusDate >= '2020/04/26' AND A.ReleaseStatusDate < '2020/05/03' )
                         OR ( A.ItemDate < '2020/05/03' AND A.ReleaseStatus < 3 )
                         OR ( A.ItemDate < '2020/04/26'
                              AND A.ReleaseStatus >= 3
                              AND A.ReleaseStatusDate >= '2020/05/03'
                              AND A.ReleaseStatusDate < '2020/05/09' ))
          UNION ALL
          SELECT B.ProjectNo, B.ProjectType, B.DevpType, A.IndexDate AS KickOffDate, A.DeviceName, B.RequestService,
                 B.Platform01, B.Platform02, B.Platform03, B.ProductType,
                 CASE WHEN ISNULL(B.NPINumber, 'NK') = 'NK' THEN 'NK' ELSE 'N' END AS NPIType, B.SalesNo,
                 B.EstFinishDate AS PlanDate, B.Remark, B.Div, B.DepartmentNo, B.EngineerNo, A.ReleaseStatus,
                 A.ReleaseStatusDate, A.PhaseDate, C.StatusDesc AS ReleaseName, D.StatusDesc AS PhaseName,
                 E.CustomerEngSName, E.CustomerChiSName, 'R' AS SubRev, A.IndexNo AS SubRevNo
              FROM tblProjectRevision AS A
                   INNER JOIN tblProject AS B ON A.ProjectNo = B.ProjectNo
                   INNER JOIN tblBasStatusDesc AS C ON A.ReleaseStatus = C.Status
                                                       AND C.Category = 'ProjectReleaseStatus'
                   LEFT OUTER JOIN tblBasStatusDesc AS D ON A.Phase = D.Status
                                                            AND D.Category = 'ProjectPhase'
                   LEFT OUTER JOIN tblMstCustomerPublic AS E ON B.CustomerNo = E.CustomerNo
              WHERE B.DevpType IN ( 'TD开发', '客户开发' )
                    AND (( A.IndexDate >= '2020/04/26' AND A.IndexDate < '2020/05/03' )
                         OR ( A.ReleaseStatusDate >= '2020/04/26' AND A.ReleaseStatusDate < '2020/05/03' )
                         OR ( A.IndexDate < '2020/05 /03' AND A.ReleaseStatus < 3 )
                         OR ( A.IndexDate < '2020/04/26'
                              AND A.ReleaseStatus >= 3
                              AND A.ReleaseStatusDate >= '2020/05/03'
                              AND A.ReleaseStatusDate < '2020/05/09' ))) AS myTable1
#6
likeyou322020-08-14 10:27
谢谢,我也仔细研究下,刚开始学
1