2013-04-09 18 views
1

這是示例表:在Access中,如何顯示每行中值最高的列的標識和列名稱?

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-訪問。請儘可能描述你的答案。謝謝。

回答

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 
+0

非常感謝你。這個解決方案爲我工作。我唯一需要添加的是: iif(五>六,'五','六')))))** AS ** col – Kiro 2013-04-10 00:11:02

+0

@Kiro - np,很高興爲您效勞!有一段時間沒有使用Access,並且稍微有些興奮:) – sgeddes 2013-04-10 00:23:57

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) 
; 
0

只是爲了simpify事情有點動這個到它自己的查詢(調用它,你喜歡什麼,但我會叫它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

相關問題