SQL添加几行并从具有日期约束的三个表中减去完全陷入困境

时间:2017-02-28 17:42:53

标签: sql-server join add

我试图从两个不同的表中提取总和,并将它们加到第一个表中。第一个是客户,第二个是按月销售,第三个是回报。如果我加入最后两个中的任何一个与第一个,我可以使它工作,但当试图把整个事情放在一起我只是疯狂的nums。任何帮助都会非常感激!

第一个查询,工作正常:

SELECT  
    CUST.CODE, CUST.NAME,
    SUM (CASE 
            WHEN (DNVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) - 1, -1))
               THEN DNVI.AMMO 
            ELSE 0 
         END) AS DCURK
FROM 
    BAESQL_CO3.dbo.CUST CUST
LEFT OUTER JOIN 
    BAESQL_CO3.dbo.DNVI DNVI ON CUST.CODE = DNVI.CSTC
WHERE 
    CUST.STAT = 'A' 
    AND (DNVI.STAT = 'E' OR DNVI.STAT = 'O')
GROUP BY 
    CUST.CODE, CUST.NAME
ORDER BY 
    CUST.CODE

第二个查询,确定:

SELECT CUST.CODE, CUST.NAME, (VCURK - DCURK) as NETA

我需要得到:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10b75f6a8>
Traceback (most recent call last):
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/rajababu/anaconda/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/Users/rajababu/anaconda/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 948, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'import_export'

我尝试的任何东西都有效......

2 个答案:

答案 0 :(得分:2)

只需将两个查询作为子查询加在一起,然后进行数学运算:

SELECT [Q1].[CODE], [Q1].[NAME], COALESCE([Q1].[VCURK], 0) - COALESCE([Q2].[DCURK], 0) AS [NETA]
FROM (
    SELECT  CUST.CODE, CUST.NAME,
            SUM (CASE WHEN (INVI.DTTE>
            DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1))
            THEN INVI.AMMO ELSE 0 END) AS VCURK

    FROM    BAESQL_CO3.dbo.CUST CUST
            LEFT OUTER JOIN BAESQL_CO3.dbo.INVI INVI
            ON CUST.CODE = INVI.CSTC

    WHERE CUST.STAT = 'A' AND (INVI.STAT = 'E' OR INVI.STAT = 'O')

    GROUP BY CUST.CODE, CUST.NAME
) AS [Q1] INNER JOIN (
    SELECT  CUST.CODE, CUST.NAME,
            SUM (CASE WHEN (DNVI.DTTE>
            DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1))
            THEN DNVI.AMMO ELSE 0 END) AS DCURK

    FROM BAESQL_CO3.dbo.CUST CUST

            LEFT OUTER JOIN BAESQL_CO3.dbo.DNVI DNVI
            ON CUST.CODE = DNVI.CSTC

    WHERE CUST.STAT = 'A' AND (DNVI.STAT = 'E' OR DNVI.STAT = 'O')

    GROUP BY CUST.CODE, CUST.NAME
) AS [Q2] ON [Q1].[CODE] = [Q2].[CODE]
ORDER BY [Q1].[CODE]

答案 1 :(得分:0)

让我们从第一个查询开始。由于我们使用open(panel) { panel.classList.remove("collapse"); } ,我们可以将LEFT OUTER JOIN条件移动到CASE子句,以及限制ON ed表的WHERE子句#39;仅限自己的价值观。在JOIN子句中的AND (INVI.STAT = 'E' OR INVI.STAT = 'O') ed表上使用JOIN条件有效地将WHERE变为LEFT OUTER JOIN,但我们可以稍后解决此问题:

INNER JOIN

请注意,现在我们SELECT CUST.CODE, CUST.NAME, COALESCE(SUM(INVI.AMMO), 0) AS VCURK FROM BAESQL_CO3.dbo.CUST CUST LEFT OUTER JOIN BAESQL_CO3.dbo.INVI INVI ON CUST.CODE = INVI.CSTC AND INVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) -1, -1) AND (INVI.STAT = 'E' OR INVI.STAT = 'O') WHERE CUST.STAT = 'A' GROUP BY CUST.CODE, CUST.NAME ORDER BY CUST.CODE WHEREGROUP BY都只引用主ORDER BY表。

第二个查询与另一个表CUST完全相同,因此我们可以JOIN将其重新计入第一个:

JOIN

现在与原始查询的唯一区别在于,SELECT CUST.CODE, CUST.NAME, COALESCE(SUM(INVI.AMMO), 0) - COALESCE(SUM(DNVI.AMMO), 0) AS NETA FROM BAESQL_CO3.dbo.CUST CUST LEFT OUTER JOIN BAESQL_CO3.dbo.INVI INVI ON CUST.CODE = INVI.CSTC AND (INVI.STAT = 'E' OR INVI.STAT = 'O') AND INVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) -1, -1) LEFT OUTER JOIN BAESQL_CO3.dbo.DNVI DNVI ON CUST.CODE = DNVI.CSTC AND (DNVI.STAT = 'E' OR DNVI.STAT = 'O') AND DNVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) - 1, -1) WHERE CUST.STAT = 'A' GROUP BY CUST.CODE, CUST.NAME ORDER BY CUST.CODE ed表中没有任何匹配记录的记录仍会在此JOIN的组合查询中返回。要复制原始结果,我们可以(如果实际需要)在NETA = 0子句中使用一个额外条件:

WHERE

这样查询不仅更短,更具可读性,而且效率更高:没有SELECT CUST.CODE, CUST.NAME, COALESCE(SUM(INVI.AMMO), 0) - COALESCE(SUM(DNVI.AMMO), 0) AS NETA FROM BAESQL_CO3.dbo.CUST CUST LEFT OUTER JOIN BAESQL_CO3.dbo.INVI INVI ON CUST.CODE = INVI.CSTC AND (INVI.STAT = 'E' OR INVI.STAT = 'O') AND INVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) -1, -1) LEFT OUTER JOIN BAESQL_CO3.dbo.DNVI DNVI ON CUST.CODE = DNVI.CSTC AND (DNVI.STAT = 'E' OR DNVI.STAT = 'O') AND DNVI.DTTE > DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()) - 1, -1) WHERE CUST.STAT = 'A' AND ( INVI.STAT IS NOT NULL OR INVI.STAT IS NOT NULL OR DNVI.STAT IS NOT NULL OR DNVI.STAT IS NOT NULL ) GROUP BY CUST.CODE, CUST.NAME ORDER BY CUST.CODE 语句,只有一个CASE和日期限制GROUP BY ed表扫描。< / p>