1

这是示例表:

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。请在您的回答中尽可能描述性。谢谢你。

4

3 回答 3

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)
;
于 2013-04-09T23:40:31.533 回答
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

这个想法是只有没有大于它们的数字的值才会有连接,因此最大(或最大数字)不应该有“连接值”,这些由q2.id IS NULL

于 2013-04-09T23:46:00.160 回答
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 小提琴演示

这实际上使用ifwhere access usesiif和 this uses &&where I think access uses AND,但重点是,您不需要group by在查询中使用聚合和 a 。

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

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
于 2013-04-09T23:54:24.613 回答