SQL选择相同的行(金额)次

时间:2017-02-16 12:03:37

标签: sql sql-server tsql select sql-server-2005

CAR       |  AMOUNT
---------------------
Ford      | 5
---------------------
Peugeot   | 7
---------------------

这是我的表,我希望在我的结果屏幕上看到5次"福特" 7次" Peugeot"

1 个答案:

答案 0 :(得分:3)

WITH x AS 
(
  SELECT TOP (10) rn = ROW_NUMBER() 
  OVER (ORDER BY [object_id]) 
  FROM sys.all_columns 
  ORDER BY [object_id]
)
--select * from x
SELECT car,AMOUNT
FROM x
CROSS JOIN #table2 AS d
WHERE x.rn <= d.AMOUNT
ORDER BY d.AMOUNT;

输出

car AMOUNT
Ford    5
Ford    5
Ford    5
Ford    5
Ford    5
Peugeot 7
Peugeot 7
Peugeot 7
Peugeot 7
Peugeot 7
Peugeot 7
Peugeot 7