在SQL语句中分组?

时间:2014-01-05 00:14:17

标签: sql sql-server case-expression

我在SQL中有一个查询,我在其中提出一个人是否有付款或未付费用。在这个查询中我有一个case语句,但我需要将记录组合​​在一起以确保将付款添加到一起,以便我检查的内容是正确的。

让我展示代码,希望这会更有意义。

以下是我目前使用的查询:

Declare @tmpTable Table
(
   [Bid #] int,
   Name varchar(200),
   Spent numeric(18,2),
   Paid numeric(18,2),
   [Credit Card On File] varchar(3),
   SaleCounter int,
   Notes varchar(max)
)

Insert into @tmpTable([Bid #], Name, Spent, Paid, [Credit Card On File], SaleCounter, Notes)

Select s.[BidderNumber] as 'Bid #', ltrim(rtrim(b.bidderName)) as 'Name', isnull(s.saleprice * s.Quantity,0) as 'Spent',
isnull(t.Amount,0) as 'Paid', 
case
    when b.cconfile = 1 then 'Yes'
    else
        'No'
    end as 'Credit Card On File', 

    s.SaleCounter, isnull(t.Notes, '') as 'Notes' 

from sales s inner join Bidders b on s.BidderNumber = b.BidderNumber
  Left outer join transactions t on t.BidderNumber = s.BidderNumber 

order by s.Biddernumber, b.biddername, b.cconfile, SaleCounter

 Select [Bid #], Name, Spent as 'Total Purchases', Paid as 'Current Payments',
       case
        when [Credit Card On File] = 'Yes' then 
            case 
                when cast(Paid as numeric(18,2)) = 0 then     cast(Spent as numeric(18,2)) 
                else
                    case when (sum(cast(Paid as numeric(18,2)))) > sum(cast(Spent as numeric(18,2))) then (cast(Paid as numeric(18,2)))- sum(cast(Spent as numeric(18,2)))
                         else (cast(Spent as numeric(18,2)) - cast(Paid as numeric(18,2)))
                         end
                end

        else 0

        end as 'Amount To Charge Credit Card',
    case
        when [Credit Card On File] = 'No' then 
            case 
                when cast(Paid as numeric(18,2)) = 0 then cast(Spent as numeric(18,2)) 
                else
                    case when (sum(cast(Paid as numeric(18,2)))) > sum(cast(Spent as numeric(18,2))) then (cast(Paid as numeric(18,2)))- sum(cast(Spent as numeric(18,2)))
                         else (cast(Spent as numeric(18,2)) - cast(Paid as numeric(18,2)))
                         end
                end--sum(Outstanding)
        ELSE 0

        end as 'Outstanding Balance',  Notes
from @tmpTable 

group by [Bid #], name, spent, paid, [Credit Card On File], SaleCounter, Notes
order by [Bid #], Name, spent, paid, [Credit Card On File], SaleCounter, Notes

这是返回并插入@tmpTable的记录集:

Bid #       Name                  Total Purchases   Current Payments    Amount To Charge Credit Card    Outstanding Balance    Notes
101         Tom & Joan Bergland   7500.00           0.00                0.00                             7500.

102         John & Bonnie Black   50.00             50.00               0.00                              0.00  

108         Cindy Davidson        3600.00           1600.00             0.00                            2000.00                 250

108         Cindy Davidson        3600.00           2000.00             0.00                            1600.00 

109         Cynthia Davis         315.00            315.00              0.00                              0.00                  2355

117         Susan Harris          75.00             75.00               0.00                             0.00   

119         Jim & Julie Hill      520.00              0.00              520.00                             0.00

125         Bill & Amy Lee        526.00            526.00              0.00                             0.00

现在我的问题是,当没有余额时,我显示出价#108所欠的余额。这两个记录反映了对余额的两笔付款,当两者合计时,它们等于余额。我需要向用户显示每笔付款,但是当付款等于总购买量时,则不需要显示欠款。但是,如果有余额,则需要显示。 (信用卡付款不同......我遇到的问题是非CC付款)

有没有办法在case语句中进行分组,或者在sql中有什么东西我不知道会不会允许我需要发生什么?

我提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我怀疑你在GROUP BY中包含了一些你不需要的字段和一些你应该聚合的字段。我查看了你的输出,这就是我可以作为查询来实现它的原因。试试看,看看你是不是在尝试:

编辑:这将连接您的Notes列

SELECT
    acct.[Bid #],
    acct.Name,
    SUM(acct.Spent) AS [Total Purchases],
    SUM(acct.Paid) AS [Current Payments],
    CASE WHEN acct.[Credit Card On File] = 'Yes' THEN ABS(SUM(acct.Spent) - SUM(acct.Paid)) ELSE 0 END AS [Amount To Charge Credit Card],
    CASE WHEN acct.[Credit Card On File] = 'No' THEN ABS(SUM(acct.Spent) - SUM(acct.Paid)) ELSE 0 END AS [Outstanding Balance],
    note.Notes
FROM
    @tmpTable acct
LEFT JOIN
    (
    SELECT DISTINCT
        t2.[Bid #],
        SUBSTRING((
            SELECT ',' + t1.Notes AS [text()]
            FROM @tmpTable t1
            WHERE t1.[Bid #] = t2.[Bid #]
            ORDER BY [Bid #]
            FOR XML PATH (''))
        ,2,1000) AS Notes
    FROM
        @tmpTable t2
    ) note
    ON (acct.[Bid #] = note.[Bid #])
GROUP BY
    acct.[Bid #],
    acct.Name,
    acct.[Credit Card On File],
    note.Notes