22,209
社区成员
发帖
与我相关
我的任务
分享
-- 改一下取结果的sql就行了
--返回结果
SELECT MIN(ID) as ID,NAMES
FROM
(
SELECT *,dbo.GetStr(ID) as NAMES FROM #rows
) t
WHERE NAMES IS NOT NULL
GROUP BY NAMES
ORDER BY MIN(ID)
-- Test Data: tb
If object_id('tb') is not null
Drop table tb
Go
Create table tb(CompanyType int,CompanyTypeName nvarchar(6))
Go
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
Go
--Start
create function f_count()
returns @t table(id int,col varchar(30))
as
begin
declare @i int
select @i= sum(CompanyType) from tb
declare @s varchar(30)
select @s = isnull(@s+',','')+CompanyTypeName from tb order by CompanyType asc
insert into @t select @i,@s
return
end
go
Select case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyType
else a.CompanyType + b.CompanyType end,
case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyTypeName
else
case when a.CompanyType < b.CompanyType then
a.CompanyTypeName+','+b.CompanyTypeName
else b.CompanyTypeName+','+a.CompanyTypeName
end
end
from tb a cross join tb b
where a.CompanyTypeName >= b.CompanyTypeName
union
select * from f_count()
order by 1
drop function f_count
/*
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
1 船舶代理公司
2 箱公司
3 船舶代理公司,箱公司
4 货代
5 船舶代理公司,货代
6 箱公司,货代
7 船舶代理公司,箱公司,货代
(所影响的行数为 7 行)
Insert into tb
--select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
2 箱公司
4 货代
6 箱公司,货代
(所影响的行数为 3 行)
Insert into tb
select 1,'船舶代理公司' union all
--select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
1 船舶代理公司
4 货代
5 船舶代理公司,货代
(所影响的行数为 3 行)
*/
/*
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
1 船舶代理公司
2 箱公司
3 船舶代理公司,箱公司
4 货代
5 船舶代理公司,货代
6 箱公司,货代
7 船舶代理公司,箱公司,货代
(所影响的行数为 7 行)
Insert into tb
--select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
2 箱公司
4 货代
6 箱公司,货代
(所影响的行数为 3 行)
Insert into tb
select 1,'船舶代理公司' union all
--select 2,'箱公司' union all
select 4,'货代'
----------- ------------------------------
1 船舶代理公司
4 货代
5 船舶代理公司,货代
(所影响的行数为 3 行)
*/
Create table tb(CompanyType int,CompanyTypeName nvarchar(6))
Go
Insert into tb
select 1,'船舶代理公司' union all
--select 2,'箱公司' union all
select 4,'货代'
Go
CREATE FUNCTION GetStr(@ID INT)
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE @str NVARCHAR(100)
SET @str=''
SELECT @str=@str+','+CompanyTypeName FROM tb
WHERE CompanyType & @ID = CompanyType
SET @str=STUFF(@str,1,1,'')
RETURN (@str)
END
GO
DECLARE @MaxType INT
SET @MaxType=(SELECT SUM(CompanyType) FROM tb)
SELECT top(@MaxType) IDENTITY(int,1,1) as ID INTO #Rows FROM sysobjects
--返回结果
SELECT *,dbo.GetStr(ID) as NAMES FROM #Rows
drop function GetStr
drop table tb
drop table #Rows
Insert into tb
--select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
Create table tb(CompanyType int,CompanyTypeName nvarchar(6))
Go
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
Go
CREATE FUNCTION GetStr(@ID INT)
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE @str NVARCHAR(100)
SET @str=''
SELECT @str=@str+','+CompanyTypeName FROM tb
WHERE CompanyType & @ID = CompanyType
SET @str=STUFF(@str,1,1,'')
RETURN (@str)
END
GO
DECLARE @MaxType INT
SET @MaxType=(SELECT SUM(CompanyType) FROM tb)
SELECT top(@MaxType) IDENTITY(int,1,1) as ID INTO #Rows FROM sysobjects
--返回结果
SELECT *,dbo.GetStr(ID) as NAMES FROM #Rows
/*
(7 行受影响)
ID NAMES
----------- ----------------------------------------------------------------------------------------------------
1 船舶代理公司
2 箱公司
3 船舶代理公司,箱公司
4 货代
5 船舶代理公司,货代
6 箱公司,货代
7 船舶代理公司,箱公司,货代
(7 行受影响)
*/
drop function dbo.GetStr
DROP TABLE tb,#Rows
CREATE FUNCTION GetStr(@ID INT)
RETURNS NVARCHAR(4000)
AS
BEGIN
DECLARE @str NVARCHAR(4000)
SET @str=''
SELECT @str=@str+','+CompanyTypeName FROM T_Common_CompanyType
WHERE CompanyType & @ID >0
SET @str=STUFF(@str,1,1,'')
RETURN (@str)
END
GO
DECLARE @MaxType INT
SET @MaxType=(SELECT SUM(CompanyType) FROM T_Common_CompanyType)
SET ROWCOUNT @MaxType
SELECT IDENTITY(int,1,1) as ID INTO #Rows FROM sysobjects
--返回结果
SELECT *,dbo.GetStr(ID) as NAMES FROM #Rows
DROP TABLE #Rows
-- Test Data: tb
If object_id('tb') is not null
Drop table tb
Go
Create table tb(CompanyType int,CompanyTypeName nvarchar(6))
Go
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
Go
--Start
create function f_count()
returns @t table(id int,col varchar(30))
as
begin
declare @i int
select @i= sum(CompanyType) from tb
declare @s varchar(30)
select @s = isnull(@s+',','')+CompanyTypeName from tb
insert into @t select @i,@s
return
end
go
Select case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyType else a.CompanyType + b.CompanyType end,
case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyTypeName else
a.CompanyTypeName+','+b.CompanyTypeName end
from tb a cross join tb b
where a.CompanyTypeName >= b.CompanyTypeName
union
select * from f_count()
order by 1
drop function f_count
/*
----------- ------------------------------
1 船舶代理公司
2 箱公司
3 箱公司,船舶代理公司
4 货代
5 货代,船舶代理公司
6 箱公司,货代
7 船舶代理公司,箱公司,货代
(所影响的行数为 7 行)
*/
--Result:
/*
*/
--End
------------------------------------
-- Author: happyflystone
-- Version:V1.001
-- Date:2008-09-12 16:55:19
------------------------------------
-- Test Data: tb
If object_id('tb') is not null
Drop table tb
Go
Create table tb(CompanyType int,CompanyTypeName nvarchar(6))
Go
Insert into tb
select 1,'船舶代理公司' union all
select 2,'箱公司' union all
select 4,'货代'
Go
--Start
Select case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyType else a.CompanyType + b.CompanyType end,
case when a.CompanyTypeName = b.CompanyTypeName then a.CompanyTypeName else
a.CompanyTypeName+','+b.CompanyTypeName end
from tb a cross join tb b
where a.CompanyTypeName >= b.CompanyTypeName
order by 1
/*
----------- -------------
1 船舶代理公司
2 箱公司
3 箱公司,船舶代理公司
4 货代
5 货代,船舶代理公司
6 箱公司,货代
(所影响的行数为 6 行)
*/
--Result:
/*
*/
--End