在Access中,如何显示每行中具有最高值的列的id和列名称?

时间:2013-04-09 23:17:46

标签: sql ms-access max

这是示例表:

Table1
| id | one | two | three | four | five | six |
|--------------------------------------------|
|   1|   11|    7|      4|     9|     4|    1|
|   2|   12|    9|      3|     8|    19|   32|
|   3|   18|    7|      7|     1|    24|    2|
|   4|    9|    1|     15|     6|     6|    4|

目前我在桌面上使用的查询是:

SELECT id, Max(colx) AS colWithMax
FROM (
   SELECT id, one AS Colx From Table1 UNION ALL
   SELECT id, two AS Colx From Table1 UNION ALL
   SELECT id, three AS Colx From Table1 UNION ALL
   SELECT id, four AS Colx From Table1 UNION ALL
   SELECT id, five AS Colx From Table1 UNION ALL
   SELECT id, six AS Colx From Table1
)
group by id;

我得到的输出是:

ID | colWithMax
-- | ---------
 1 |     11
 2 |     32
 3 |     24
 4 |     15

我的问题是如何更改查询以便输出显示列名而不是列中的值?

根据样本表,所需的输出为:

ID | colWithMax
-- | --------- 
 1 |   one
 2 |   six
 3 |   five
 4 |   three

注意:我对sql有基本的了解,之前从未使用过ms-access。请在答案中尽可能具有描述性。谢谢。

3 个答案:

答案 0 :(得分:0)

您可以尝试以下查询 -

select id, colname from (   
SELECT id, one AS Colx, 'one' Colname From Table1 UNION ALL
   SELECT id, two AS Colx, 'Two' Colname From Table1 UNION ALL
   SELECT id, three AS Colx, 'Three' colname From Table1 UNION ALL
   SELECT id, four AS Colx, 'four' colname From Table1 UNION ALL
   SELECT id, five AS Colx, 'Five' colname From Table1 UNION ALL
   SELECT id, six AS Colx, 'Six' colname From Table1
) as t1
where exist ( select 1 from (
SELECT id, Max(colx) AS colWithMax
FROM (
   SELECT id, one AS Colx, 'one' Colname From Table1 UNION ALL
   SELECT id, two AS Colx, 'Two' Colname From Table1 UNION ALL
   SELECT id, three AS Colx, 'Three' colname From Table1 UNION ALL
   SELECT id, four AS Colx, 'four' colname From Table1 UNION ALL
   SELECT id, five AS Colx, 'Five' colname From Table1 UNION ALL
   SELECT id, six AS Colx, 'Six' colname From Table1
)
group by id) as t2
where t1.id = t2.id
and  t1.colx = t2.colx)
;

答案 1 :(得分:0)

只是为了简化一些事情,将其移动到它自己的查询中(称之为你喜欢的但我会称之为Qry以达到这个答案的目的。正如你所看到的,我已经添加了一个列值来自的列的名称。

SELECT id, val, col
FROM (
    SELECT id, one AS val, 'one' AS col FROM Table1 UNION ALL
    SELECT id, two AS val, 'two' AS col FROM Table1 UNION ALL
    SELECT id, three AS val, 'three' AS col FROM Table1 UNION ALL
    SELECT id, four AS val, 'four' AS col FROM Table1 UNION ALL
    SELECT id, five AS val, 'five' AS col FROM Table1 UNION ALL
    SELECT id, six AS val, 'six' AS col FROM Table1
) AS Qry

在使用上面定义的第二个/新查询中执行类似

的操作
SELECT q1.id, q1.val, q1.col  AS colWithMax
FROM Qry q1
LEFT JOIN Qry q2 ON q2.id = q1.id
                AND q2.val > q1.val
WHERE q2.id IS NULL

这个想法是,只有没有大于它们的数字的值才会有连接,因此最大(或最大数字)应该没有“连接值”,这些值由{{标识1}}

答案 2 :(得分:0)

以下是使用if进行多次查询的另一个旋转:

select  id,
  if (one > two && one > three && one > four && one > five && one > six, 'one',  
  if (two > three && two > four && two > five && two > six, 'two',
  if (three > four && three > five && three > six, 'three',
  if (four > five && four > six, 'four',
  if (five > six, 'five', 'six'))))) col
from Table1

SQL Fiddle Demo

这实际上使用的if访问权使用了iif,这使用&&,我认为访问权限使用AND,但重点是,您不需要使用您的查询中的汇总和group by

以下是我对访问等效项的尝试:

select  id,
  iif (one > two AND one > three AND one > four AND one > five AND one > six, 'one',  
  iif (two > three AND two > four AND two > five AND two > six, 'two',
  iif (three > four AND three > five AND three > six, 'three',
  iif (four > five AND four > six, 'four',
  iif (five > six, 'five', 'six'))))) col
from Table1