4

我正在尝试开发的查询遇到一个小挑战。

这是我的桌子的样子:-

帐户表

ClientNo        AccountType          Balance
  1234             SUP1                25
  1234            SUP1.1               35
  1234             RET1                20
  1111             SUP1                50
  1111             DIS4                60

我正在尝试得到如下所示的结果:-

ClientNo   TotSupBal   TotSuppAccts   TotRetBal  TotRetAccts  TotDisBal   TotDisAccts
 1234         70             2           20          1            0             0
 1111         50             1            0          0            60            1

本质上,一个客户可以多次出现在 Accounts 表中,因为每个客户可以有多个帐户。

帐户类型将始终以相同的字符开头,但是取决于这些帐户中有多少,该数字实际上可以是任何数字,并且后续帐户将始终是十进制然后是数字...例如,第一个 SUP 帐户只是 SUP1,但是下一个 SUP 帐户将是 SUP1.1,然后是 SUP1.2 等...

我写了以下查询

SELECT ClientNo, SUM(Balance) AS TotSupBal, COUNT(AccountType) AS TotSuppAccts
FROM Account
WHERE (AccountType LIKE 'SUP1.%') OR (AccountType = 'SUP1')
GROUP BY ClientNo

*有 2 个不同的 WHERE 子句的原因是因为我不能只使用 SUP1%,因为有像 SUP12 这样的帐户与 SUP1 不同。

此查询工作正常,但它只生成 SUP 帐户类型的列表。我怎样才能产生相同类型的输出,但是在每个帐户类型的多个列中?

我正在使用 Microsoft SQL 2008 R2

4

3 回答 3

3

PIVOT 是您所需要的 >> http://msdn.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

这是一个完全有效的解决方案:

WITH Accounts (AccountCategory, ClientNo, Balance) as (
  select 
    case 
        when AccountType like 'SUP%' then 'sup'
        when AccountType like 'RET%' then 'ret'
        when AccountType like 'DIS%' then 'dis' 
    end as AccountCategory,
    ClientNo, 
    Balance
  from Account
)
select * from (
  select ClientNo, sup as TotSupBal, ret as TotRetBal, dis as TotDisBal from Accounts as SourceTable PIVOT (
    SUM(Balance)
    FOR AccountCategory IN ([sup], [ret], [dis])
  ) as pt
) as sums inner join (
  select ClientNo, sup as TotSupAccts, ret as TotRetAccts, dis as TotDisAccts from Accounts as SourceTable PIVOT (
   COUNT(Balance)
   FOR AccountCategory IN ([sup], [ret], [dis])
  ) as pt
) as counts on sums.ClientNo = counts.ClientNo

在 SqlFiddle 上试试:http ://sqlfiddle.com/#!6/d5e91/26

于 2012-12-28T04:24:34.627 回答
0

让我假设您事先知道帐户类型是什么。在这种情况下,您只需要一个条件聚合总和:

select clientNo,
       sum(case when (AccountType LIKE 'SUP1.%') OR (AccountType = 'SUP1')
                then Balance
            end) as TotSupBal,
       sum(case when (AccountType LIKE 'SUP1.%') OR (AccountType = 'SUP1')
                then 1
                else 0
           end) as TotSupAccts,
       sum(case when left(AccountType, 3) = 'RET'
                then Balance
            end) as TotRetBal,
       sum(case when left(AccountType, 3) = 'RET'
                then 1
                else 0
           end) as TotRetAccts,
       . . .
from account
group by clientNo

我不确定其他帐户的确切逻辑是什么,所以我只看前三个字符。

于 2012-12-28T03:47:00.590 回答
0
SELECT
    ClientNo, 
    SUM(Balance) AS TotSupBal,
    COUNT(AccountType) AS TotSuppAccts,
    ret_bal AS TotRetBal,
    total_ret AS TotRetAccts
FROM 
    Account,
    (
        SELECT
            ClientNo c_num,
            SUM(Balance) AS ret_bal,
            COUNT(AccountType) total_ret
        WHERE AccountType LIKE 'RET%'
        GROUP BY ClientNo
    ) Table1RET_type  -- Your name for new table(You create new temporary table for you select) 
WHERE
    ((AccountType LIKE 'SUP1.%') OR (AccountType = 'SUP1'))
    AND Table1RET_type.c_num = ClientNo -- This is called join Table(google it for more info)
GROUP BY ClientNo

现在您必须为要创建的所有列重复此逻辑。

于 2012-12-28T03:48:49.773 回答