订单联合查询

时间:2016-07-03 22:47:56

标签: sql ms-access

所以我有这个sql:

class BigBaseClass
{
public:
    virtual ~BigBaseClass() {}

    virtual BigBaseClass* clone() const { return new BigBaseClass(*this); }
};

class BigDerivedClass : public BigBaseClass
{
public:
    BigDerivedClass* clone() const override { return new BigDerivedClass(*this); }
};

class A
{
    BigBaseClass *ptr;

public:
    explicit A(BigBaseClass* ob);
    ~A() { delete ptr; }
};

A::A(BigBaseClass* ob) : ptr(ob)
{
}

int main()
{
    A a(new BigDerivedClass);
}

除了按字母顺序排序外,它给了我想要的结果。是一种定制输出顺序的方法,所以代替:

SELECT TOP 3 Members.ChildsName, Members.MembershipType,  Results.TotalPoints
FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
WHERE (((Members.MembershipType)="Senior"))
ORDER BY Members.MembershipType, Results.TotalPoints
UNION
SELECT TOP 3 Members.ChildsName, Members.MembershipType, Results.TotalPoints
FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
WHERE (((Members.MembershipType)="Intermediate"))
ORDER BY Members.MembershipType, Results.TotalPoints
UNION
SELECT TOP 3 Members.ChildsName, Members.MembershipType, Results.TotalPoints
FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
WHERE (((Members.MembershipType)="Junior"))
ORDER BY Members.MembershipType, Results.TotalPoints;

它会显示:

ChildsName | MembershipType | TotalPoints
=========================================
Jon Snow   | Intermediate   | 48
HODOR      | Intermediate   | 67
Cersei     | Intermediate   | 789
Ned Stark  | Junior         | 5
Daenerys   | Junior         | 16
Bran Stark | Junior         | 456
Arya Stark | Senior         | 15
Rob Stark  | Senior         | 69
Tyrion     | Senior         | 6215

我如何使用上面的代码执行此操作?

3 个答案:

答案 0 :(得分:1)

我已将结果包装在子查询中以便订购。对于membershipType的分组和排序,我为每种会员类型设置了排名。 我希望这会有所帮助:

SELECT * FROM (
    SELECT TOP 3 Members.ChildsName AS ChildsName, Members.MembershipType AS MembershipType,  Results.TotalPoints AS TotalPoints, 1 AS rank
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE (((Members.MembershipType)="Senior"))
    ORDER BY Results.TotalPoints

    UNION ALL

    SELECT TOP 3 Members.ChildsName AS ChildsName, Members.MembershipType AS MembershipType,  Results.TotalPoints AS TotalPoints, 2 AS rank
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE (((Members.MembershipType)="Intermediate"))
    ORDER BY Results.TotalPoints

    UNION ALL

    SELECT TOP 3 Members.ChildsName AS ChildsName, Members.MembershipType AS MembershipType,  Results.TotalPoints AS TotalPoints, 3 AS rank
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE (((Members.MembershipType)="Junior"))
    ORDER BY Results.TotalPoints
) m
ORDER BY m.rank, m.ChildsName, m.TotalPoints;

答案 1 :(得分:1)

试试这个:

SELECT * 
FROM 
(
    SELECT TOP 3 Members.ChildsName, Members.MembershipType,  Results.TotalPoints, 0 AS MembershipTypeOrder
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE Members.MembershipType='Senior'
    ORDER BY Results.TotalPoints
    UNION
    SELECT TOP 3 Members.ChildsName, Members.MembershipType, Results.TotalPoints, 1 AS MembershipTypeOrder
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE Members.MembershipType)='Intermediate'
    ORDER BY Results.TotalPoints
    UNION
    SELECT TOP 3 Members.ChildsName, Members.MembershipType, Results.TotalPoints, 2 AS MembershipTypeOrder
    FROM Members INNER JOIN Results ON Members.[Member ID] = Results.ID
    WHERE Members.MembershipType='Junior'
    ORDER BY Results.TotalPoints
) ResultSet
ORDER BY ResultSet.MembershipTypeOrder, ResultSet.TotalPoints;

或者,您可以将成员资格类型存储在单独的表中。例如:

MembershipTypeID | Name           | Order
=========================================
1                | Intermediate   | 1
2                | Junior         | 2
3                | Senior         | 0

然后,您可以使用INNER JOIN获取成员资格类型名称以及列表顺序。

答案 2 :(得分:0)

Select * From (SELECT TOP 3
Members.ChildsName, Members.MembershipType,
Results.TotalPoints FROM Members INNER JOIN Results ON Members.
[Member ID] = Results.ID 
WHERE (((Members.MembershipType)="Senior")) 
ORDER BY Members.MembershipType,
Results.TotalPoints 
UNION SELECT TOP 3 Members.ChildsName, Members.MembershipType,
Results.TotalPoints FROM Members 
INNER JOIN Results ON Members.
[Member ID] = Results.ID WHERE
(((Members.MembershipType)="Intermediate")) ORDER BY Members.MembershipType,
Results.TotalPoints UNION SELECT TOP 3 Members.ChildsName,
Members.MembershipType, Results.TotalPoints FROM Members INNER JOIN Results ON Members.
[Member ID] = Results.ID WHERE (((Members.MembershipType)="Junior")) ORDER BY
Members.MembershipType, Results.TotalPoints) 
as myQuery order by 2 desc;