How to merge the result from the same column - sql

So the data i want to combine column C & D together because they have the same ID as column A
Column A
Column B
Column C
Column D
A
A
B
B
A
A
C
C
And i want to look like this
Column A
Column B
Column C
Column D
A
A
B C
B C

Try to use aggregation query like below (Test on MySQL 8.0):
select Column A, Column B, concat(Column C, Column D) from table group by Column A, Column B;
In MySQL 8.0, this query sql will not work as ERROR 1055 (42000): this is incompatible with sql_mode=only_full_group_by. So set sql_mode=‘’ for current session.

Related

SQL multiple columns WHERE condition

I have table
Column A
Column B
Column C
1
a
d
2
b
e
3
c
f
and very large .csv file (about 1M rows). It contains Columns A, B and C.
Column A
Column B
Column C
4
a
d
5
b
w
6
c
f
I need to extract rows from table where (B = 'a' and C = 'd') or (B = 'b' and C = 'w') or (B = 'c' and C = 'f')
Result will be:
Column A
Column B
Column C
1
a
d
3
c
f
I've tried query like in description, but it's too large for request (1M rows)
You are basically trying to join on the columns (b, c) manually. However, SQL knows how to do a join more efficiently.
Import the CSV file as a temporary table, analyze it, create a multi-column index on its (b, c), then do a join like this:
SELECT R.*
FROM realTable R
JOIN csvTable C
ON R.b = C.b AND R.c = C.c;

How to get more column values along with distinct column in the Oracle database?

How to get more columns along with distinct column values in Oracle ?
select DISTINCT cname
from customer
where code is not null;
I need cname, cvalue, cdate with distinct cname
Your question does not really make sense. DISTINCT does not affect columns, it eliminates duplicate rows. So if you have:
a b c
a b c
d b c
Using DISTINCT gives you:
a b c
d b c
You're going to have to be more specific in the output you're getting and what you want.

Merge statement help in oracle

I am using merge for the 1 st time ... I went through existing questions but couldn't get proper help.
Please help me with the below need,
I have a table "table_a" with 3 columns A, B and C. C is a new column added combination of column A and B are unique, to be specific column B is a list of sub codes taken from table_b and configured against the entity in column A.
I need to update column C with a hard coded value for the existing A and B combinations and if some subcode missing from table_b in table_a I need to insert the rows for the same in table_a.
eg. table_a
A B C
= = =
p x
p y
table_b
M
=
x
y
z
After execution of query
table_a
A B C
= = =
p x 1 -- updated with column C
p y 1 -- updated with column C
p z 1 -- new row inserted for the row in table_b
Kindly let me know if anything is not clear.
MERGE INTO table_A a
USING table_b b
ON(a.b = b.m)
WHEN MATCHED THEN
UPDATE SET a.c = 1
WHEN NOT MATCHED THEN
INSERT (a, b, c)
VALUES ('P', b.m, 1)
Note : The insert has two hardcode values for column a and column c as 'P' and 1 respectively.
If you don't want to hard-code the inserted values you can use an in-line view to generate all the expected combinations and merge against that.
merge into table_a a
using (
select t.a, b.m, 1 as c
from (select distinct a from table_a) t
cross join table_b b
) b
on (a.b = b.m)
when matched then
update set a.c = b.c
when not matched then
insert (a, b, c) values (b.a, b.m, b.c);
The using clause does a cross-join of all (distinct) table_a.a values against all table_b.m values, to give all possible combinations. (Which seems to be what you want; you haven't shown any other link between the two). I've also included the fixed value 1 in that view.
Then the merge either sets c for the rows the matching values that already exit in table_a, or inserts a new row using the values from the in-line view.
SQL Fiddle.
You might be able to get your unique a values from some other look-up table, which would be better than hitting table_a twice, but that depends on your real data model.

Getting Unique result set from two table in SQL Server

I m new to SQL Server, please help me with the solution to following problem.
I have two tables which have the following columns:
Table-A only has one column with data like (A, B, C, D)
Table-B is a temporary table which can contain (B, B, C, C, E, E, F, G)
I need to insert contents of table B into table A.
Conditions are
if table A already contains any alphabet in B then that alphabet should be ignored.
Only a unique alphabet needs to be inserted into table A. i.e. Only one E can be inserted from two set of E and
if F and G also needs to be inserted.
How can I possibly solve this problem?
Thanking you in advance....
Try this....
INSERT INTO Table (Column)
SELECT DISTINCT tt.Column
FROM TempTab tt
WHERE tt.Column NOT IN (SELECT DISTINCT Column FROM Table)
Here is the SQL FIDDLE with this query.
INSERT INTO A(col)
(SELECT distinct col from B
minus
(SELECT distinct col FROM A INTERSECT SELECT distinct col FROM B))

Sql Query To read from 4 tables

i have 3 tables
table A, table B and a relational table C
table A has ta_id as primary key along with other columns
table B has tb_id as primary key along with other columns
table C has ta_id, tb_id as foreign keys along with other columns.
i wanted to find all the rows of table B which have a common ta_id in table C.
my sql query for the that is.
SELECT B.ta_id,
B.type,
B.language,
B.user_id
FROM B
INNER JOIN C
ON B.tb_id=C.tb_id
where C.ta_id = 1
ORDER BY B.user_id
the above query seems to be working..
but now i have another table called table D with D.tb_id as a foreign key (which is primary key in table B ).
each row of table B has 0 or more rows associated in table D or we can say
1 or more rows in table D has exactly one corresponding row in table B.
Now i want to list my each row of table B with all the associated rows of table D.
so it should look like this.
first row of table B
first corresponding row of table D
second corresponding row of table D
...
..
second row of table B
first corresponding row of table D
second corresponding row of table D
...
..
so in a way i am mixing the contents of 2 tables in display
Please tell me how to achieve this using a sql query..?
Waiting for reply..!
Thanks
Big O
Just add another inner join like this:
SELECT B.ta_id, B.type, B.language, B.user_id
FROM B
INNER JOIN C
ON B.tb_id=C.tb_id
INNER Join D
ON B.tb_id=D.tb_id
WHERE C.ta_id = 1
ORDER BY B.user_id
I believe that you can use SQL views easily to query data with lot of tables
You cannot do this in one simpl query, you need a loop. Think about what you are trying to do...
TABLE B ROW 1
TABLE D ROW 1 (Matching Row 1 Table B)
TABLE D ROW 2 (Matching Row 1 Table B)
TABLE D ROW 3 (Matching Row 1 Table B)
TABLE B ROW 2
TABLE D ROW 1 (Matching Row 2 Table B)
TABLE D ROW 2 (Matching Row 2 Table B)
TABLE D ROW 3 (Matching Row 2 Table B)
ETC...
ETC...
The only way you can do this is inside a stored procedure using temp tables and looping.