首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

有哪位高手可以实现这个查询功能

2013-06-19 
有谁可以实现这个查询功能table1ndb004ndb005ndb006NDB00720130302660221120130303660102-1 1032013030366

有谁可以实现这个查询功能
table1
ndb004        ndb005ndb006NDB007
201303026602211
20130303660102-1 103
201303036602041
201303036602061
20130303660204-1
201303036602211
201303036602041
20130303100101-1 201

在数据库里直接得到这个表

talbe2
ndb004              ndb005                             ndb006
20130302660221-1
20130303660221,660204,660206,660204,660221,660204-1



sql语句
[解决办法]

----------------------------
-- Author  :DBA_Huangzj(發糞塗牆)
-- Date    :2013-05-28 11:42:30
-- Version:
--      Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) 
--Jun 17 2011 00:54:03 
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1, v.721)
--
----------------------------
--> 测试数据:[table1]
if object_id('[table1]') is not null drop table [table1]
go 
create table [table1]([ndb004] datetime,[ndb005] int,[ndb006] int,[NDB007] int)
insert [table1]
select '20130302',660221,1,null union all
select '20130303',660102,-1,103 union all
select '20130303',660204,1,null union all
select '20130303',660206,1,null union all
select '20130303',660204,-1,null union all
select '20130303',660221,1,null union all
select '20130303',660204,1,null union all
select '20130303',100101,-1,201
--------------开始查询--------------------------

SELECT  a.[ndb004] ,
       
        STUFF(( SELECT  ',' + CONVERT(VARCHAR(128),[ndb005])
                FROM    [table1] b
                WHERE   b.[ndb004] = a.[ndb004]
                        AND b.[ndb006] = a.[ndb006]
              FOR
                XML PATH('')
              ), 1, 1, '') '[ndb005]',  a.[ndb006]
FROM    [table1] a
GROUP BY a.[ndb004] ,


        a.[ndb006] 

----------------结果----------------------------
/* 
ndb004                  [ndb005]                                                                                                                                                                                                                                                         ndb006
----------------------- ---------------------------------------------------------------------------------------------------------------- -----------
2013-03-02 00:00:00.000 660221                                                                                                                                                                                                                                                           1
2013-03-03 00:00:00.000 660102,660204,100101                                                                                                                                                                                                                                             -1


2013-03-03 00:00:00.000 660204,660206,660221,660204    
*/


[解决办法]
借用下版主大佬的数据

if object_id('[table1]') is not null drop table [table1]
go 
create table [table1]([ndb004] datetime,[ndb005] int,[ndb006] int,[NDB007] int)
insert [table1]
select '20130302',660221,1,null union all
select '20130303',660102,-1,103 union all
select '20130303',660204,1,null union all
select '20130303',660206,1,null union all
select '20130303',660204,-1,null union all
select '20130303',660221,1,null union all
select '20130303',660204,1,null union all
select '20130303',100101,-1,201
--------------开始查询--------------------------
select ndb004,ndb005=
stuff((select ','+cast(ndb005 as varchar) from table1 where ndb004=t.ndb004 for XML path('')),1,1,''),
MIN(ndb006) ndb006
from table1 t group by ndb004

热点排行
Bad Request.