我有一个联合查询,它从两个不同的总体中获取计数。如何强制结果按查询中写入的顺序而不是升序/降序返回?
select count(datediff(yyyy,dob,admitdate) as counts from myTable where condition union select count(datediff(yyyy,dob,admitdate) as counts from myTable where condition
我希望第一个结果始终作为第一行返回。这可能吗?
您必须使用ORDER BY子句指定订单。在您的示例中,您可以执行以下操作:
SELECT 1 AS seq, COUNT(datediff(yyyy,dob,admitdate) as counts FROM myTable WHERE ...condition-1... UNION SELECT 2 AS seq, COUNT(datediff(yyyy,dob,admitdate) as counts FROM myTable WHERE ...condition-2... ORDER BY seq