Skip to main content

Posts

Showing posts with the label Group by

Why not equal displaying actual result in select queries by joining two select tables

I am using mysql. I am trying to join two queries by != condition. For this example it should return empty result set. But it seems the condition is not applied. I am not understanding why. Can anybody please help? My attempts are below: SELECT today_student.* FROM ( SELECT scd.student_id, sc.transaction_date FROM student_collection_details scd INNER JOIN student_collection sc ON (scd.student_collection_id = sc.id) WHERE 1=1 AND sc.transaction_date BETWEEN DATE('2022-06-01 00:00:00') AND DATE('2022-06-27 00:00:00') AND scd.admission_year_id = 2 AND scd.month_id = 21 AND scd.collection_head_id = 9 GROUP BY scd.student_id ) prev_student, ( SELECT scd.student_id, sc.transaction_date FROM student_collection_details scd INNER JOIN student_collection sc ON (scd.student_collection_id = sc.id) WHERE 1=1 AND sc.transaction_date = DATE('2022-06-28 00:00:00') AND scd.admission_year_id = 2 ...

group_concat by combining column values of two table

I am trying to combine columns between two tables and concat them. While I am able to use group_concat on the columns which are specific to just one table but there is also a column for which I need to combine from table1.role_id and table2.role_id and join the result with specific delimiter. I am using sqlite db. SQL Fiddle: link Table1 name role_id dep_id a 1 d1 a 1 d2 b 2 d3 b 3 d5 c 3 d4 Table2 name role_id ipv6 ipv4 a 1 ipv6_1 ipv4_1 a 2 ipv6_2 ipv4_1 b 7 ipv6_3 ipv4_2 b 7 ipv6_7 ipv4_8 e 2 ipv6_4 ipv4_3 The query I have come up with select t1.name, -- combine role_id column from both the tables and do group_concat on it?? group_concat(DISTINCT t1.dep_id), group_concat(DISTINCT t2.ipv6), group_concat(DISTINCT t2.ipv4) from table1 t1 inner join table2 t2 on t1.name = t2.name group by t1.name Current query output name group_conca...