我有如下表格中的数据。
现在我想根据quantity列的值返回行。
像上面的数据行1有数量4,那么在select查询的输出中,行1将有4行,类似于行2,数量是8,那么将有8行。
当我在上面的数据上写查询时,它应该返回26行。

最佳答案

查询:
SQLFIDDLEExample

SELECT tbl.*
FROM tbl,
    (SELECT number
    FROM master..spt_values
    WHERE Type = 'P') n
WHERE tbl.Quantity > n.number

结果:
| ROWNO | QUANTITY | SIZE |
|-------|----------|------|
|     1 |        4 |  YXS |
|     1 |        4 |  YXS |
|     1 |        4 |  YXS |
|     1 |        4 |  YXS |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     2 |        8 |  2XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     3 |        7 |   XL |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |
|     4 |        7 |  YXS |

10-06 06:36