从表中多次选择记录

时间:2011-12-30 11:38:31

标签: sql sql-server sql-server-2008 tsql

我有一个这样的SQL语句:

编辑:

SELECT 
    location as Location 
FROM
    Table1 
WHERE 
    OnsiteOffshore = 'Offshore' AND Acc_Code = 'ABC' 

UNION 

SELECT 
    Country 
FROM 
    Table1 
WHERE
    OnsiteOffshore = 'Onsite' AND Acc_Code = 'ABC'

此SQL查询提供以下结果:

Chennai
Bangalore
USA
NewZealand

但由于某些要求,我需要输出如下:

Chennai
Chennai
Chennai
Chennai
Bangalore
Bangalore
Bangalore
Bangalore
USA
USA
USA
USA
NewZealand
NewZealand
NewZealand
NewZealand

意思是说每个位置需要输出4次。

请帮忙解决问题。

7 个答案:

答案 0 :(得分:12)

SELECT Location 
FROM Table1
  CROSS JOIN
    ( VALUES (1),(2),(3),(4)
    ) AS four(dummy)

如果4不是常量但是(因为@xQbert注意/询问)是表的行数,您可以使用:

SELECT a.Location 
FROM Table1 AS a
  CROSS JOIN
     Table1 AS b

如果您没有Table1但任何(但是很复杂的)查询,您可以将其用于4份副本:

SELECT Location 
FROM (
       SELECT Location       --- complex query here
       ...                   --- inside parenthesis
     UNION 
       SELECT Country
       ...
     ) AS Table1
  CROSS JOIN
    ( VALUES (1),(2),(3),(4)
    ) AS four(dummy)

n份副本:

WITH cte AS
  ( SELECT Location       --- complex query here
    ...                   --- inside parenthesis
    UNION 
    SELECT Country
    ...
  )
SELECT a.Location 
FROM cte AS a
  CROSS JOIN
     cte AS b

答案 1 :(得分:6)

在任何RDBMS中最简单且(可能)完全接受; - ):

select location from (
    Select Location From Table1 union all
    Select Location From Table1 union all
    Select Location From Table1 union all
    Select Location From Table1
) t
order by location

使用CTE (Common Table Expressions)更好的方式:

;with cte (id) as (
    select 1 union all
    select id + 1 from cte where id < 4
)
select location from Table1
cross join cte

答案 2 :(得分:3)

您可以为此创建一个包装程序...其中,您首先为Select Location From Table1创建游标,而不是循环浏览此游标并以任何方式提取数据。

您可以选择使用某些前端技术来执行此操作。对于前端语言,即Java,C ++,.NET或任何其他流行语言,这不是一项艰巨的任务。

答案 3 :(得分:2)

仅在Table1至少有4条记录

时才有效
SELECT Location
FROM Table1 a, (select top 4 id from Table1) b 
order by a.Location

答案 4 :(得分:2)

DECLARE @counter int
DECLARE @max int = 4

CREATE TABLE #myTable (
    Name nvarchar(50)
)

SET @counter = 0

WHILE @counter < @max

BEGIN
    INSERT INTO #myTable SELECT cityname FROM citytable

    SET @counter = @counter + 1
END


SELECT name FROM #myTable ORDER BY name

DROP TABLE #myTable

答案 5 :(得分:0)

select t1.location 
from table1 t1, table1 t2

应返回n的n个位置。因此,如果地点包含10个条目,其中一个是美国,美国将列出10次。这会根据输入的位置数量进行缩放。

或更现代的语法:

Select t1.location
from table1 t1
Cross join table1 t2

注意缺少ON条款;那是因为我们想要自我加入的笛卡尔积。

- 编辑反映已更改的SQL

Select A.Location 
FROM (
  Select  location as Location 
  from Table1 where OnsiteOffshore = 'Offshore' and Acc_Code = 'ABC' 
  UNION 
  Select  Country 
  from Table1 where OnsiteOffshore = 'Onsite' and Acc_Code = 'ABC') A
CROSS JOIN (
  Select  location as Location 
  from Table1 where OnsiteOffshore = 'Offshore' and Acc_Code = 'ABC' 
  UNION 
  Select  Country 
  from Table1 where OnsiteOffshore = 'Onsite' and Acc_Code = 'ABC') B

答案 6 :(得分:0)

8 - )

Select Location From Table1 UNION ALL
Select Location From Table1 UNION ALL
Select Location From Table1 UNION ALL
Select Location From Table1 
ORDER BY 1