在MS Access SQL中排序联合查询

时间:2012-02-23 11:08:16

标签: ms-access sorting union

好的,我有一个特别讨厌的工会订购问题,所以任何帮助都会受到赞赏。

情景如下:

会员表,包含以下记录(实际数据):

REI882
YUI987
POBO37
NUBS26
BTBU12
MZBY10
TYBW54

(这些按我希望他们从我的查询中返回的顺序列出。)

关于这些MemberID的构建有很多业务规则,我认为这些规则与该类别无关。他们是历史性的,并且是一成不变的。我被他们困住了。它们表明该成员的资历。

从ID中的最后4个字符开始,按升序排序。就排序而言,ID的前两个字符完全没有意义。

所以最可能的记录是?A001(最高级)和最低记录是?ZZ99(最低级)。

当我查询我的会员表时,我回来的列表必须显示最高级别的顶级...显然标准排序不起作用。这是我迄今为止所做的:

这些查询中的第一个处理排序成员,其ID只有1个前导字母。第二部分涉及有两个主要信件的人。

SELECT * FROM (

    SELECT Member.ID
    FROM Member
    WHERE (((IsNumeric(Mid([Member.ID],4,1)))=-1)) **check the 4th character is a digit
    ORDER BY (Mid([Member.ID],3,1)), (Mid([Member.ID],4,1)), (Mid([Member.ID],5,1)), (Mid([Member.ID],6,1))
) t1

UNION

SELECT * FROM (
    SELECT Member.ID
    FROM Member
    WHERE (((IsNumeric(Mid([Member.ID],4,1)))=0)) **check the 4th character is a letter
    ORDER BY (Mid([Member.ID],3,1)), (Mid([Member.ID],4,1)), (Mid([Member.ID],5,1)), (Mid([Member.ID],6,1))
) t2

但是我和工会一起得到了疯狂的结果!如果我单独运行每个选项 - 没有问题我的时髦(严重依赖于访问中的一些讨厌的字符串操作!)排序完全按照我的意愿工作。

我知道这很复杂,但我希望我已经清楚地解释了这一点,并且有人为了解决这个问题而获得一些荣誉!

编辑:我查询的结果似乎是随机的:

YUI987
MZBY10
NUBS26
BTBU12
REI882
POBO37
TYBW54

3 个答案:

答案 0 :(得分:1)

ORDER BY语句中的{p> SELECT UNION与另一个SELECT不正确。

请参阅指定条件订单 here

您可以使用:

SELECT ID FROM(  
(SELECT Member.ID,1 AS T,Left([Member.ID],2) AS Part1, Right([Member.ID],4) AS Part2
  FROM Member
  WHERE (((IsNumeric(Mid([Member.ID],3,1)))=-1)))    
UNION    
  (SELECT Member.ID,2 AS T,Left([Member.ID],3) AS Part1, Right([Member.ID],3) AS Part2
  FROM Member
  WHERE (((IsNumeric(Mid([Member.ID],4,1)))=-1) and ((IsNumeric(Mid([Member.ID],3,1)))=0)))     
UNION    
  (SELECT Member.ID,3 AS T,Left([Member.ID],4) AS Part1, Right([Member.ID],2) AS Part2
  FROM Member
  WHERE (((IsNumeric(Mid([Member.ID],5,1)))=-1) and ((IsNumeric(Mid([Member.ID],4,1)))=0)))     

ORDER BY T,Part1,Part2)

@Justin Kirk:我不知道你的问题究竟是什么。但我希望它可以帮到你

答案 1 :(得分:0)

为什么不使用RIGHT功能。

这样的东西
SELECT  ID
FROM    (
            SELECT  ID 
            FROM    (
                        SELECT Member.ID
                        FROM Member
                        WHERE (((IsNumeric(Mid([Member.ID],4,1)))=-1)) **check the 4th character is a digit
                    ) t1
            UNION
            SELECT  ID 
            FROM    (
                        SELECT Member.ID
                        FROM Member
                        WHERE (((IsNumeric(Mid([Member.ID],4,1)))=0)) **check the 4th character is a letter
                    ) t2
        ) t3
ORDER BY RIGHT(ID,4)

答案 2 :(得分:0)

如何跳过UNION?

SELECT members.ID
FROM members
ORDER BY Right([ID],3), Right(id,4)

根据新规则,这种混乱可能有效。

SELECT 
    Len(IIf([textId] Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Left([textid],2),
    IIf([textId] Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Left([textid],3),
    IIf([textId] Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Left([textid],4),"_")))) AS Ln, 

    IIf(textId Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Left(textid,2),
    IIf(textId Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Left(textid,3),
    IIf(textId Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Left(textid,4),"_"))) AS Alpha, 

    IIf(textId Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Val(Right(textid,4)),
    IIf(textId Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Val(Right(textid,3)),
    IIf(textId Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Val(Right(textid,2)),0))) AS Numbr, 

    table.textid
FROM table
ORDER BY 
    Len(IIf([textId] Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Left([textid],2),
    IIf([textId] Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Left([textid],3),
    IIf([textId] Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Left([textid],4),"_")))), 

    IIf(textId Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Left(textid,2),
    IIf(textId Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Left(textid,3),
    IIf(textId Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Left(textid,4),"_"))), 

    IIf(textId Like "[a-z][a-z][0-9][0-9][0-9][0-9]",Val(Right(textid,4)),
    IIf(textId Like "[a-z][a-z][a-z][0-9][0-9][0-9]",Val(Right(textid,3)),
    IIf(textId Like "[a-z][a-z][a-z][a-z][0-9][0-9]",Val(Right(textid,2)),0)))