SQL中的单条件选择查询

时间:2015-08-05 07:55:07

标签: sql sql-server sql-server-2000

我有下表说文件名。

myname

我想要来自此表AND的所有不同文件名 如果任何文件名的标志为1,我希望将该文件名替换为4个文件名,如FilaA_part_1,FilaA_part_2,FilaA_part_3,FilaA_part_4。

输出应该是

filename   flag
fileA       1
fileB       0
fileC       0
fileD       1
fileA       1

我可以使用临时表来实现这一点。我想知道单选查询是否可行。

我可以达到

FilaA_part_1
FilaA_part_2
FilaA_part_3
FilaA_part_4
fileB      
fileC
FilaD_part_1
FilaD_part_2
FilaD_part_3
FilaD_part_4     

5 个答案:

答案 0 :(得分:1)

试试这应该在sql server 2000中工作

SELECT filename + '_part' + num
FROM   Yourtable
       CROSS JOIN (SELECT '_1' num
                   UNION ALL
                   SELECT '_2'
                   UNION ALL
                   SELECT '_3'
                   UNION ALL
                   SELECT '_4') A
WHERE  flag = 1
UNION ALL
SELECT filename
FROM   Yourtable B
WHERE  NOT EXISTS (SELECT 1
                   FROM   Yourtable A
                   WHERE  a.filename = b.filename
                          AND a.flag = 1) 

SQLFIDDLE DEMO

答案 1 :(得分:0)

工会应该这样做:

SELECT FILENAME
FROM FILENAMES
WHERE FLAG = 0
UNION 
SELECT FILENAME + '_part_1'
FROM FILENAMES
WHERE FLAG = 1
UNION 
SELECT FILENAME + '_part_2'
FROM FILENAMES
WHERE FLAG = 1
UNION 
SELECT FILENAME + '_part_3'
FROM FILENAMES
WHERE FLAG = 1
UNION 
SELECT FILENAME + '_part_4'
FROM FILENAMES
WHERE FLAG = 1

答案 2 :(得分:0)

declare @i varchar(10) 
set @i =1 
create table #temp ([filename] varchar(50)) 
while (@i <= 4) 
begin 
    insert into #temp([filename]) 
    SELECT distinct 
           case when flag = 1 then [filename]+'Part_'+@i 
                              else [filename] 
           end [filename] 
    FROM ( 
        SELECT 'fileA'[filename] ,1 flag 
        UNION ALL 
        SELECT 'fileB',0 
        UNION ALL 
        SELECT 'fileC', 0 
        UNION ALL 
        SELECT 'fileD', 1 
        UNION ALL 
        SELECT 'fileA' , 0 
    ) BASE 
    set @i= @i+1 
end 
select distinct [filename] from #temp

答案 3 :(得分:0)

尝试此查询。

SELECT filename + '_part' + num
FROM   FILENAMES
   CROSS JOIN (SELECT '_1' num
               UNION ALL
               SELECT '_2'
               UNION ALL
               SELECT '_3'
               UNION ALL
               SELECT '_4') A
WHERE  flag = 1
UNION ALL
SELECT filename
FROM   FILENAMES B
WHERE  NOT EXISTS (SELECT 1
               FROM   FILENAMES A
               WHERE  a.filename = b.filename
                      AND a.flag = 1)

答案 4 :(得分:0)

这是您在Q中发布的确切输出,我希望这会对您有所帮助

declare @temp table
(filenames nvarchar(44),   flag int
)
insert into @temp values ('fileA',      1)
insert into @temp values ('fileB',      0)
insert into @temp values ('fileC',      0)
insert into @temp values ('fileD',      1)
insert into @temp values ('fileA',      0);

; with cte as(
select filenames,flag from @temp t1 where not exists (select 1 from @temp t2 where t1.filenames = t2.filenames AND t2.flag = 1)
union 
select filenames + '_part_1',flag from @temp where flag = 1
union                       
select filenames + '_part_2',flag from @temp where flag = 1
union                       
select filenames + '_part_3',flag from @temp where flag = 1
union                       
select filenames + '_part_4',flag from @temp where flag = 1)

select * FROM cte