sql - 需要使用自定义模式在另一个表中插入数据
问题描述
我有像这些列一样的表名“table1”:
id code price
1 1000 5.3
2 1001 4.3
3 1002 6.5
4 1003 7.5
我需要将这些数据插入到表“table2”中,如下所示:
id code price code1 price2
1 1000 5.3 1001 4.3
2 1002 6.5 1003 7.5
问题:这可能吗?如果是这样,我该怎么做?
- 注意:我在网站上搜索了很多,但没有找到我想要的。
解决方案
您可以使用以下方法将偶数和奇数代码放在单独的列中:
select row_number() over (order by min(code)) as id,
max(case when code % 2 = 1 then code end) as id_1,
max(case when code % 2 = 1 then price end) as price_1,
max(case when code % 2 = 0 then code end) as id_2,
max(case when code % 2 = 0 then price end) as price_2
from t
group by ceiling(code / 2.0)
推荐阅读
- python - 在 Django 模板中显示文本文件的内容
- javascript - 是否可以在 window.prompt 弹出窗口上显示移动数字键盘?
- flutter - 如何在颤动中将日期时间转换为持续时间?
- angular - ngOnChanges 不会触发
- ios - iOS - 在方向更改时重新启动应用程序?
- c++ - 无法删除堆内存上的指针
- javascript - 如何使用 React JS 映射我需要以 JSON 格式对日期属性进行分组的对象数组
- php - PHP 上的模数结果错误,但 Python 上的结果正确
- python - 如何从列表列表中创建对象?
- python - GtkSourceView/Buffer 崩溃:Gtk-ERROR: Byte index is off the end of the line