【问题标题】:ORDER BY table structure instead of numerical [closed]ORDER BY 表结构而不是数字[关闭]
【发布时间】:2021-09-13 06:25:48
【问题描述】:

我当前的 MsSQL 查询如下所示:

all =  'SELECT Master_Sub_Account , cAccountTypeDescription , Debit , Credit FROM [Kyle].[dbo].[PostGL] AS genLedger'\
                ' Inner JOIN [Kyle].[dbo].[Accounts] '\
                'on Accounts.AccountLink = genLedger.AccountLink '\
                'Inner JOIN [Kyle].[dbo].[_etblGLAccountTypes] as AccountTypes '\
                'on Accounts.iAccountType = AccountTypes.idGLAccountType'\
                ' WHERE genLedger.AccountLink not in (161,162,163,164,165,166,167,168,122)'\
                ' AND genLedger.TxDate > ?'\
                ' ORDER BY iAccountType'

我需要 ORDER BY iAccountType 来显示数据在表格中的放置顺序,而不是数字。

当前输出如下所示(请参阅帐户列)

但我需要它看起来像这样:

【问题讨论】:

  • 删除 django 和 python 标签,因为这似乎无关紧要
  • he order that the data is placed in the table 到底是什么意思?
  • 没有“数据放入表中的顺序”。在 SQL 中,表是 无序 行的集合。如果您想知道何时将行插入到表中,则由将记录此类详细信息的数据放在表中的适当列中。

标签: sql sql-server sql-order-by


【解决方案1】:

根据随附的屏幕截图,它按帐户列 (ASC) 排序。那是你需要的吗?如果是,那么您必须使用 ASC 关键字。

这是一个例子:

create table #tmpTable (col1 varchar(10))

insert into #tmpTable
values ('3020>010')
 ,('2750>020')
 ,('1000>180')
 ,('1000>001')
 ,('3620>011')
 ,('3200')
 ,('3850')

 select col1
 from #tmpTable
 order by col1 asc

 drop table #tmpTable

结果必须是:

1000>001
1000>180
2750>020
3020>010
3200
3620>011
3850

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多