简体   繁体   中英

Access/SQL: Multiple conditions on same column

I'm stuck with the following problem:

I have a table called "Pickauf_0110" with ~80 columns and ~60000 records.

The two columns I'm interested in are: "nr_pickl" and "platz_von".

The entries in the column "nr_pickl" look like this:

123456
123456
123457

The entries in the column "platz_von" look like this:

03-01-00-00 
03-02-00-00
04-01-00-00
05-06-03-02

None of these columns are keys, so the records are not unique.

Here's what I want to find out:

How many entries in "platz_von" start with "03", how many with "04" and how many with "05" for every unique entry in "nr_pickl".

My idea was to do a triple selfjoin, with three different conditions (platz_von like '03*'...), sum these and finally group them by nr_pickl.

Here's my sql query that fails with an error:

    SELECT 
         p1.nr_pickl,
         sum(p1.platz_von) as Gang_03,
         sum(p2.platz_von) as Gang_04,
         sum(p3.platz_von) as Gang_05
    FROM
        ((Pickauf_0110 as p1
    INNER JOIN
        Pickauf_0110 as p2 ON p1.nr_pickl = p2.nr_pickl)
    INNER JOIN
        Pickauf_0110 as p3 ON p1.nr_pickl = p3.nr_pickl)
    WHERE
        p1.platz_von like '03*' AND
        p2.platz_von like '04*' AND
        p3.platz_von like '05*'
    GROUP BY
       p1.nr_pickl

I know that my query can not work, but I have no idea where my mistake is and if my approach is correct. Any help is appreciated!

SELECT 
   p1.nr_pickl,
   sum(IIF(LEFT(p1.platz_von, 2) = "03", 1, 0) as Gang_03,
   sum(IIF(LEFT(p1.platz_von, 2) = "04", 1, 0) as Gang_04,
   sum(IIF(LEFT(p1.platz_von, 2) = "05", 1, 0) as Gang_05
FROM
   Pickauf_0110 as p1
GROUP BY p1.nr_pickl

EDIT: I haven't tried this for syntax. But this should work in MS-Access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM