还是一个小计及合计的问题?

benwangz 2005-01-26 09:58:35
原表:
提货单位 品种等级 编号 数量 单价 付款方式
aaa p2-2 4001 10 10 现金
aaa p2-2 4001 12 10 现金
aaa p3-2 4002 10 20 现金
bbb p2-2 4001 20 10 预付
bbb p2-2 4001 10 10 预付


要求:
提货单位 品种等级 编号 数量 单价 付款方式
aaa p2-2 4001 22 10 现金
aaa p3-2 4002 10 20 现金
小计 32
bbb p2-2 4001 20 10 预付
小计 30
合计 62

请各位高手指点。
...全文
190 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
hero_one 2005-01-26
  • 打赏
  • 举报
回复
簡化一下
select 提貨單位=(case when 提貨單位 is not null and 品種等級 is null then '小計'
when 提貨單位 is null then '合計'
else 提貨單位
end),
品種等級 = (case when 品種等級 is null then '' else 提貨單位 end) ,
編號=( case when 品種等級 is null then '' else max(編號) end),
數量=sum(數量),
單價=max(單價),
付款方式=( case when 品種等級 is null then '' else max(付款方式) end)
from tab
group by 提貨單位,品種等級 with ROLLUP
having 提貨單位 is not null or (提貨單位 is null and 品種等級 is null)


Softlee81307 2005-01-26
  • 打赏
  • 举报
回复
參看這個例子就知道了,用isnull
Create table kc(bt varchar(3),kf varchar(3) ,bc int)
insert into kc
select 'aaa','gg',20 union all select 'aaa','b',30
union all
select 'bbb','d',10 union all select 'bbb','f',50


--------------------------下面是例子-----------------------

select bt=(case when bt is not null and kf is null then '小計'
when bt is null then '合計' else bt end)
,kf=isnull(kf,''),sum(bc) from kc group by bt,kf with cube having bt is not null or (bt is null and kf is null)
Softlee81307 2005-01-26
  • 打赏
  • 举报
回复
參看這個例子就知道了,用isnull
Create table kc(bt varchar(3),kf varchar(3) ,bc int)
insert into kc
select 'aaa','gg',20 union all select 'aaa','b',30
union all
select 'bbb','d',10 union all select 'bbb','f',50


--------------------------下面是例子-----------------------

select bt=(case when bt is not null and kf is null then '小計'
when bt is null then '合計' else bt end)
,kf=isnull(kf,''),sum(bc) from kc group by bt,kf with cube having bt is not null or (bt is null and kf is null)
hero_one 2005-01-26
  • 打赏
  • 举报
回复
select 提貨單位=(case when 提貨單位 is not null and 品種等級 is null then '小計'
when 提貨單位 is null then '合計'
else 提貨單位
end),
品種等級 = (case when 提貨單位 is null or 品種等級 is null then '' else 提貨單位 end) ,
編號=( case when 提貨單位 is null or 品種等級 is null then '' else max(編號) end),
數量=sum(數量),
單價=max(單價),
付款方式=( case when 提貨單位 is null or 品種等級 is null then '' else max(付款方式) end)
from tab
group by 提貨單位,品種等級 with ROLLUP
having 提貨單位 is not null or (提貨單位 is null and 品種等級 is null)


benwangz 2005-01-26
  • 打赏
  • 举报
回复
谢谢大家,现在问题基本解决了,但就是在‘小计’和‘合计’后的字段内会有内容,怎么把内容设置为‘null’呢?还请各位高手在给点建议。
benwangz 2005-01-26
  • 打赏
  • 举报
回复
怎么把‘小计’和‘合计’后的字段内容设为‘null’
daijingjie2002 2005-01-26
  • 打赏
  • 举报
回复
select 提货单位=case when when (grouping(提货单位)=1) and grouping(品种等级)=1 then '合计' else 提货单位 end,品种等级=case when grouping(品种等级)=1 then '小计' else 品种等级 end,编号=max(编号),数量=sum(数量),单价=max(单价),付款方式=max(付款方式) from 表 group by 提货单位,品种等级 with ROLLUP
benwangz 2005-01-26
  • 打赏
  • 举报
回复
还是没有合计啊,只有小计
Softlee81307 2005-01-26
  • 打赏
  • 举报
回复
上面發錯了,

select 提货单位=(case when 提货单位 is not null and 品种等级 is null then '小計'
when 提货单位 is null then '合計' else 提货单位 end)
,品种等级,编号=max(编号),数量=sum(数量),单价=max(单价),付款方式=max(付款方式) from 表 group by 提货单位,品种等级 with ROLLUP
having 提货单位 is not null or (提货单位 is null and 品种等级 is null)
daijingjie2002 2005-01-26
  • 打赏
  • 举报
回复
select 提货单位=case when (grouping(提货单位)=1) then '小计' else 提货单位 end,品种等级,编号=max(编号),数量=sum(数量),单价=max(单价),付款方式=max(付款方式) from 表 group by 提货单位,品种等级 with ROLLUP
Softlee81307 2005-01-26
  • 打赏
  • 举报
回复
select 提货单位,品种等级,编号=max(编号),数量=sum(数量),单价=max(单价),付款方式=max(付款方式) from 表 group by 提货单位,品种等级 with ROLLUP
having 提货单位 is not null or (提货单位 is null and 品种等级 is null)

Softlee81307 2005-01-26
  • 打赏
  • 举报
回复
給一個類的例子給你
Create table kc(bt varchar(3),kf varchar(3) ,bc int)
insert into kc
select 'aaa','gg',20 union all select 'aaa','b',30
union all
select 'bbb','d',10 union all select 'bbb','f',50


--------------------------下面是例子-----------------------

select bt=(case when bt is not null and kf is null then '小計'
when bt is null then '合計' else bt end)
,kf,sum(bc) from kc group by bt,kf with cube having bt is not null or (bt is null and kf is null)
cloudchen 2005-01-26
  • 打赏
  • 举报
回复
select 提货单位,品种等级=max(品种等级),编号,数量=sum(数量),单价=max(单价),付款方式=max(付款方式)
from 表
group by 提货单位,编号
with rollup
benwangz 2005-01-26
  • 打赏
  • 举报
回复
那合计没有啊
daijingjie2002 2005-01-26
  • 打赏
  • 举报
回复
select 提货单位,品种等级,编号=max(编号),数量=sum(数量),单价=max(单价),付款方式=max(付款方式) from 表 group by 提货单位,品种等级 with ROLLUP

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧