UNION ALL не возвращает повторяющиеся значения - PullRequest
0 голосов
/ 04 ноября 2019

UNION ALL не возвращает строки с повторяющимися значениями

Пробные соединения слева, внешние соединения и правые соединения

SELECT '8' AS period,
       CONVERT(varchar(30), Invoicedate, 103) AS InvoiceDate,
       'D' AS Debtorflag,
       drnumber,
       invno AS invno,
       unknown_0 AS [Description],
       drvalue,
       sumcol1 AS ValueIncl,
       SUBSTRING(vatflag, 1, 2) AS vat_included,
       sumcol2 AS Vat,
       '' AS unknown_1,
       '' AS unknown_2,
       ledgercode,
       '1' AS unknown_3,
       '1' AS unknown_4,
       '0' AS unknown_5,
       '0' AS unknown_6,
       '0' AS unknown_7,
       sumcol3 AS total_value
FROM (SELECT loadno,
             '8' AS period,
             DATEADD(dd, 0, DATEDIFF(dd, 0, invdate)) AS Invoicedate,
             'D' AS Debtorflag,
             [tbldebtor].drnumber,
             invno,
             CASE
                  WHEN CONVERT(nvarchar(MAX), orderno) = '' THEN CONVERT(nvarchar(MAX), [tblLoads].loadno)
                  ELSE CONVERT(nvarchar(MAX), orderno)
             END AS unknown_0,
             DrValueIncl AS sumcol1,
             DrVatCode AS vatflag,
             Drvat AS sumcol2,
             '' AS unknown_1,
             '' AS unknown_2,
             ledgercode,
             '1' AS unknown_3,
             '1' AS unknown_4,
             '0' AS unknown_5,
             '0' AS unknown_6,
             '0' AS unknown_7,
             DrValueIncl AS sumcol3,
             drvalue
      FROM [tblLoads]
           LEFT JOIN [tbldebtor] ON [tblLoads].drno = [tbldebtor].drname
      WHERE (Finalized = 1
         AND (Exported IS NULL
           OR Exported <> 1))
      GROUP BY loadno,
               DATEADD(dd, 0, DATEDIFF(dd, 0, invdate)),
               [tbldebtor].drnumber,
               drvat,
               drvatflag,
               invno,
               orderno,
               [tblLoads].loadno,
               ledgercode,
               Drvalueincl,
               drVatCode,
               drvalue
      UNION ALL
      SELECT '',
             '8' AS period,
             DATEADD(dd, 0, DATEDIFF(dd, 0, invdate)) AS Invoicedate,
             'D' AS Debtorflag,
             [tbldebtor].drnumber,
             invno,
             CASE
                  WHEN CONVERT(nvarchar(MAX), orderno) = '' THEN CONVERT(nvarchar(MAX), l.loadno)
                  ELSE CONVERT(nvarchar(MAX), orderno)
             END AS unknown_0,
             a.Value AS sumcol1,
             a.vatcode AS vatflag,
             a.Vat AS sumcol2,
             '' AS unknown_1,
             '' AS unknown_2,
             GLcode AS ledgercode,
             '1' AS unknown_3,
             '1' AS unknown_4,
             '0' AS unknown_5,
             '0' AS unknown_6,
             '0' AS unknown_7,
             a.value AS sumcol3,
             valueexcl AS DrValue
      FROM [tblLoads] l
           LEFT JOIN [tbldebtor] ON l.drno = [tbldebtor].drname
           LEFT JOIN tbladdinvoice a ON l.loadno = a.loadno
      WHERE (Finalized = 1
         AND (Exported IS NULL
           OR Exported <> 1))
      GROUP BY DATEADD(dd, 0, DATEDIFF(dd, 0, invdate)),
               [tbldebtor].drnumber,
               vat,
               drvatflag,
               invno,
               orderno,
               l.loadno,
               GLcode,
               Drvalueincl,
               drvatcode,
               vatcode,
               value,
               vat,
               valueexcl,
               a.loadno) tblz
WHERE (invno <> ''
   AND INVNO IS NOT NULL)
GROUP BY CONVERT(varchar(30), InvoiceDate, 103),
         drnumber,
         vatflag,
         invno,
         unknown_0,
         ledgercode,
         sumcol1,
         sumcol2,
         sumcol3,
         drvalue
HAVING sumcol1 > 0;

Например, две строки содержат одинаковое количество (4200,00), однаковозвращается только одна строка

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...