/ / Как мога да използвам 0 вместо NULL в SQL Server - sql, sql-server, null, обединявам

Как мога да използвам 0 вместо NULL в SQL Server - sql, sql-server, null, обединявам

Разгледайте заявката по-долу:

WITH TEMP([Company], [Company Name], [GL Description], [Amount], [BeginBal])
AS
(
SELECT
GLAC.GLCo AS Company,
HQCO.Name AS "Company Name",
GLAC.Description AS "GL Description",
SUM(GLDT.Amount) AS Amount,
GLYB.BeginBal
FROM
GLAC
LEFT JOIN
HQCO ON GLAC.GLCo = HQCO.HQCo
LEFT JOIN
GLDT ON GLAC.GLCo = GLDT.GLCo AND GLAC.GLAcct = GLDT.GLAcct
LEFT JOIN
GLYB ON GLAC.GLCo = GLYB.GLCo AND GLAC.GLAcct = GLYB.GLAcct
WHERE
GLAC.udCategory = "Cash At Bank"
AND GLAC.Active = "Y"
AND (GLAC.GLCo = 1 or GLAC.GLCo = 5 or GLAC.GLCo = 6 or GLAC.GLCo = 7)
AND GLYB.FYEMO = "2012-06-01 00:00:00"
GROUP BY
GLAC.GLCo, HQCO.Name , GLAC.Description, GLYB.BeginBal
)
SELECT
Company, [Company Name], [GL Description], Amount + BeginBal as "Balance"
FROM
TEMP

Това дава следната таблица:

 +---------+----------------------+------------------------------+-------------+
| Company |     Company Name     |        GL Description        |   Balance   |
+---------+----------------------+------------------------------+-------------+
|       1 | Contracting Pty Ltd  | Cash At Bank ANZ Cheque a/c  | -8423347.81 |
|       1 |  Contracting Pty Ltd | Investment Online Saver Acct | 15040000    |
|       1 |  Contracting Pty Ltd | Westpac Bonus Cash Reserve   | 123133      |
|       5 | ABC UTC PTY LTD      | Cash At Bank ANZ Cheque a/c  | 13121902.53 |
|       6 | ABC UTC PTY LTD      | Cash At Bank ANZ Cheque a/c  | 3932872.45  |
|       6 | ABC UTC PTY LTD      | Investment Online Saver Acct | 142448      |
|       7 |  Plant Hire Pty Ltd  | Cash At Bank ANZ Cheque a/c  | 253253.32   |
+---------+----------------------+------------------------------+-------------+

Някои от моите данни липсват и това се дължи на този ред:

GLYB.FYEMO = "2012-06-01 00:00:00"

Проблемът е, че някои записи в "GLAC" нямат стойност в GLYB, поради което нулевата стойност не се появява в таблицата ми.

Как мога да направя всички записи, ако се появи някое от тези условия:

  • GLYB.FYEMO = "2012-06-01 00:00:00"
  • GLYB.FYEMO is null

Отговори:

1 за отговор № 1

Мисля, че трябва да преместите условието on клауза, тъй като where обръща външната връзка към вътрешна връзка:

WITH TEMP([Company], [Company Name], [GL Description], [Amount], [BeginBal]) AS (
select GLAC.GLCo as Company, HQCO.Name as [Company Name], GLAC.Description as [GL Description],
SUM(GLDT.Amount) as Amount, GLYB.BeginBal
from GLAC LEFT JOIN
HQCO
ON GLAC.GLCo = HQCO.HQCo LEFT JOIN
GLDT
ON GLAC.GLCo = GLDT.GLCo and GLAC.GLAcct = GLDT.GLAcct LEFT JOIN
GLYB
ON GLAC.GLCo = GLYB.GLCo and
GLAC.GLAcct = GLYB.GLAcct and
GLYB.FYEMO = "2012-06-01 00:00:00"
where GLAC.udCategory = "Cash At Bank" and GLAC.Active = "Y"  and
(GLAC.GLCo in (1, 5, 6, 7)
Group By GLAC.GLCo, HQCO.Name , GLAC.Description, GLYB.BeginBal
)
Select Company, [Company Name], [GL Description], Amount + BeginBal as Balance
from TEMP;