【问题标题】:How can I eliminate the duplicate values in my answer如何消除答案中的重复值
【发布时间】:2018-09-19 09:00:08
【问题描述】:

我在 Microsoft SQL Server 中使用 AventureWorks2014 数据库。我需要创建一个视图来显示CustomerID、全名和通过网络销售给客户的总金额。

我的问题是,我似乎无法将与单个客户对应的值加起来,以便单个客户回答我的结果中的单行。这是我的代码,任何帮助将不胜感激。我基本上需要显示在网络上销售给客户的总金额。

if object_id('vTotalWebSalesPerCustomer', 'v') is not null
    drop view vTotalWebSalesPerCustomer;
go

create view vTotalWebSalesPerCustomer
as
    select distinct 
        c.CustomerID,
        ltrim(rtrim(concat(concat(concat(concat(concat(concat(concat(p.Title, ' '), p.LastName), ', '), ' '), p.FirstName), ' '), p.Suffix))) as NomClient,
        soh.TotalDue
    from
        [Sales].[Customer] as c
    left join 
        [Person].[Person] as p on c.CustomerID = p.BusinessEntityID
    left join 
        [Sales].[SalesOrderHeader] as soh on soh.CustomerID = c.CustomerID
    where 
        year(soh.OrderDate) = 2014 
        and datepart(quarter, soh.OrderDate) = 1 
        and [OnlineOrderFlag] = 1
go

select * 
from vTotalWebSalesPerCustomer

谢谢

【问题讨论】:

  • 你有select distinct,所以根据定义,结果集中没有重复的行。
  • 当我运行我的视图时,我从这个客户所做的所有不同交易中多次获得具有不同销售价值的相同 CustomerID。这就是我试图把togheter。我需要为每个客户分别计算 soh.TotalDue 的总和。
  • 您使用的是 T-SQL (MSSQL)?
  • 是的,我正在使用 transact-sql

标签: sql-server sum duplicates distinct


【解决方案1】:

听起来你需要使用 GROUP BY 和 SUM(),例如:

SELECT column-names
 FROM table-name
WHERE condition
GROUP BY column-names
ORDER BY column-names

如:

SELECT SUM(O.TotalPrice), C.FirstName, C.LastName
  FROM [Order] O JOIN Customer C 
    ON O.CustomerId = C.Id
 GROUP BY C.FirstName, C.LastName
 ORDER BY SUM(O.TotalPrice) DESC

会返回:

Sum         FirstName   LastName
117483.39   Horst       Kloss
115673.39   Jose        Pavarotti
113236.68   Roland      Mendel
57317.39    Patricia    McKenna
52245.90    Paula       Wilson
34101.15    Mario       Pontes
32555.55    Maria       Larsson

【讨论】:

    【解决方案2】:

    DISTINCT 过滤结果行以删除重复项。我认为你想要的是聚合行。也许这会做你想要的:

    create view vTotalWebSalesPerCustomer as
        select      c.CustomerID,
                            ltrim(rtrim(concat(concat(concat(concat(concat(concat(concat(p.Title, ' '), p.LastName), ', '), ' '), p.FirstName), ' '), p.Suffix))) as NomClient,
                            sum(soh.TotalDue) as TotalDue
        from [Sales].[Customer] as c
        left join [Person].[Person] as p on c.CustomerID = p.BusinessEntityID
        left join [Sales].[SalesOrderHeader] as soh on soh.CustomerID = c.CustomerID
        where year(soh.OrderDate) = 2014 and datepart(quarter, soh.OrderDate)=1 and [OnlineOrderFlag] = 1
        group by c.CustomerID,NomClient
    

    请注意,我删除了 distinct,在您想要总计的金额上添加了 sum 运算符,并按客户 ID 和名称字段分组(SQL Server 要求您在 GROUP BY 中列出所有不属于的结果列汇总)。

    【讨论】:

    • 我觉得这样可以,但是 Group By 子句中的 NomClient 有一个错误,它说列名无效。
    【解决方案3】:

    您可以在SELECT 上使用GROUP BY 来使用以下VIEW

    IF OBJECT_ID('vTotalWebSalesPerCustomer', 'v') IS NOT NULL
        DROP VIEW vTotalWebSalesPerCustomer;
    GO
    
    CREATE VIEW vTotalWebSalesPerCustomer AS
        SELECT 
            x.CustomerID,
            LTRIM(RTRIM(CONCAT(p.Title, ' ', p.LastName, ', ', p.FirstName, ' ', p.Suffix))) AS NomClient,
            x.TotalDue
        FROM (
            SELECT 
                c.CustomerID AS CustomerID,
                SUM(soh.TotalDue) AS TotalDue
            FROM [Sales].[Customer] AS c
            LEFT JOIN [Sales].[SalesOrderHeader] AS soh ON soh.CustomerID = c.CustomerID
            WHERE YEAR(soh.OrderDate) = 2014 AND DATEPART(quarter, soh.OrderDate) = 1 AND [OnlineOrderFlag] = 1
            GROUP BY c.CustomerID
        )x LEFT JOIN [Person].[Person] AS p ON x.CustomerID = p.BusinessEntityID
    GO
    

    注意:您只需要一个CONCAT 函数即可连接所有字符串值。

    【讨论】:

    • 感谢您的回答,您能帮我调整一下这个查询,以便在存在标题时显示标题吗?它似乎没有在客户姓名前打印诸如先生或女士之类的标题。即使它被插入到 NomClient 变量中(这是 NameClient 的法语)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2013-09-29
    • 2023-04-02
    相关资源
    最近更新 更多