开发者

add columns to a query in access 2003 based on unknown rows

I have this query in Access 2003:

col1 col2 col3 col4 
111  123  001  12.5 
111  123  002  12.5 
111  123  003  12.5
111  123  004  12.5
---------------------
112  211  001  5.2
112  211  002  5.2
11开发者_如何学编程2  211  003  5.2

What I'm trying to do is to move those rows starting with col3 in columns and suprime the duplicates, I mean I want to have in the end like this:

col1 col2 new_col3 new_col4 new_col5......(unknown # of columns)
111  123  001      002      003       004   005.................. 12.5 
 (here I don't want a sum like in the crosstab query).

I tried in so many ways, but I didn't get anything. Please, I really need your help. Thank you in advance.


It isn't clear what you want at the intersection of the new columns and the rows. Using what little sample output you gave us, let's assume you want the col3/col4 values themselves. To crosstab multiple columns, you need to use multiple queries.

Query1:
TRANSFORM Min(T1.col3) AS MinOfcol3
SELECT T1.col1, T1.col2
FROM Table1 AS T1
GROUP BY T1.col1, T1.col2
PIVOT T1.col3;

Query2:
TRANSFORM Min(Table1.col4) AS MinOfcol4
SELECT Table1.col1, Table1.col2
FROM Table1
GROUP BY Table1.col1, Table1.col2
PIVOT Table1.col4;

Query3:
SELECT Query1.*, Query2.*
FROM Query1 
    INNER JOIN Query2 
        ON (Query1.col2 = Query2.col2) 
            AND (Query1.col1 = Query2.col1);

If the values in col3 and col4 are known, then another way to do it would be:

Select col1, col2
    , Min( IIF( col3 = '001', col3, NULL ) ) As col3_001
    , Min( IIF( col3 = '002', col3, NULL ) ) As col3_002
    , Min( IIF( col3 = '003', col3, NULL ) ) As col3_003
    , Min( IIF( col3 = '004', col3, NULL ) ) As col3_004
    , Min( IIF( col4 = 12.5, col4, NULL ) ) As col4_12_5
    , Min( IIF( col4 = 5.2, col4, NULL ) ) As col4_5_2
From Table1
Group By col1, col2

Now with all that, we really should ask why on Earth you want to do this in the first place. Why not return multiple rows and process the information that way?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜