主键,是主键的问题!

解决方案 »

  1.   

    前3个字段是KEY
    manCd studyCd dateFlg insertFlg      不考虑 不考虑 不考虑 不考虑
    303044 1101 20080101 1 NULL NULL NULL NULL
    303044 1102 20080201 1 NULL NULL NULL NULL
    303044 1102 20080218 1 NULL NULL NULL NULL
    303044 1102 20080228 2 NULL NULL NULL NULL
    303044 1103 20080301 2 NULL NULL NULL NULL
    303044 1104 20080401 2 NULL NULL NULL NULL
    303044 1105 20080501 2 NULL NULL NULL NULL
    303044 9901 20080701 2 NULL NULL NULL NULL
            ..............还有很多其他manCd的数据,不考虑。
     第一个字段是KEY
    studyCd studyMei
    1101 AA
    1102 BB
    1103 CC
    1104 DD
    1105 EE
    9901 FF
    希望得到:
    studyCd studyCd date1 date2 date3 date4 date5 state
    1101 AA 20080101 完成(就这,总是 未完成)
    1102 BB 20080201 20080218 20080228 未完成
    1103 CC 20080301 未完成
    1104 DD 20080401 未完成
    1105 EE 20080501 未完成
    9901 FF 20080701 未完成
      

  2.   


    create table 基本表(manCd varchar(3),studyCd int,dateFlg datetime,insertFlg int)
    insert into 基本表
    select '001',1001,'2007/6/24',1 union all
    select '001',1001,'2007/6/25',1 union all
    select '001',1002,'2007/7/24',1 union all
    select '001',1002,'2007/7/25',1 union all
    select '001',1002,'2007/7/26',2 union all
    select '001',1005,'2007/7/25',2 union all
    select '001',1005,'2007/7/28',2 union all
    select '001',1006,'2007/6/24',2 union all
    select '001',1007,'2007/7/25',1 union all
    select '001',1007,'2007/7/26',2 union all
    select '010',1008,'2007/7/28',2
    --> 测试数据: 内容表
    create table  内容表 (studyCd int,studyName Nvarchar(6))
    insert into 内容表
    select 1001,N'木工' union all
    select 1002,N'瓦匠' union all
    select 1005,N'水工' union all
    select 1006,N'水工工' union all
    select 1007,N'瓦匠匠' union all
    select 1008,N'木工工'
    select studyCd,studyName,[date1],[date2],[date3],[date4],[date5],insertFlgName
    from 
    (
        select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
            ,'date'+cast(ROW_NUMBER() OVER(    PARTITION BY B.studyCd    ORDER BY A.dateFlg) as varchar(10))  AS datetype
        from (    select * 
                    , insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end 
                                    from 基本表 where studyCd=Base.studyCd) 
                from 基本表 Base) A
            ,内容表 B
        where A.studyCd=B.studyCd
        group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
    ) m
    pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表
      

  3.   

    我在刚才那个贴里贴答案了:
    http://topic.csdn.net/u/20080624/18/0ca5f9a3-3dd7-4b5e-9191-1ed7fccca8f9.html?958144869
      

  4.   


    create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
    insert into 基本表
    select '303044',1101,'20080101',1 union all
    select '303044',1102,'20080201',1 union all
    select '303044',1102,'20080218',1 union all
    select '303044',1102,'20080228',2 union all
    select '303044',1103,'20080301',2 union all
    select '303044',1104,'20080401',2 union all
    select '303044',1105,'20080501',2 union all
    select '303044',9901,'20080701',2--> 测试数据: 内容表
    create table  内容表 (studyCd int,studyName Nvarchar(6))
    insert into 内容表
    select 1101,N'AA' union all
    select 1102,N'BB' union all
    select 1103,N'CC' union all
    select 1104,N'DD' union all
    select 1105,N'EE' union all
    select 9901,N'FF'select studyCd,studyName,isnull([date1],'') date1
    ,isnull([date2],'') date2
    ,isnull([date3],'') date3
    ,isnull([date4],'') date4
    ,isnull([date5],'') date5
    ,insertFlgName
    from 
    (
    select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
    ,'date'+cast(ROW_NUMBER() OVER( PARTITION BY B.studyCd ORDER BY A.dateFlg) as varchar(10))  AS datetype
    from ( select * 
    , insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end 
    from 基本表 where studyCd=Base.studyCd) 
    from 基本表 Base) A
    ,内容表 B
    where A.studyCd=B.studyCd
    group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
    ) m
    pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表/*
    1101 AA 20080101 完成
    1102 BB 20080201 20080218 20080228 未完成
    1103 CC 20080301 未完成
    1104 DD 20080401 未完成
    1105 EE 20080501 未完成
    9901 FF 20080701 未完成
    */
      

  5.   

    山羊:)你的where 条件跟要求的不一样呀。我是要求特定一个人的。
      

  6.   


    那就加多个条件:
    create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
    insert into 基本表
    select '303044',1101,'20080101',1 union all
    select '303044',1102,'20080201',1 union all
    select '303044',1102,'20080218',1 union all
    select '303044',1102,'20080228',2 union all
    select '303044',1103,'20080301',2 union all
    select '303044',1104,'20080401',2 union all
    select '303044',1105,'20080501',2 union all
    select '303044',9901,'20080701',2--> 测试数据: 内容表
    create table  内容表 (studyCd int,studyName Nvarchar(6))
    insert into 内容表
    select 1101,N'AA' union all
    select 1102,N'BB' union all
    select 1103,N'CC' union all
    select 1104,N'DD' union all
    select 1105,N'EE' union all
    select 9901,N'FF'declare @manCd  varchar(50)
    set @manCd='303044'
    select studyCd,studyName,isnull([date1],'') date1
        ,isnull([date2],'') date2
        ,isnull([date3],'') date3
        ,isnull([date4],'') date4
        ,isnull([date5],'') date5
        ,insertFlgName
    from 
    (
        select B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
            ,'date'+cast(ROW_NUMBER() OVER(    PARTITION BY B.studyCd    ORDER BY A.dateFlg) as varchar(10))  AS datetype
        from (    select * 
                    , insertFlgName=(select case when max(insertFlg)=1 then N'完成' else N'未完成' end 
                                    from 基本表 where studyCd=Base.studyCd) 
                from 基本表 Base) A
            ,内容表 B
        where A.studyCd=B.studyCd 
    and A.manCd=@manCd
        group by B.studyCd,B.studyName,A.dateFlg,A.insertFlgName
    ) m
    pivot (max(dateFlg) for datetype in ([date1],[date2],[date3],[date4],[date5])) as pivdrop table 基本表,内容表/*
    1101    AA    20080101                    完成
    1102    BB    20080201    20080218    20080228            未完成
    1103    CC    20080301                    未完成
    1104    DD    20080401                    未完成
    1105    EE    20080501                    未完成
    9901    FF    20080701                    未完成
    */
      

  7.   

    --跟刚刚那个..不是一个样??--> 测试数据: 内容表
    create table 基本表(manCd varchar(10),studyCd int,dateFlg VARCHAR(8),insertFlg int)
    insert into 基本表
    select '303044',1101,'20080101',1 union all
    select '303044',1102,'20080201',1 union all
    select '303044',1102,'20080218',1 union all
    select '303044',1102,'20080228',2 union all
    select '303044',1103,'20080301',2 union all
    select '303044',1104,'20080401',2 union all
    select '303044',1105,'20080501',2 union all
    select '303044',9901,'20080701',2--> 测试数据: 内容表
    create table  内容表 (studyCd int,studyName Nvarchar(6))
    insert into 内容表
    select 1101,N'AA' union all
    select 1102,N'BB' union all
    select 1103,N'CC' union all
    select 1104,N'DD' union all
    select 1105,N'EE' union all
    select 9901,N'FF'gowith cte as
    (
       select *,px=row_number() over(partition by studyCd order by dateFlg)
       from 基本表
    )
     select 
        a.studyCd,
        b.studyName,
        date1=max(case px when 1 then convert(varchar(10),dateFlg,111) else '' end),
        date2=max(case px when 2 then convert(varchar(10),dateFlg,111) else '' end),
        date3=max(case px when 3 then convert(varchar(10),dateFlg,111) else '' end),
        date4=max(case px when 4 then convert(varchar(10),dateFlg,111) else '' end),
        date5=max(case px when 5 then convert(varchar(10),dateFlg,111) else '' end),
        state=case 
              when exists(select * from 基本表 
                              where studyCd=a.studyCd and insertFlg=2) 
                   then '未完成' else '完成' end
      from 
          cte as a
      left outer join 内容表 b
         on a.studyCd=b.studyCd
      where 
         a.manCd='303044'
      group by 
         a.studyCd,b.studyName
      order by 
         a.studyCddrop table 基本表,内容表/*
    studyCd     studyName date1      date2      date3      date4      date5      state
    ----------- --------- ---------- ---------- ---------- ---------- ---------- ------
    1101        AA        20080101                                               完成
    1102        BB        20080201   20080218   20080228                         未完成
    1103        CC        20080301                                               未完成
    1104        DD        20080401                                               未完成
    1105        EE        20080501                                               未完成
    9901        FF        20080701                                               未完成(6 行受影响)
    */
      

  8.   

    疯了今晚我疯了结贴,不耽误各位高手的宝贵时间了。我现在需要好好睡觉,明天再看这个问题吧。我用的是SQL SERVER 2008,用大家给的都好用,可是把表名和字段名换成实际的表名和字段名。就完全变样了。结贴!!!
      

  9.   

    -  -一直都注重学习开发语言,从来不注意学习SQL语句,瘸腿的代价啊。先下了,各位SQL高人!