I have a table where i need to compare 2 column. See below example: I need query where i should group Column 1 & 2 and tell ABC = XYZ = 123, group by date and get sum of qty.
I tried query where i used comparing (column1 = column2 or column2 = column1) & used group by date. BUt i am not getting expected result.
Any help would be appreciated.
Edit: Added Sample data & expected result. I am expecting answer where row 1 & row 2 are same since data from column 2 match is found in column 1.
I try create select for your expected result, but there will be set some statements for ignore row which was changed:
SELECT col1, col2, done_at, SUM(qty) FROM (
SELECT col1, col2, done_at, qty, 0 AS semiline
FROM test WHERE 1
UNION ALL
SELECT test1.col2 AS col1, test1.col1 AS col2, test1.done_at, test1.qty, 1 AS semiline
FROM test AS test1
JOIN test AS test2 ON test1.col2=test2.col1
WHERE 1 GROUP BY test1.col2, test1.col1, test1.done_at
) AS result WHERE 1
GROUP BY col1, done_at;
+------+------+------------+----------+
| col1 | col2 | done_at | SUM(qty) |
+------+------+------------+----------+
| ABC | XYZ | 2017-12-29 | 2 | <-- this row is bad, i think...
| XYZ | 123 | 2017-12-29 | 3 |
| XYZ | 123 | 2018-10-04 | 7 |
+------+------+------------+----------+
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.