现有一个表:
tbl:
内容:code  name  qty   other qtys
A    AAA    10    001   1
A    AAA    10    002   5
A    AAA    10    003   4
如何显示:
code  name  qty   other qtys
A     AAA    10    001   1
                   002   5
                   003   4

解决方案 »

  1.   

    为什么不在程序里去实现呢?
    写一大串sql,还不如在程序里加几个判断, sql语句主要是数据库的读取
      

  2.   

    表的结构有问题,如果设计正确的话;在2个表中,对相应字段用isnull(字段,''),再left 或者 right join 就可以;
      

  3.   

    这样的SQL谁写的出来,我就拜他为师...这也太不符合规范了.
      

  4.   

    1、用临时表、临时表变量 可以很方便的实现
    需求有些少见 :)现查出
    A     AAA    10    第一条记录To Temp再查出前三列为A     AAA    10的记录 insert 后两条记录 :)直接联接查询不太可行
      

  5.   

    --如果other這列是不重復,且有序的, 可以實現,但是效率不夠好,還是建議在前台去實現
    Select 
    (Case When B.code Is Null Then '' Else A.code End) As code,
    (Case When B.code Is Null Then '' Else A.name End) As name,
    (Case When B.code Is Null Then Null Else A.qty End) As qty,
    A.other,
    A.qtys
    From
    tbl A
    Left Join
    (Select code, name, qty, Min(other) As other From tbl Group By code, name, qty)B
    On A.code = B.code And A.name = B.name And A.qty = B.qty And A.other = B.other
      

  6.   

    --創建測試環境
    Create Table tbl
    (code Varchar(10),
     name Varchar(10),
     qty Int,
     other Char(3),
     qtys Int)
    --插入數據
    Insert tbl Select 'A',    'AAA',    10,    '001',   1
    Union All Select 'A',    'AAA',    10,    '002',   5
    Union All Select 'A',    'AAA',    10,    '003',   4
    GO
    --測試
    Select 
    (Case When B.code Is Null Then '' Else A.code End) As code,
    (Case When B.code Is Null Then '' Else A.name End) As name,
    (Case When B.code Is Null Then Null Else A.qty End) As qty,
    A.other,
    A.qtys
    From
    tbl A
    Left Join
    (Select code, name, qty, Min(other) As other From tbl Group By code, name, qty)B
    On A.code = B.code And A.name = B.name And A.qty = B.qty And A.other = B.other
    GO
    --刪除測試環境
    Drop Table tbl
    --結果
    /*
    code name qty other qtys
    A AAA 10 001 1
    NULL 002 5
    NULL 003 4
    */
      

  7.   

    为什么不在程序里去实现呢?
    写一大串sql,还不如在程序里加几个判断, sql语句主要是数据库的读取
    我觉得对。