【问题标题】:SQL Select same row (amount) timesSQL 选择同一行(数量)次
【发布时间】:2017-07-05 12:48:11
【问题描述】:
CAR       |  AMOUNT
---------------------
Ford      | 5
---------------------
Peugeot   | 7
---------------------

这是我的表,我想在结果屏幕上看到 5 次“福特”和 7 次“标致”

【问题讨论】:

  • 向我们展示您想要的确切输出。

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


【解决方案1】:
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

【讨论】:

  • 谢谢 Chanukya
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
相关资源
最近更新 更多