Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在select distinct语句中按列减去第一个字符排序_Sql_Sql Server - Fatal编程技术网

Sql 如何在select distinct语句中按列减去第一个字符排序

Sql 如何在select distinct语句中按列减去第一个字符排序,sql,sql-server,Sql,Sql Server,我需要根据select distinct语句中的列按顺序组织查询。select distinct语句将字符连接到数字的前面,即S1、S2…S11 代码如下: select distinct s.book_Id as EpbookId ,concat('S',ps.Param_Value) as bookID ,concat(sa.Location,'-',s.book_Number) as Label ,concat('&

我需要根据select distinct语句中的列按顺序组织查询。select distinct语句将字符连接到数字的前面,即S1、S2…S11

代码如下:

    select distinct 
        s.book_Id as EpbookId
        ,concat('S',ps.Param_Value) as bookID
        ,concat(sa.Location,'-',s.book_Number) as Label
        ,concat('<book ID="',concat('S',ps.Param_Value),
            '" label="',concat(sa.Location,'-',s.book_Number),'"/>') as DataLine
        from 
            books s
            inner join books_Address sa on 
                s.book_Id = sa.book_Id 
                and sa.Addr_Type_Id = 1
                and s.bookS_TYPE_ID = 1
                and s.Active = 1
            inner join Parameters_books ps on 
                ps.book_Id = s.book_Id
                and ps.Param_Id = @bookParam
基本上我只需要按ps.Param_值排序 问题是我不能使用ps.Param_值的简单顺序 因为我在select distinct语句中没有ps.Param_值。 我也不能按bookID排序,因为结果没有排序 由于前面的字母正确。 我还尝试了按SUBSTRINGbookID,1,10排序,但再次尝试,除非我在select语句中放入SUBSTRINGbookID,1,10,否则它将无法工作,因为它是select distinct语句。
那么,有没有一种方法可以按照连接的“S”后面的数字进行排序,而无需在select语句中添加额外的内容。或者可以将ps.Param_值添加到我的select distinct语句中,而不实际返回到我的数据中吗?

对于您的特定示例,您可以使用

order by DataLine
ps.Param_值是字符串的第一个非常量元素,因此这应该是您想要的


更通用的解决方案是使用group by,然后使用order by minps.Param_值。

我不确定我是否100%遵循,但我相信您可以:

将ps.param_值保持在不同的位置 在计算S+concat逻辑的不同块周围添加一个select 按外部选择中的ps.param_值下单 您可能希望显式列出所有列,但为了简洁起见,我省略了它们

SELECT 
*, 
concat('S',ps.Param_Value) as bookID
FROM
(
  select distinct 
        s.book_Id as EpbookId
        ,ps.Param_Value as Param_Value
        ,concat(sa.Location,'-',s.book_Number) as Label
        ,concat('<book ID="',concat('S',ps.Param_Value),
            '" label="',concat(sa.Location,'-',s.book_Number),'"/>') as DataLine
        from 
            books s
            inner join books_Address sa on 
                s.book_Id = sa.book_Id 
                and sa.Addr_Type_Id = 1
                and s.bookS_TYPE_ID = 1
                and s.Active = 1
            inner join Parameters_books ps on 
                ps.book_Id = s.book_Id
                and ps.Param_Id = @bookPara
) A
ORDER BY
Param_Value

按concat'S',ps.param_值排序将起作用。@Hellion不幸的是,当我按concat'S',ps.param_值排序时,我得到S1,S10,S11,S12…S2,S3。因为它不再识别多位数字。首先哪个表正在创建非不同的行?谢谢你的回答!当我按数据线排序时,它返回S1、S10、S11、S12…S2、S3,我认为这是因为它不再识别多位数。请详细说明您关于使用group by和order by minps.Param_值的第二个建议。我想要什么?我还需要将minps.Param_值作为我的选择的一部分吗?谢谢你的回答,康纳。你能告诉我这会是什么样的吗?我想你遵循了我想要的,并且在正确的轨道上。我只是不确定这会是什么样子。另外,您知道嵌套select语句是否会影响查询速度吗?不,添加嵌套块通常不会对查询性能产生实质性影响。这远比连接或distinct wrt性能影响小得多,在这种情况下,这也可能不是问题-没有样本数据集和优化很难说,您认为内部distinct只对直列值进行操作会对查询有利吗,等着在更高的层次上把它们连接起来?这可能会有帮助,但我对这会有多大帮助没有强烈的意见。如果您可以将表示逻辑的内容隔离到顶部,它通常会帮助QP更容易地/潜在地进行优化。我会从应用程序的设计角度来看待这一点,而不是严格地试图让查询进行得更快。
select distinct 
EpbookId,CONCAT('S',bookID) AS bookId, Label, DataLine
from
(SELECT 
        s.book_Id as EpbookId
        ,ps.Param_Value as bookID
        ,concat(sa.Location,'-',s.book_Number) as Label
        ,concat('<book ID="',concat('S',ps.Param_Value),
            '" label="',concat(sa.Location,'-',s.book_Number),'"/>') as DataLine
        from 
            books s
            inner join books_Address sa on 
                s.book_Id = sa.book_Id 
                and sa.Addr_Type_Id = 1
                and s.bookS_TYPE_ID = 1
                and s.Active = 1
            inner join Parameters_books ps on 
                ps.book_Id = s.book_Id
                and ps.Param_Id = @bookParam
                ORDER BY ps.param_value
                ) myTempView