samedi 12 décembre 2015

SQL OVER clause alternative

I have a database table containing three columns; id, value1 and value2.

CREATE TABLE data (
    id smallint NOT NULL,
    value1 integer,
    value2 integer
);

There are 10 rows in the table as shown below.

id  v1  v2
1   1   1
2   1   2
3   2   1
4   2   2
5   1   1
6   3   2
7   3   3
8   2   1
9   1   1
10  1   3

I'm looking for a query that returns rows where value1 and value2 are the same as other rows. In the example above the result I'm after is as shown below.

id  v1  v2
1   1   1
3   2   1
5   1   1
8   2   1
9   1   1

I know this can be done using an OVER clause using the following query

SELECT
  id, value1, value2
FROM
  (
  SELECT
    id, value1, value2, count(id) OVER (PARTITION BY value1, value2) AS count
  FROM
    data
  ) AS tmp
WHERE
  count > 1
ORDER BY
  id

I need an alternative way of doing this without using OVER because it's not supported in SQLite. I think it can be done using inner join and group by but haven't managed to figure it out. Can anyone help?

Aucun commentaire:

Enregistrer un commentaire