22,209
社区成员
发帖
与我相关
我的任务
分享
--2005
if object_id('[t]') is not null drop table [t]
go
create table [t] (A nvarchar(6),B int)
insert into [t]
select 'A01',200 union all
select 'B01',300 union all
select 'C01',400
select
stuff((select ','+A from t for xml path('')),1,1,''),
sum(b)
from t
if object_id('[t]') is not null drop table [t]
go
create table [t] (A nvarchar(6),B int)
insert into [t]
select 'A01',200 union all
select 'B01',300 union all
select 'C01',400
declare @s varchar(100)
select @s=isnull(@s+',','')+a from t
select @s,sum(b)from t
/*
---------------------------------------------------------------------------------------------------- -----------
A01,B01,C01 900
(1 個資料列受到影響)
*/
if object_id('[t]') is not null drop table [t]
go
create table [t] (A nvarchar(6),B int)
insert into [t]
select 'A01',200 union all
select 'B01',300
alter FUNCTION GET_STRING(@a VARCHAR(10))
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @CLASS VARCHAR(50)
SELECT @CLASS=ISNULL(@CLASS+',','')+a FROM t WHERE right(a,2)=right(@a,2)
RETURN @CLASS
END
select distinct dbo.GET_STRING(t.a)as a ,t1.b
from t ,(select right(a,2) a,sum(b)b from t group by right(a,2))as t1
where right(t.a,2)=t1.a
a b
-------------------------------------------------- -----------
A01,B01 500
(所影响的行数为 1 行)
if object_id('[t]') is not null drop table [t]
go
create table [t] (A nvarchar(6),B int)
insert into [t]
select 'A01',200 union all
select 'B01',300
select
stuff((select ','+A from t for xml path('')),1,1,''),
sum(b)
from t
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-28 16:01:53
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] varchar(3),[b] int)
insert [tb]
select 'A01',200 union all
select 'B01',300
--------------开始查询--------------------------
select a,b
from
(
select id,a=stuff((select ','+ltrim([a]) from (select id=1,* from tb)a where id=t.id for xml path('')), 1, 1, '') ,sum(b) as b
from (select id=1,* from tb)t
group by id
)t
----------------结果----------------------------
/* a b
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
A01,B01 500
(1 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-28 16:01:53
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] varchar(3),[b] int)
insert [tb]
select 'A01',200 union all
select 'B01',300
--------------开始查询--------------------------
DECLARE @STR VARCHAR(8000)
SELECT @STR=ISNULL(@STR+',','')+a FROM (SELECT DISTINCT a FROM tb)AS T
SELECT @STR,sum(b) from tb
----------------结果----------------------------
/* ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
A01,B01 500
(1 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-12-28 16:01:53
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([a] varchar(3),[b] int)
insert [tb]
select 'A01',200 union all
select 'B01',300
--------------开始查询--------------------------
select a,b
from
(
select id,a=stuff((select ','+ltrim([b]) from (select id=1,* from tb)a where id=t.id for xml path('')), 1, 1, '') ,sum(b) as b
from (select id=1,* from tb)t
group by id
)t
----------------结果----------------------------
/* a b
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------
200,300 500
(1 行受影响)
*/