使用一个查询选择Multirow(但请更改)

时间:2009-09-25 19:06:29

标签: sql sql-server tsql

如何通过更改下面的查询来获得此结果。

foo
01
02
03

    declare @a as int 
    set @a = 1

    select single.* from
    (select 
    case
    when 0=0 then '0'+
        case 
        when @a = 1 then '1'
        when @a = 2 then '2'
        when @a = 3 then '3'
        end 
    end
    as foo) single
    cross join
    (select 1 as bir union all select 2 union all select 3) multi

正如你所看到的,我需要首先写'1'然后a = a + 1 in循环(在每个“when”语句中)

PS:我有一个很大的查询,我需要写三行但有一些更改。(我需要在输出-Last列下将最后两行从“A0”更改为“**”)< / p>

    TR13    BA20    1143    2009-08-31  2009-08-31  ***615134   TR74063001  40  1937,52 A0
    TR13    BA20    1143    2009-08-31  2009-08-31  ***615134   TR74063001  50  1937,52 **
    TR13    BA20    1143    2009-08-31  2009-08-31  ***615134   TR74063001  50  1937,52 **

3 个答案:

答案 0 :(得分:1)

我会诚实地对你说:我不知道问题中查询的目的是什么。唯一明显的是大部分都是徒劳的。

但是,如果我正确地理解了您的问题,那么您在表格中有一堆或多个记录,您希望生成一个结果集,其中包含每个记录的三个副本,但每个副本稍作修改。所以如果你有类似的东西:

SourceTable
--------|---------|---------|--------|
 field1 |  filed2 |  filed3 | field4 |
--------|---------|---------|--------|
   TR13 |  BA20   | 2009-08 | A0     |
   TR14 |  BA21   | 2009-08 | A1     |
   TR15 |  BA22   | 2009-08 | A2     |
   TR16 |  BA23   | 2009-08 | A3     |

你想得到这个:

   TR13 |  BA20   | 2009-08 | A0     |
   TR13 |  BA20   | 2009-08 | **     |
   TR13 |  BA20   | 2009-08 | **     |
   TR14 |  BA21   | 2009-08 | A1     |
   TR14 |  BA21   | 2009-08 | **     |
   TR14 |  BA21   | 2009-08 | **     |
   TR15 |  BA21   | 2009-08 | A2     |
   TR15 |  BA21   | 2009-08 | **     |
   TR15 |  BA21   | 2009-08 | **     |
   TR16 |  BA23   | 2009-08 | A3     |
   TR16 |  BA23   | 2009-08 | **     |
   TR16 |  BA23   | 2009-08 | **     |

然后你可以像这样编写你的查询:

 select SourceTable.field1
        , SourceTable.field2
        , SourceTable.field3
        , case when multi.bir = 1 then SourceTable.field4 else '**' end
  from SourceTable
   cross join
   (select 1 as bir union all select 2 union all select 3) multi

答案 1 :(得分:0)

您可以创建一个包含一列的表变量并在其中插入1,2,3,然后将其连接到您的数据,然后使用您的case语句。

类似的东西:

DECLARE @onetwothree table (
    num nvarchar(2)
)
INSERT into @onetwothree (num) VALUES ('01')
INSERT into @onetwothree (num) VALUES ('02')
INSERT into @onetwothree (num) VALUES ('03')

SELECT YourBigQuery.*, OneTwoThree.num  
FROM YourBigQuery, @onetwothree AS OneTwoThree

以案例陈述:

DECLARE @onetwothree table (
    num tinyint
)
INSERT into @onetwothree (num) VALUES (1)
INSERT into @onetwothree (num) VALUES (2)
INSERT into @onetwothree (num) VALUES (3)

SELECT YourBigQuery.*, (
      CASE OneTwoThree.num 
        WHEN 1 THEN YourBigQuery.YourLastColumn 
        ELSE '**'
      END
    )  
FROM YourBigQuery, @onetwothree AS OneTwoThree

答案 2 :(得分:0)

与MikyDs的答案几乎相同。

    declare @a as int 
    set @a = 1

    select 
      case 
        when multi.bir <> 1 then '**' 
        else single.foo
      end        
    from
    (select 
    case
    when 0=0 then '0'+
            case 
            when @a = 1 then '1'
            when @a = 2 then '2'
            when @a = 3 then '3'
            end 
    end
    as foo) single
    cross join
    (select 1 as bir union all select 2 union all select 3) multi

结果:

(No column name)
01
**
**