22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('menuset') is not null drop table menuset
if object_id('menudetail') is not null drop table menudetail
create table menuset(单号 int,序号 int,套菜名称 nvarchar(100),单价 money,数量 int)
create table menudetail(单号 int,主单序号 int,套菜序号 int,商品 nvarchar(100),单价 money,数量 int)
insert into menuset(单号,序号,套菜名称,单价,数量)
select 1,1,N'1000标准',100,1 union all
select 1,2,N'1000标准',100,1 union all
select 1,3,N'1000标准',100,1
insert into menudetail(单号,主单序号,套菜序号,商品,单价,数量)
select 1,1,1,N'鱼香肉丝',12,1 union all
select 1,1,2,N'宫保鸡丁',12,1 union all
select 1,2,1,N'鱼香肉丝',12,1 union all
select 1,2,2,N'宫保鸡丁',12,1 union all
select 1,3,1,N'鱼香肉丝',10,1 union all
select 1,3,2,N'宫保鸡丁',12,1
create function fn_getMenuDetail(@OrderID int,@MenuID int)
RETURNS nvarchar(4000)
AS
begin
declare @str nvarchar(4000)
select @str=isnull(@str+N'//','')+商品+','+convert(nvarchar(100),单价)+','+convert(nvarchar(100),数量) from menudetail as md where 单号=@OrderID and 主单序号=@MenuID order by 商品
return @str
end
select 单号,min(序号) as 序号, 套菜名称,单价,数量,count(0) as [Count] from menuset
group by 单号,套菜名称,单价,数量,dbo.fn_getMenuDetail(单号,序号)