樓主修改一下:--函數,合並字符串欄位
--測試數據
/*
CREATE table person_info(dept_name varchar(10),position_name varchar(10),person_name varchar(10))
INSERT INTO person_info
SELECT '資訊部','軟件編碼員','小李'
UNION ALL SELECT '資訊部','軟件編碼員','小王'
UNION ALL SELECT '行政部','秘書','小芳'
*/
--合並函數
CREATE FUNCTION FunMergeCharField(@vchA varchar(10),@vchB varchar(10))
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r=''
SELECT @r=@r+','+person_name FROM person_info WHERE dept_name=@vchA and position_name=@vchB
RETURN(substring(@r,2,8000))
END
GO
--删除测试
DROP TABLE person_info
DROP FUNCTION FunMergeCharField--調用
SELECT dept_name,position_name,在職人員=dbo.FunMergeCharField(dept_name,position_name) 
FROM person_info 
GROUP BY dept_name,position_name
go

解决方案 »

  1.   

    select distinct train from (select a.* from tablename a join (select * from tablename where station='广安') b on a.train=b.train where a.station='重庆') c
      

  2.   

    有一个火车经过站点详细信息的表,表中内容如下id(加1递增字段)
    train(车次的名称)
    station(车站的名称)下边是数据表中的内容(下面所有的信息都是经过站点的信息,开车方向是从上向下开)
    直达就是经过这两个车站的意思。1 1003 重庆
    2 1003 北碚
    3 1003 广安
    4 1004 庆北
    5 1004 北碚
    6 1004 广安
    7 1005 广安
    8 1005 北碚
    9 1005 重庆
    ……
    还有很多类似的数据前提:数据表的结构不能变要求功能:得到“重庆”到“广安”的直达列车的所有车次名称。我得用什么查询语句才可以?用SQL 查询语句和能实现此功能的存储过程都可以。
      

  3.   

    zheninchangjiang(john shu) 
    您好,麻烦您能详细解释一下你的语句大概什么意思,我的SQL查询不是特别熟,麻烦了。select distinct train from (select a.* from tablename a join (select * from tablename where station='广安') b on a.train=b.train where a.station='重庆') c其中的a,b,c是什么意思?
    a.*
    a.train
    b.train
    又是什么意思?谢谢。
      

  4.   

    select distinct train from (select a.* from tablename a join (select * from tablename where station='广安') b on a.train=b.train where a.station='重庆' and b.id>a.id) c
      

  5.   

    再次补充说明:刚才说的还是不十分清晰有一个火车经过站点详细信息的表,表中内容如下表名:infoid(加1递增字段)
    train(车次的名称)
    station(车站的名称)下边是数据表中的内容(下面所有的信息都是经过站点的信息,开车方向是从上向下开)
    直达就是经过这两个车站的意思。1 1003 重庆
    2 1003 北碚
    3 1003 广安
    4 1004 庆北
    5 1004 北碚
    6 1004 广安
    7 1005 广安
    8 1005 北碚
    9 1005 重庆
    ……
    还有很多类似的数据------------------------------------------------------------------
                      前提:数据表的结构和内容都不能变
    ------------------------------------------------------------------
    要求功能:得到“重庆”到“广安”的直达列车的所有车次名称。我得用什么查询语句才可以?用SQL 查询语句和能实现此功能的存储过程都可以。谢谢大家的帮忙。
      

  6.   

    a,b.c 是表或查询的别名,方便使用或者为了识别,如a.* 相当于tablename.*
      

  7.   

    --查询的函数
    create function f_qry(
    @start_station varchar(10),
    @end_station varchar(10)
    )returns table
    as
    return(select train from 表 a 
    where station=@start_station and exists(
    select 1 from 表 where station=@end_station 
    and train=a.train and id>a.id)
    group by train)
    go--调用进行查询
    select * from f_qry('重庆','广安')
      

  8.   

    将我的查询中的tablename 换成info 就行了
      

  9.   

    --测试--测试数据
    create table 表(id int identity(1,1),train char(4),station varchar(10))
    insert 表 select '1003','重庆'
    union all select '1003','北碚'
    union all select '1003','广安'
    union all select '1004','庆北'
    union all select '1004','北碚'
    union all select '1004','广安'
    union all select '1005','广安'
    union all select '1005','北碚'
    union all select '1005','重庆'
    union all select '1005','广安'
    go--查询的函数
    create function f_qry(
    @start_station varchar(10),
    @end_station varchar(10)
    )returns table
    as
    return(select train from 表 a 
    where station=@start_station and exists(
    select 1 from 表 where station=@end_station 
    and train=a.train and id>a.id)
    group by train)
    go--调用进行查询
    select * from f_qry('重庆','广安')
    go--删除测试
    drop table 表
    drop function f_qry/*--测试结果
    train 
    ----- 
    1003
    1005(所影响的行数为 2 行)
    --*/
      

  10.   

    --写成函数是为了方便以后查询和调用上的方便--如果直接查询得到“重庆”到“广安”的直达列车的所有车次名称。--这样一句就够了:select train from 表 a 
    where station'重庆' and exists(
    select 1 from 表 where station='广安' 
    and train=a.train and id>a.id)
    group by trai
      

  11.   

    zheninchangjiang(john shu)
    谢谢你,你的查询方法,即简单,查询速度又快。我再测试一下其他人的方法,看看有没有更好的。等一下,我给你加分
      

  12.   

    zjcxc(邹建)
    你好,你的函数办法也非常方便,谢谢您,等一下给您加分
      

  13.   

    咱也贴上一个
    select * from 
    (select * from 表 where station='重庆') a 
    join (select * from 表 where station='广安') b
    on a.train=b.train
    where b.id>a.id