T-SQL:丑陋的SQL

时间:2016-05-02 03:55:58

标签: sql-server tsql

编辑:大家都明白,我并不担心格式化,我担心GROUP By的使用以及聚合字段的使用情况,因为它没有多大意义。< / p>

我的任务是让一些SQL更具可读性。虽然我一般都知道该怎么做,但这个特殊的查询让我感到厌烦。查询的要点涉及作者按一大堆字段进行分组,并将这些字段添加到查询结果中。对于他/她不是GROUP BY的字段,他们使用MIN聚合函数我想“让错误消失”

MIN(ISNULL(dbo.V_CONNECT_ContactPartnerDetail_0010.SAPNr, N'')) AS sapkunr, 

我的困难来自于我可以将GROUP BY填充到CTE中,然后从那里分支出来,但我从来没有得到行计数来匹配我创建的查询和原始查询。任何有助于使这个SQL更具可读性并使其意图更清晰(没有使错误消失的功能)的帮助将不胜感激。

SELECT dbo.V_CONNECT_ContactPartnerDetail_0010.ID_FI                    AS firmencode,
       dbo.V_CONNECT_ContactPartnerDetail_0010.ID_KP                    AS partnercode,
       dbo.V_CONNECT_ContactPartnerDetail_0010.Nachname,
       Min(Isnull(dbo.V_CONNECT_ContactPartnerDetail_0010.Vorname, '')) AS vname,
       Min(CASE V_CONNECT_ContactPartnerDetail_0010.Anrede
             WHEN 'Frau' THEN 2
             ELSE 1
           END)                                                         AS anrede,
       Min(Isnull(dbo.V_CONNECT_ContactPartnerDetail_0010.EMail, N''))  AS mail,
       Min(Isnull(dbo.V_CONNECT_ContactPartnerDetail_0010.SAPNr, N''))  AS sapkunr,
       Isnull(dbo.V_CONNECT_ContactPartnerDetail_0010.Titel, N'')       AS titel
FROM   dbo.V_CONNECT_ContactPartnerDetail_0010
       INNER JOIN dbo.V_CONNECT_ContactPartnerPivot
               ON dbo.V_CONNECT_ContactPartnerDetail_0010.ID_C005 = dbo.V_CONNECT_ContactPartnerPivot.ID_C005
       LEFT OUTER JOIN dbo.V_CONNECT_Firmen_PZ_Download
                    ON dbo.V_CONNECT_ContactPartnerDetail_0010.ID_VF = dbo.V_CONNECT_Firmen_PZ_Download.ID_VF
WHERE  ( dbo.V_CONNECT_ContactPartnerDetail_0010.VKO = '0010' )
GROUP  BY dbo.V_CONNECT_ContactPartnerDetail_0010.ID_FI,
          dbo.V_CONNECT_ContactPartnerDetail_0010.ID_KP,
          dbo.V_CONNECT_ContactPartnerDetail_0010.Nachname,
          dbo.V_CONNECT_ContactPartnerDetail_0010.Ort,
          dbo.V_CONNECT_ContactPartnerPivot.flg_spl,
          dbo.V_CONNECT_ContactPartnerPivot.flg_ha,
          dbo.V_CONNECT_ContactPartnerPivot.flg_fu,
          dbo.V_CONNECT_ContactPartnerPivot.flg_ma,
          dbo.V_CONNECT_ContactPartnerPivot.flg_ph,
          Isnull(dbo.V_CONNECT_ContactPartnerDetail_0010.Titel, N'') 

1 个答案:

答案 0 :(得分:5)

这是更多&#34;可读&#34;给我

 SELECT cpd.ID_FI                                          AS firmencode
      , cpd.ID_KP                                          AS partnercode
      , cpd.Nachname                                       AS Nachname
      , MIN(ISNULL( cpd.Vorname  ,''))                     AS vname
      , MIN(CASE cpd.Anrede WHEN 'Frau' THEN 2 ELSE 1 END) AS anrede
      , MIN(ISNULL( cpd.EMail    ,N''))                    AS mail
      , MIN(ISNULL( cpd.SAPNr    ,N''))                    AS sapkunr
      , ISNULL( cpd.Titel        ,N'')                     AS titel
   FROM dbo.V_CONNECT_ContactPartnerDetail_0010  cpd 
   JOIN dbo.V_CONNECT_ContactPartnerPivot  cpp 
     ON cpd.ID_C005 = cpp.ID_C005
   LEFT
   JOIN dbo.V_CONNECT_Firmen_PZ_Download  fpd
     ON fpd.ID_VF = cpd.ID_VF
  WHERE cpd.VKO = '0010'
  GROUP
     BY cpd.ID_FI
      , cpd.ID_KP
      , cpd.Nachname
      , cpd.Ort
      , cpp.flg_spl
      , cpp.flg_ha
      , cpp.flg_fu
      , cpp.flg_ma
      , cpp.flg_ph
      , ISNULL(cpd.Titel ,N'')

修改

如果我的任务是使某些SQL更具可读性,那么我将从上面的更改开始。

除此之外,还不清楚为什么GROUP BY子句包含SELECT列表中不包含的表达式。这样做是有效的。但是,有什么好奇的是,如果来自&#34; cpd&#34;具有不同的&#34; Ort&#34;的值,然后返回多行,并使用相同的值&#34; ID_FI&#34;,&#34; ID_KP&#34;, &#34; Nachname&#34;

真正突出的是外部联接到&#34; fpd&#34;,除了引用&#34; ID_VF&#34;在连接条件中的列,没有任何对来自&#34; fpd&#34;的列的引用。查询中的任何其他位置。似乎如果删除了外部联接,我们会得到相同的结果。

我建议的第一个结构改变是删除加入&#34; fpd&#34;。

 SELECT cpd.ID_FI                                          AS firmencode
      , cpd.ID_KP                                          AS partnercode
      , cpd.Nachname                                       AS Nachname
      , MIN(ISNULL( cpd.Vorname  ,''))                     AS vname
      , MIN(CASE cpd.Anrede WHEN 'Frau' THEN 2 ELSE 1 END) AS anrede
      , MIN(ISNULL( cpd.EMail    ,N''))                    AS mail
      , MIN(ISNULL( cpd.SAPNr    ,N''))                    AS sapkunr
      , ISNULL( cpd.Titel        ,N'')                     AS titel
   FROM dbo.V_CONNECT_ContactPartnerDetail_0010  cpd 
   JOIN dbo.V_CONNECT_ContactPartnerPivot        cpp 
     ON cpp.ID_C005 = cpd.ID_C005
  WHERE cpd.VKO = '0010'
  GROUP
     BY cpd.ID_FI
      , cpd.ID_KP
      , cpd.Nachname
      , cpd.Ort
      , ISNULL( cpd.Titel        ,N'')
      , cpp.flg_spl
      , cpp.flg_ha
      , cpp.flg_fu
      , cpp.flg_ma
      , cpp.flg_ph

我们可以在GROUP BY子句中重新排列表达式,以移动&#34; Title&#34;来自&#34; cpd&#34;的其他列。如果没有ORDER BY子句,则无法保证返回行的顺序。

我们无法告知(来自查询,以及提供的信息)&#34; ID_C005&#34; column是'#34; cpd&#34;中的PRIMARY KEY或UNIQUE KEY。或&#34; cpp&#34;。

如果不知道,我们无法在不改变结果的情况下对查询进行其他更改。如果&#34; ID_C005&#34;在&#34; cpp&#34;中是独一无二的,那么我们就可以消除所有的&#34; cpp&#34; GROUP BY的列引用。

如果内部联接的目的(到&#34; cpp&#34;)是过滤掉&#34; cpd&#34;的行。在&#34; cpp&#34;中没有匹配的行,我们可以对查询进行一些其他更改。这可能会使它更具有可读性#34;