【问题标题】:SQL - sales values are multiplied because of duplicate values in join table, how to get around itSQL - 由于连接表中的重复值,销售额值相乘,如何解决它
【发布时间】:2018-07-05 08:46:57
【问题描述】:

我正在学习 sql(我正在使用 postgresql),我想问一下是否有人可以帮助我进行查询。

我正在尝试从销售表中获取一些数据,并将其与客户帐户表和分支表中的数据进行匹配。

以下是我的查询,它工作正常。但是我注意到一些销售值是相乘的,因为在帐户名称(客户帐户)所在的表中,一个帐户有 2 行数据。 在客户帐户表中是带有修改日期的列。所以,我确定我只能选择最近的行。但我不知道如何将它添加到我的查询中。请问有人可以帮忙吗?

select s.accountid, 
       ca.accountname, 
       s.branchnumber, 
       b.branchname, 
       sum(s.revenue) as revenue, 
       sum(s.revenue)-sum(s.cost) as gp
from sales s 
  left join customeraccount ca on s.accountid = ca.accountid and s.company = ca.company
  left join branches b on s.branchnumber = b.branchnumber and s.company = b.company
where s.company = 'FR1'
and s.salestype = 1
and s.deliverydate between '2018-01-01' and '2018-06-30'
group by s.accountid, ca.accountname, s.branchnumber, b.branchname

`

【问题讨论】:

  • 所以客户帐户有两行具有相同的 accountid 和 accountname 和 company 但不同的修改日期?
  • 帐户和国家始终相同。如果帐户被重命名,帐户名称可能会不同。因此,最近修改日期的行具有正确的数据。

标签: sql postgresql aggregate-functions


【解决方案1】:

这是我的解决方案:

此查询将为您提供 accountid + accountname + company 的唯一组合,这是每个 accountid + company 的最后修改行

SELECT customeraccount.accountid, customeraccount.accountname, customeraccount.company
FROM customeraccount 
INNER JOIN
(SELECT accountid, company, max(modified_on) AS last_modified_on
 FROM customeraccount 
 group by accountid, company) AS customeraccount_last_modified
ON  customeraccount.accountid = customeraccount_last_modified.accountid
AND customeraccount.company = customeraccount_last_modified.company
AND customeraccount.modified_on = customeraccount_last_modified.last_modified_on

解释:

  1. 我们想为每个accountid + company 确定最后修改日期(我称之为modified_on):

    SELECT accountid, company, max(modified_on) AS last_modified_on
    FROM customeraccount 
    group by accountid, company
    
  2. 现在我们要从具有modified_on 值的行中获取accountname,这是我们之前获取的最后一次修改。因此,我根据查询中三列的相等性在customeraccount 和来自 pt.1 的查询(它仅用作此查询的临时临时表)之间创建了一个连接。然后您可以从customeraccount 获取您希望的任何列,并确保您获得每个accountid + company 的唯一值(希望很清楚......)

您可以使用此查询并将其放入查询中的 FROM 子句中,确保为它创建别名。

【讨论】:

    【解决方案2】:

    正如 Sharon Ben Asher 指出的那样,我忽略了您需要修改最后一行。我更新了我的答案以反映这一点。我正在使用两个 CTE。一个称为“lastdate”,用于 accountid/company 和最后修改日期,另一个称为“customer”,用于保存与最后修改日期相等的 customeraccount 行。在 from 子句中,您只需将 customeraccount 替换为客户 CTE。

    with lastdate as (
    Select accountid, 
           company, 
           max(modified_date) modified_date
    from customeraccount
    group by accountid, 
             company )
    ,cutomers as ( 
    Select accountid, 
           company, 
           accountname
    from customeraccount 
    inner join  lastdate on  (lastdate.accountid = customeraccount.accountid and 
                lastdate.company = customeraccount.company and 
                lastdate.modified_date = customeraccount.modified_date ))
    select s.accountid, 
           ca.accountname, 
           s.branchnumber, 
           b.branchname, 
           sum(s.revenue) as revenue, 
           sum(s.revenue)-sum(s.cost) as gp
    from sales s 
      left join cutomers ca on s.accountid = ca.accountid and s.company = ca.company
      left join branches b on s.branchnumber = b.branchnumber and s.company = b.company
    where s.company = 'FR1'
    and s.salestype = 1
    and s.deliverydate between '2018-01-01' and '2018-06-30'
    group by s.accountid, ca.accountname, s.branchnumber, b.branchname
    

    -HTH

    【讨论】:

    • OP 表示她想从最后修改的行中获取值
    • @SharonBenAsher 感谢您指出这一点。我更新了我的答案。
    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2021-02-10
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多