Trying to sum distinct values SQL

后端 未结 3 1643
隐瞒了意图╮
隐瞒了意图╮ 2021-01-02 02:03

I\'m having trouble coming up with a value for a cell in SSRS, which should be a sum of distinct values. I have a SSRS report that looks similar to the below screenshot:

相关标签:
3条回答
  • 2021-01-02 02:40

    Get the distinct list first...

    SELECT SUM(SQ.COST)
    FROM
    (SELECT DISTINCT [Tracking #] as TRACK,[Ship Cost] as COST FROM YourTable) SQ
    
    0 讨论(0)
  • 2021-01-02 02:46

    try something like

    
    select sum(shipcost) from
    (select distinct tracking#, shipcost from table)
    

    cheers

    0 讨论(0)
  • 2021-01-02 02:47

    You can do the following:

    SELECT SUM(distinct [Ship Cost]) . . .
    

    But, I don't recommend this. You could have two items with the same cost and only one would be counted.

    The better way is to select one value for each Tracking #, using the row_number() function:

    select SUM(case when seqnum = 1 then [Ship Cost] end)
    from (select t.*,
                 row_number() over (partition by [Order #], [Tracking #]
                                    order by (select NULL)
                                   ) as seqnum
          . . .
         ) t
    
    0 讨论(0)
提交回复
热议问题