【发布时间】:2014-12-18 21:02:18
【问题描述】:
我正在插入另一个无序表中的数据,但是我在 select 语句中对数据进行排序,如下所示:
INSERT INTO idmsCBStatDet
SELECT
StmNo,PageNo,TxLine,ImgStatus,ImgType,
ImgChqNo,ImgPath,ImgIndNo,TxBranchcode,
TxCustAccNo,Txbranchname,TxCustName,
TxRefNo,TxCCY,TxDate,ImgNo,TxDesc,
TxValDate,TxDebit,TxCredit,TxCustno,
TxBalance,TxChqNo,TxIndicator,InfoDate,
InfoAmt,InfoDesc,InfoImgNo,ImgPageClass,
Txacentrysrno,terminal
FROM idmsCBStatDet0262107113001
GROUP BY StmNo,PageNo,TxLine,ImgStatus,ImgType,
ImgChqNo,ImgPath,ImgIndNo,TxBranchcode,
TxCustAccNo,Txbranchname,TxCustName,
TxRefNo,TxCCY,TxDate,ImgNo,TxDesc,
TxValDate,TxDebit,TxCredit,TxCustno,TxBalance,
TxChqNo,TxIndicator,InfoDate,InfoAmt,InfoDesc,
InfoImgNo,ImgPageClass,Txacentrysrno,terminal
ORDER BY cast(txline as int) asc
查询后数据是无序的,TxLine是无序格式的。
如何确保数据以有序的形式插入
非常感谢您的帮助。
【问题讨论】:
-
SQL 表表示无序集。如果您希望数据按顺序排列,请在查询中使用
order by cast(txline as int)。 -
@GordonLinoff 我正在使用
order by cast(txline as int)对数据进行排序,但是在我查询数据时插入数据是无序的。 -
@munaialex 。 . .这就是 SQL 的工作原理。表本质上是无序的,因为它们代表 无序 集。如果您希望查询结果按特定顺序排列,则需要在查询中使用
order by。 -
除了顺序混乱之外,为什么不使用任何聚合函数(例如 MAX、SUM、COUNT)进行 GROUP BY? SELECT DISTINCT 会给你同样的结果。
-
@GordonLinoff @jarlh 感谢您的帮助。我所要做的就是删除导致数据以无序形式插入的
order by子句。但是,删除group by子句会导致数据以无序形式插入。
标签: sql sql-order-by insert-into