select 型号,数量,价格
from (
select 型号,数量,价格
from 基本表
union all
select left(型号,1) as 型号,null as 数量,sum(价格) as 价格
from 基本表
group by left(型号,1)
) as t
order by 型号
from (
select 型号,数量,价格
from 基本表
union all
select left(型号,1) as 型号,null as 数量,sum(价格) as 价格
from 基本表
group by left(型号,1)
) as t
order by 型号
解决方案 »
- sql server 2005 两个字符的连接
- 请教怎么获取现有数据里的随机数
- 更改表字段的问题
- 在线:请教一个关于先进先出的问题
- 如何把表中的数据也用SQL语句生成出来
- 超级复杂的一个SQL报表需求,求高手解惑
- 想要还原网站上的数据库SQLSERVER(虚拟主机)但是提示是数据库正在使用不能还原
- osql执行脚本文件时,如何根据条件跳过剩余的语句?
- 关于SQL自动编号的问题!
- 请问那位朋友会使用ms sql的自定义函数功能,这个功能有用吗,是不是可以使用存储过程代替这功能
- **********两台数据库服务器的触发器问题?? ***********
- 在执行SQL,出现仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 'cmd_UserCommand' 中为标识列指定显式值。WHY
insert mm select 'A1' , 10 , 100
union all select 'A2' , 20 , 200
union all select 'A3' , 30 , 300union all select 'B1' , 10 , 1000
union all select 'B2' , 20 , 2000
union all select 'B3' , 30 , 3000select 型号,数量,价格
from (
select 型号,数量,价格 from mm
union all
select 型号=left(型号,1) , 数量=null , 价格=sum(价格) from mm
group by left(型号,1)
) #t
order by 型号
from (
select 型号,数量,价格 from mm
union all
select 型号=left(型号,1) , 数量=null , 价格=sum(价格) from mm
group by left(型号,1)
) #t
order by 型号
同意!
from (select 型号,数量,价格 from 基本表
union all select left(型号,1) as 型号,null as 数量,sum(价格) as 价格 from 基本表
group by left(型号,1)
)a order by a.型号
(select left(型号,1) as 型号,sum(价格) as 价格 from 基本表 group by left(型号,1)) ii
union all
select * from 基本表
(select left(型号,1) as 型号,sum(价格) as 价格 from 基本表 group by left(型号,1)) ii
union all
select * from 基本表 order by 型号
from mm group by left(型号,1)
union all
select 型号,cast(数量 as char(5)),价格 from mm
Order by 型号
A 600
B 6000 〈〈 这里不对
A1 10 100
A2 20 200
A3 30 300
〈〈 B要在这里
B1 10 1000
B2 20 2000
B3 30 3000
create table mm(型号 varchar(10),数量 int ,价格 int)
insert mm select 'A1' , 10 , 100
union all select 'A2' , 20 , 200
union all select 'A3' , 30 , 300union all select 'B1' , 10 , 1000
union all select 'B2' , 20 , 2000
union all select 'B3' , 30 , 3000select left(型号,1) as 价格 ,'' as 数量,sum(价格) as 价格 from mm group by left(型号,1)
union
select 型号,cast(数量 as varchar(5)),价格 from mmdrop table mm结果:
型号 数量 价格
A 600
A1 10 100
A2 20 200
A3 30 300
B 6000
B1 10 1000
B2 20 2000
B3 30 3000
能否不建立临时表?
from table
union
select left(型号,1),null,sum(isnull(数量,0))
from table
group by left(型号,1)
order by 型号
select left(型号,1) as 型号 ,'' as 数量,sum(价格) as 价格 from mm group by left(型号,1)
union
select 型号,cast(数量 as varchar(5)),价格 from mm
Order by 型号
from mm
union
select left(型号,1),null,sum(isnull(数量,0))
from mm
group by left(型号,1)
order by 型号测试结果如下:
型号 数量 价格
A NULL 60
A1 100 10
A2 200 20
A3 300 30
B NULL 60
B1 1000 10
B2 2000 20
B3 3000 30
create table mm(型号 varchar(10),数量 int ,价格 int)
insert mm select 'A1' , 10 , 100
union all select 'A2' , 20 , 200
union all select 'A3' , 30 , 300union all select 'B1' , 10 , 1000
union all select 'B2' , 20 , 2000
union all select 'B3' , 30 , 3000
select 型号,数量,substring(型号,1,1) as a,价格 into #tttt from mm select (
case when Grouping(型号)=1 and Grouping(a)=0 then '合计'
when Grouping(a)=1 and Grouping(型号)=1 then '总计'
else 型号
end) as 型号,
--数量,
sum(价格) as 价格 from #tttt group by a,型号 WITH ROLLUP
having Grouping(型号)=1 or Grouping(a)=0 drop table #tttt
drop table mm
else left(型号,patindex('%[0-9]%',型号+'0')-1) end
,数量=sum(数量),价格=sum(价格)
from 基本表
group by left(型号,patindex('%[0-9]%',型号+'0')-1)
,型号 with rollup
having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
order by 型号
else left(型号,patindex('%[0-9]%',型号+'0')-1) end
,数量=case grouping(型号) when 1 then ''
else cast(sum(数量) as varchar) end
,价格=sum(价格)
from 基本表
group by left(型号,patindex('%[0-9]%',型号+'0')-1)
,型号 with rollup
having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
order by 型号
create table 基本表(型号 varchar(10),数量 int,价格 int)
insert 基本表 select 'A1',10,100
union all select 'A2',20,200
union all select 'A3',30,300
union all select 'B1',10,1000
union all select 'B2',20,2000
union all select 'B3',30,3000
go--查询处理
select 型号=case grouping(型号) when 0 then 型号
else left(型号,patindex('%[0-9]%',型号+'0')-1) end
,数量=case grouping(型号) when 1 then ''
else cast(sum(数量) as varchar) end
,价格=sum(价格)
from 基本表
group by left(型号,patindex('%[0-9]%',型号+'0')-1)
,型号 with rollup
having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
order by 型号
go--删除测试
drop table 基本表/*--测试结果型号 数量 价格
---------- -------- ---------
A 600
A1 10 100
A2 20 200
A3 30 300
B 6000
B1 10 1000
B2 20 2000
B3 30 3000(所影响的行数为 8 行)
--*/
是考虑到你一型号可能是这样的:AA1
AA2
BBB1
BBB2即总类长度不固定的情况,所以没有写死,而是用了patindex来动态判断
如果不存在这个问题,型号是固定的,则直接用left(型号,1)的效率会高一些
drop table [dbo].[temp1]
gocreate table temp1(型號 varchar(20) not null,數量 int null,價格 int null)
goinsert into temp1
select 'A1',10,100 union
select 'A2',20,200 union
select 'A3',30,300 union
select 'B1',10,1000 union
select 'B2',20,2000 union
select 'B3',30,3000goselect * from temp1 order by 1
/*
型号 数量 价格
A1 10 100
A2 20 200
A3 30 300
B1 10 1000
B2 20 2000
B3 30 3000
*/select 型號,isnull(cast(數量 as varchar(10)),'') as 數量,價格 from
(
select left(型號,len(型號)-1) as 型號,NULL as 數量,sum(價格) as 價格 from temp1 group by left(型號,len(型號)-1)
union all
select * from temp1
) a
order by 1
/*
型號 數量 價格
A 600
A1 10 100
A2 20 200
A3 30 300
B 6000
B1 10 1000
B2 20 2000
B3 30 3000
*/
方法1:采用Grouping 聚合函数实现分组,特点:灵活,方便,代码维护性强.select 型号=case grouping(型号) when 0 then 型号
else left(型号,patindex('%[0-9]%',型号+'0')-1) end
,数量=case grouping(型号) when 1 then ''
else cast(sum(数量) as varchar) end
,价格=sum(价格)
from 基本表
group by left(型号,patindex('%[0-9]%',型号+'0')-1)
,型号 with rollup
having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
order by 型号
方法2:采用嵌套的方法,语句简单,直观,灵活性不足。如果记录多,最好不要采用。
select 型號,isnull(cast(數量 as varchar(10)),'') as 數量,價格 from
(
select left(型號,len(型號)-1) as 型號,NULL as 數量,sum(價格) as 價格 from temp1 group by left(型號,len(型號)-1)
union all
select * from temp1
) a
order by 1
else left(型号,patindex('%[0-9]%',型号+'0')-1) end
,数量=case grouping(型号) when 1 then ''
else cast(sum(数量) as varchar) end
,价格=sum(价格)
from 基本表
group by left(型号,patindex('%[0-9]%',型号+'0')-1)
,型号 with rollup
having grouping(left(型号,patindex('%[0-9]%',型号+'0')-1))=0
order by 型号
这样的语句我能否在DELPHI中调用执行??
是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。仅在与包含 CUBE 或 ROLLUP 运算符的 GROUP BY 子句相联系的选择列表中才允许分组。语法
GROUPING ( column_name )参数
column_name是 GROUP BY 子句中用于检查 CUBE 或 ROLLUP 空值的列。返回类型
int注释
分组用于区分由 CUBE 和 ROLLUP 返回的空值和标准的空值。作为CUBE 或 ROLLUP 操作结果返回的 NULL 是 NULL 的特殊应用。它在结果集内作为列的占位符,意思是"全体"。示例
下面的示例将 royalty 的数值分组,并聚合 advance 的数值。GROUPING 函数应用于 royalty 列。USE pubs
SELECT royalty, SUM(advance) 'total advance',
GROUPING(royalty) 'grp'
FROM titles
GROUP BY royalty WITH ROLLUP结果集在 royalty 下显示两个空值。第一个 NULL 代表从表中这一列得到的空值组。第二个 NULL 在 ROLLUP 操作所添加的汇总行中。汇总行显示的是所有 royalty 组的 advance 合计数值,并且在 grp 列中用 1 标识。下面是结果集:royalty total advance grp
--------- --------------------- ---
NULL NULL 0
10 57000.0000 0
12 2275.0000 0
14 4000.0000 0
16 7000.0000 0
24 25125.0000 0
NULL 95400.0000 1