则:
select cast(產品id as varchar) as 產品id,入庫數量,convert(varchar(10),入庫日期,120) as 入庫日期 from tb
union all
select cast(產品id as varchar)+'小计',sum(入庫數量),null from tb group by 產品id
order by 產品id
则:
select cast(產品id as varchar) as 產品id,入庫數量,convert(varchar(10),入庫日期,120) as 入庫日期 from tb
union all
select cast(產品id as varchar)+'小计',sum(入庫數量),null from tb group by 產品id
order by 產品id
create table tb(id int,產品id numeric(10),入庫數量 numeric(10),入庫日期 datetime)
Insert into tb
select '1','1','10','2004-12-11'
union all select '2','2','15','2004-12-11'
union all select '3','1','20','2004-05-22'
union all select '4','2','5','2004-12-25'
select * from tb
--查詢結果
select 產品id=case when 入庫日期 is null then '小計' else cast(產品id as varchar) end ,入庫數量,入庫日期 from
(select 產品id,入庫數量=sum(入庫數量),入庫日期
from tb group by 產品id ,入庫日期 with rollup)a
where 產品id is not null
create table tb(id int,產品id numeric(10),入庫數量 numeric(10),入庫日期 datetime)
Insert into tb
select '1','1','10','2004-12-11'
union all select '2','2','15','2004-12-11'
union all select '3','1','20','2004-05-22'
union all select '4','2','5','2004-12-25'
select * from tb
--查詢結果
select 產品id=case when 入庫日期 is null then '小計' else 產品id end ,入庫數量,入庫日期 from tb
union all
select 產品id,入庫數量=sum(入庫數量),null from tb group by 產品id
order by 1,3 desc