高手帮忙写一条SQL语句(已附表结构)

hooyes 2014-04-14 01:40:57

DECLARE @hooyes TABLE
(
area_name VARCHAR(50) ,
area_code VARCHAR(20)
)

INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '1001', 'CA' )
INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '1002', 'CA' )
INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '1003', 'CA' )
INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '2001', 'US' )
INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '4001', 'AU' )
INSERT INTO @hooyes
( area_code, area_name )
VALUES ( '4002', 'AU' )


SELECT area_name ,
area_code
FROM @hooyes
/*
直接查询的结果:


area_name area_code
CA 1001
CA 1002
CA 1003
US 2001
AU 4001
AU 4002
*/

/*

想要一条 SQl 查出如下结果,按area_name分组,并且合并 area_code,以逗号隔开。

area_name area_code
CA 1001,1002,1003
US 2001
AU 4001,4002

*/

...全文
120 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzw_0736 2014-04-14
  • 打赏
  • 举报
回复

select DISTINCT a.area_name,b.area_code
from @hooyes a 
CROSS APPLY (SELECT STUFF((SELECT',' + area_code FROM @hooyes WHERE area_name = a.area_name FOR XML PATH('')),1, 1, '') area_code) b
  • 打赏
  • 举报
回复
[code=sql]
DECLARE @hooyes TABLE
    (
      area_name VARCHAR(50) ,
      area_code VARCHAR(20)
    )
     
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '1001', 'CA' )
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '1002', 'CA' )
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '1003', 'CA' )
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '2001', 'US' )
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '4001', 'AU' )
INSERT  INTO @hooyes
        ( area_code, area_name )
VALUES  ( '4002', 'AU' )
 
 
select area_name,
	STUFF((SELECT',' + area_code FROM @hooyes WHERE area_name = a.area_name FOR XML PATH('')),1, 1, '')
from @hooyes a
group by area_name

/*
AU	4001,4002
CA	1001,1002,1003
US	2001
*/

[/code]
yoan2014 2014-04-14
  • 打赏
  • 举报
回复
SELECT  area_name ,
        STUFF((SELECT ',' + b.area_code FROM @hooyes AS b WHERE b.area_name = a.area_name FOR XML PATH('') ) ,1,1,'')
FROM    @hooyes AS a 
GROUP BY area_name

--
area_name                                          
-------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AU                                                 4001,4002
CA                                                 1001,1002,1003
US                                                 2001

(3 row(s) affected)
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unlock; --那么这个用户名就能使用了。 --(默认全局数据库名orcl) 1、select ename, sal * 12 from emp; --计算年薪 2、select 2*3 from dual; --计算一个比较纯的数据用dual表 3、select sysdate from dual; --查看当前的系统时间 4、select ename, sal*12 anuual_sal from emp; --给搜索字段更改名称(双引号 keepFormat 别名有特殊字符,要加双引号)。 5、--任何含有空值的数学表达式,最后的计算结果都是空值。 6、select ename||sal from emp; --(将sal的查询结果转化为字符串,与ename连接到一起,相当于Java中的字符串连接) 7、select ename||'afasjkj' from emp; --字符串的连接 8、select distinct deptno from emp; --消除deptno字段重复的值 9、select distinct deptno , job from emp; --将与这两个字段都重复的值去掉 10、select * from emp where deptno=10; --(条件过滤查询) 11、select * from emp where empno > 10; --大于 过滤判断 12、select * from emp where empno 10 --不等于 过滤判断 13、select * from emp where ename > 'cba'; --字符串比较,实际上比较的是每个字符的AscII值,与在Java中字符串的比较是一样的 14、select ename, sal from emp where sal between 800 and 1500; --(between and过滤,包含800 1500) 15、select ename, sal, comm from emp where comm is null; --(选择comm字段为null的数据) 16、select ename, sal, comm from emp where comm is not null; --(选择comm字段不为null的数据) 17、select ename, sal, comm from emp where sal in (800, 1500,2000); --(in 表范围) 18、select ename, sal, hiredate from emp where hiredate > '02-2月-1981'; --(只能按照规定的格式) 19、select ename, sal from emp where deptno =10 or sal >1000; 20、select ename, sal from emp where deptno =10 and sal >1000; 21、select ename, sal, comm from emp where sal not in (800, 1500,2000); --(可以对in指定的条件进行取反) 22、select ename from emp where ename like '%ALL%'; --(模糊查询) 23、select ename from emp where ename like '_A%'; --(取第二个字母是A的所有字段) 24、select ename from emp where ename like '%/%%'; --(用转义字符/查询字段中本身就带%字段的) 25、select ename from emp where ename like '%$%%' escape '$'; --(用转义字符/查询字段中本身就带%字段的) 26、select * from dept order by deptno desc; (使用order by desc字段 对数据进行降序排列 默认为升序asc); 27、sel

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧