AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 325841
Accepted
sql-noob
sql-noob
Asked: 2023-04-11 06:15:53 +0800 CST2023-04-11 06:15:53 +0800 CST 2023-04-11 06:15:53 +0800 CST

为什么我的行重复并错误地显示总和?

  • 772

我有这些表:

Payments

vendor   | VendorLocation | VendorName   | CheckDate | Amount | TransactionID | InvoiceID
---------------------------------------------------------------------
777      |                | Sony        | 5/1/2020   | 1.50   | 345           | ABC555
777      |                | Sony        | 5/1/2020   | 1.20   | 444           | ABC555
777      |  103           | Sony Music  | 5/1/2020   | 8.50   | 888           | XYZ888
777      |  105           | Sony VR     | 5/1/2020   | 2.50   | 789           | XYZ999
777      |  105           | Sony VR     | 5/1/2020   | 2.50   | 833           | XYZ111
Vendor Location

vendor   | VendorLocation | LocationType |Address 1   | State | City
---------------------------------------------------------------------
777      |  103           | X            |123 Ave      | CA    | Rivertown
777      |  105           | Z            |666 Ave      | CA    | Northtown
777      |  106           | Z            |888 Ave      | CA    | Southtown
Vendor Address

vendor   | VendorLocation | Address 1  | State | City
---------------------------------------------------------------------
777      |                | 1 Main St  | CA    | Rivertown
777      |  103           | 123 Ave    | CA    | Rivertown
777      |  105           | 666 Ave    | CA    | Northtown

我想要的输出是:

Vendor Spending

vendor   | VendorLocation | LocationType  | VendorName | TotalTransactions|TotalSpend| Address 1
---------------------------------------------------------------------
777      |                | Z             | Sony       |  2                | $2.70   | 1 Main St
777      |  105           | Z             | Sony VR    |  2                | $5.00   | 666 Ave

但是,我得到的结果低于重复且不正确的 totalSpend 金额:

Vendor Spending

vendor   | VendorLocation | LocationType  | VendorName | TotalTransactions|TotalSpend| Address 1
---------------------------------------------------------------------
777      |                | Z             | Sony       |  2                | $2.70   | 1 Main St
777      |  105           | Z             | Sony VR    |  2                | $7.70   | 1 Main St


This is query I have:

select distinct
ap.vendor Vendor_ID
,vl.VendorLocation
,vl.LocationType
,ap.VendorName
,count(ap.transactionID) as TotalTransactions
,sum( ap.amount) as TotalSpend
,ad.Address1
from payments ap
join  VendorLocation vl
on ( (vl.vendor=ap.vendor and vl.VendorLocation=ap.VendorLocation)
or (vl.vendor=ap.vendor and vl.VendorLocation='')
or (vl.vendor=ap.vendor and ap.VendorLocation='')
and vl.LocationType in ('Z')
) join VENDORADDRESS ad
on (ad.VENDOR=ap.vendor AND ad.VendorLocation=ap.VendorLocation)
where
vl.LocationType in ('Z') and
ap.CheckDate>='4/1/2022'
group by
vl.vendor
,vl.LOCATION_CODE
,vl.LocationType
,ap.VendorName
,ad.Address1
sql-server-2016
  • 1 1 个回答
  • 24 Views

1 个回答

  • Voted
  1. Best Answer
    J.D.
    2023-04-11T07:04:04+08:002023-04-11T07:04:04+08:00

    您有一些相当非规范化的表。在多个表(例如Address 1)中重复相同的数据点是多余的、低效的,最重要的是会导致数据完整性差。我建议修复您的表格设计。


    除此之外,您的查询可以像这样简化:

    ;WITH _VendorTransactionTotals AS
    (
        SELECT
            vendor, 
            VendorLocation, 
            VendorName,
            COUNT(1) AS TotalTransactions,
            SUM(Amount) AS TotalSpend
        FROM Payments
        WHERE CheckDate >= '4/1/2022'
        GROUP BY vendor, VendorLocation, VendorName
    )
    
    SELECT
        VTT.vendor, 
        VTT.VendorLocation,
        VL.LocationType,
        VTT.VendorName,
        VTT.TotalTransactions,
        VTT.TotalSpend,
        VL.[Address 1]
    FROM _VendorTransactionTotals AS VTT
    INNER JOIN [Vendor Location] AS VL
        ON VTT.vendor = VL.vendor
        AND VTT.VendorLocation = VL.VendorLocation
    WHERE VL.LocationType = 'Z'
    

    通过首先使用 CTE 聚合您的Payments表,这会在加入表之前对其进行重复数据删除Vendor Location。这也使查询更具可读性并简化了代码。


    请注意,对于其中没有奇数字符(例如空格)的表和列,您还应该使用标准名称。


    顺便说一句,您的原始查询的问题很可能出在这里:

    or (vl.vendor=ap.vendor and vl.VendorLocation='')
    or (vl.vendor=ap.vendor and ap.VendorLocation='')
    and
    

    这就是说,Vendor Location表中任何带有空白的 VendorLocation行都可以连接到表中所有相同的行,反之亦然,表中任何带有空白的行都可以连接到表中所有相同的行。vendorPaymentsPayments VendorLocationVendorVendor Location

    此外,不清楚表格Vendor Address与Vendor Location表格的关系,尤其是空白地址。你怎么知道1 Main Strelates to LocationType = 'Z'?澄清这一点可能有助于确定您的目标是否可行以及如何最好地实现它。

    • 1

相关问题

  • 消息 39011 SQL Server 无法与 LaunchPad 服务通信

  • Where 子句过滤 Azure Stretch 数据库的行?

  • SQL Server 2016 标准版中是否会包含临时表?

  • 无法在 Windows 10 上启动 SQL Server 2016 CTP3 服务

  • SQL Server 2016 中具有规范化架构的行级安全性

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve