创建一个总行以显示ms sql中列项的总差异

时间:2012-10-30 09:18:56

标签: sql sql-server-2008

我正在尝试创建一个从列中获取项目的总行,并显示总字段中的差异而不是总和。这在SQL中是否可行?

SELECT P.Part_Number, P.Units_On_Hand 
FROM Part AS P 
WHERE P.Part_Number IN ('AX12', 'AZ52')

这是我目前要显示的项目,我需要进行处理。如何修改它以包含总列以显示差异?

1 个答案:

答案 0 :(得分:0)

;with cte as
(
    select part_number, SUM(units_on_hand) total
    from yourtable
    where part_number IN ('AX12', 'AZ52')
    group by part_number
)
    select t1.part_number, t2.part_number, abs(t1.total-t2.total)
    from cte t1
        inner join cte t2
        on t1.part_number<t2.part_number