How to add a column to table results, with the number of repeats of a certain value on a row? - mysql

For example, we have a table:
| id | field | value |
|----|-------|-------|
| 1 | X | Y |
| 1 | V | Y |
| 1 | Z | W |
| 1 | Z | T |
and I want to have a following output:
| id | field | value | field_occurencies |
|----|-------|-------|-------------------|
| 1 | X | Y | 1 |
| 1 | V | Y | 1 |
| 1 | Z | W | 2 |
| 1 | Z | T | 2 |
Is there any way to do this?

Windowing functions are for that very purpose:
COUNT(*) over (partition by field) as field_occurence
We can't tell if your particular database product supports window functions (you didn't tell us)

Related

Oracle 12 - How to identify duplicates with increment?

I want to differentiate duplicate values in my query by adding an increment or a count of some sort.
The idea is to concatenate my two columns to create a new unique reference.
I tried this :
SELECT
value,
COUNT(*) OVER (PARTITION BY value) value_incr
FROM table
and got the following result :
| value | value_incr |
| --- | --- |
| a | 1 |
| b | 3 |
| b | 3 |
| b | 3 |
| c | 2 |
| c | 2 |
| d | 1 |
But what I would like to get is :
| value | value_incr |
| --- | --- |
| a | 1 |
| b | 1 |
| b | 2 |
| b | 3 |
| c | 1 |
| c | 2 |
| d | 1 |
Is there a way to differentiate my duplicates in Oracle 12 ?
My best solution for now is to add a ROWNUM column, but it's not really satisfying.
Thank you

Find all rows with value x if there is a pattern of occurence using MySQL

I have two tables
Table A
--id--something--
| 1 | x |
| 2 | y |
| 3 | x |
| 4 | z |
| 5 | x |
| 6 | z |
Table B
--id-----A.id-----Value--
| 1 | 1 | 0 |
| 2 | 5 | 1 |
| 3 | 10 | 1 |
| 4 | 17 | 1 |
| 5 | 19 | 0 |
| 6 | 34 | 1 |
I want to find all "somethings" from Table A, that have at least one pattern: there is a row that have a relation to Table B with value = 0 and next row with the same value in column "something" doesn't have relation to Table B at all.
In this case it would be:
x
because:
Joined Table:
--id--something-- --id-----A.id-----Value--
| 1 | x | | 1 | 1 | 0 |
| 3 | x | |NULL| NULL | NULL |
As mentioned, while certainly do-able, this is quite a complicated request. Anyway, here's something to get you started...
SELECT x.*
, MIN(y.id) next_id
FROM a x
JOIN a y
ON y.id > x.id
AND y.something = x.something
GROUP
BY x.id;
+----+-----------+---------+
| id | something | next_id |
+----+-----------+---------+
| 1 | x | 3 |
| 3 | x | 5 |
| 4 | z | 6 |
+----+-----------+---------+

MYSQL view containing a row for each element in string

I don't know if it's possible but I try to ask you.
I've a user table
|ID|production_lines|activity|
|1 | A|B|C | X|Y|Z |
|2 | B|C|D | X|Y|Z |
I need a view displaying this
|production_line|activity|count|
| A | X | 1 |
| B | X | 2 |
| C | X | 2 |
| D | X | 1 |
| A | Y | 1 |
| B | Y | 2 |
| C | Y | 2 |
| D | Y | 1 |
and so on...
The "count" columns is the count of how many time i've a combination of this particular production_line and activity.
Thank you very much

MySQL - Get all rows from left table per group on right table

I need to show all records from table A per user_id in table B, even if not matched. I have tried with LEFT JOIN and GROUP-ing but did not achieved my expected result. Also my skill on SQL is not good, so need help.
Here is my table data:
Table A : gateways
=========================
| ID | Gateway |
=========================
| 1 | Paypal |
| 2 | Webpay |
| 3 | Stripe |
=========================
Table B : gateway_user
==================================
| GatewayID | UserID | Active |
==================================
| 1 | 1 | Y |
| 1 | 2 | Y |
| 1 | 3 | N |
| 2 | 1 | Y |
| 2 | 2 | N |
| 3 | 1 | Y |
==================================
The result I expect is to see all results from left table per user_id on right, even if it doesn't exist in Right Table.
==================================
| GatewayID | UserID | Active |
==================================
| 1 | 1 | Y |
| 1 | 2 | Y |
| 1 | 3 | N |
| 2 | 1 | Y |
| 2 | 2 | N |
| 2 | 3 | null |
| 3 | 1 | Y |
| 3 | 2 | null |
| 3 | 3 | null |
==================================
Thank you.
You have to create artificial table containing list of all userId:
SELECT gw.id, ua.userId, gu.active
FROM gateways gw
JOIN (SELECT DISTINCT userId
FROM gateway_user) ua
LEFT JOIN gateway_user gu
ON gu.userId = ua.userId AND gu.gatewayId = gw.gatewayId

Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field?

Is there a way in SQL (MySQL) to do a "round robin" ORDER BY on a particular field?
As an example, I would like to take a table such as this one:
+-------+------+
| group | name |
+-------+------+
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | D |
| 2 | E |
| 2 | F |
| 3 | G |
| 3 | H |
| 3 | I |
+-------+------+
And run a query that produces results in this order:
+-------+------+
| group | name |
+-------+------+
| 1 | A |
| 2 | D |
| 3 | G |
| 1 | B |
| 2 | E |
| 3 | H |
| 1 | C |
| 2 | F |
| 3 | I |
+-------+------+
Note that the table may have many rows, so I can't do the ordering in the application. (I'd obviously have a LIMIT clause as well in the query).
I'd try something like:
SET #counter = 0;
SELECT (#counter:=#counter+1)%3 as rr, grp, name FROM table ORDER by rr, grp
What you can do is create a temporary column in which you create sets to give you something like this:
+-------+------+-----+
| group | name | tmp |
+-------+------+-----+
| 1 | A | 1 |
| 1 | B | 2 |
| 1 | C | 3 |
| 2 | D | 1 |
| 2 | E | 2 |
| 2 | F | 3 |
| 3 | G | 1 |
| 3 | H | 2 |
| 3 | I | 3 |
+-------+------+-----+
To learn how to create the sets, have a look at this question/answer.
Then its a simple
ORDER BY tmp, group, name
You can use MySQL variables to do this.
SELECT grp, name, #row:=#row+1 from table, (SELECT #row:=0) r ORDER BY (#row % 3);
+------+------+--------------+
| grp | name | #row:=#row+1 |
+------+------+--------------+
| 1 | A | 1 |
| 2 | D | 4 |
| 3 | G | 7 |
| 1 | B | 2 |
| 2 | E | 5 |
| 3 | H | 8 |
| 1 | C | 3 |
| 2 | F | 6 |
| 3 | I | 9 |
+------+------+--------------+