I have two tables from two databases:
abc.table1
xyz.table2
How can I check which columns found in table 1 are not in table 2?
TABLE 1
id
name
phone_number
1
John
111111111111
2
Jane
222222222222
TABLE 2
id
date
phone_number
1
0945
111111111111
2
0950
222222222222
3
1045
333333333333
RESULT:
NAME
(Since the column name is in Table 1 but not found in Table 2)
Please mentin database name. If you are using sql server then can use below query to get what you are looking for:
select COLUMN_NAME from abc.INFORMATION_SCHEMA.columns
where TABLE_NAME='TABLE1' and COLUMN_NAME not in (select COLUMN_NAME from
xyz.INFORMATION_SCHEMA.columns where table_name='TABLE2')
Related
I have a table like this:
email
id
albert
1
jped
2
rufus
3
rufuscomp
3
cousruf
3
peter
4
peter2
4
clarisse
5
johan
6
john
7
And I would like to obtain a table like this:
id
email_1
email_ 2
email_3
1
albert
NULL
NULL
2
jped
NULL
NULL
3
rufus
rufuscomp
cousruf
4
peter
peter2
NULL
5
clarisse
NULL
NULL
6
johan
NULL
NULL
7
john
NULL
NULL
I would like to do this in SQL language. So the algorythm should identify the maximum numbers of repetitions in company_id to prepare the number of columns it will have the new table, and then rearrange all the values.
I have found this SQL - Grouping creating new columns but it's not working in SQL Server.
I wanna share the solution I have found. First of all I had to use the ROW_NUMBER() window function, then I followed exactly this: https://www.sqlshack.com/dynamic-pivot-tables-in-sql-server/
So the final code is this one:
SELECT
*
FROM
(
SELECT
[id],
[number_of_row],
[email]
FROM
(
SELECT
id,
CONCAT(
'Email_',
ROW_NUMBER() OVER (
PARTITION BY id
ORDER BY
email
)
) AS number_of_row,
email
FROM
original_table
) AS table_2
) AS table_3 PIVOT (
MAX([email]) FOR [number_of_row] IN (
[Email_1],
[Email_2],
[Email_3]
)
) AS pivot_table
I have 2 tables
First table
Id Type Value
1 2 1,2,3,5
2 1 1,3,6
3 1 2,3,1,6
Second table
Id Name
1 Leon
2 Anna
3 Biorn
4 Alex
5 Peter
6 Luis
Values in First table are Ids in Second table.
I need query that returns all names by type from the first table
For example:
Type = 1
return: Leon,Anna,Biorn,Luis
type = 2
return: Leon,Anna,Biorn,Peter
I'm trying to create a View that will look like this:
Type Name
1 Leon
1 Anna
1 Biorn
1 Luis
2 Leon
2 Anna
2 Biorn
2 Peter
So I can easily select all the names by type, but I can't figure out how to do it. Please help!
You seem to recognize that this is a poor data structure. You should have a junction table -- storing lists of integers as a delimited string is not a SQLish data structure.
Sometimes, we are stuck with other people's bad design decisions. Here is one thing you can do:
select t1.type, t2.name
from table1 t1 join
table2 t2
on ',' + t1.value + ',' like ',%' + cast(t2.id as varchar(255)) + '%,';
I have a table like this
id u_id attr_key attr_value process_id insert_time
------|-------|----------|------------|--------------|--------------
1 1 name john 1 1
2 1 family smith 1 2
3 2 job clerk 2 3
4 1 name sarah 3 4
.............
I have to find two things:
I have to create a view by tablefunc(crosstab) to fetch a group of data for any of u_id ..so it's simple
I have to find (realtime) last value of any key of any u_id (like Hbase database) so I don't have any good solution
this is what i need
id u_id attr_key attr_value
------|-------|----------|------------
4 1 name sarah
2 1 family smith
Any idea or function?
(its possible to add a column in my data model )
The most efficient solution to greatest-n-per-group problems in Postgres is to use distinct on ():
The following will retrieve that for a single u_id
select distinct on (attr_key) id, u_id, attr_key, attr_value
from the_table
where u_id = 1
order by attr_key, insert_time desc;
An index on (u_id, attr_key, insert_time) should help for performance
I have a table "Employee" as shown below
Id Name
1 John
2 Jaffer
3 Syam
4 Aish
5 Gidson
1 Aboo
2 Sindhu
3 Saravanan
I want to get two outputs like
Id
1
2
3
Id
4
5
Which transformation should i use?
Could you Please help on that?
You will have to write two queries.
SELECT Id
FROM Employee
GROUP BY Id
HAVING COUNT(Id)>1
The above query will give you first output
SELECT Id
FROM Employee
GROUP BY Id
HAVING COUNT(Id)=1
This will give you 2nd output.
I have a requirement wherein the table structure I have looks like
ID Owner Id NAME
1 20 Name 1
1 21 Name 2
1 34 Name 3
2 10 Name 4
2 12 Name 5
3 100 Name 6
I need a query that would give me the results as
ID Owner ID Name
1 20 Name 1, Name2, Name 3
2 10 Name4, Name5
3 100 Name 6
Currently we do this on the codebehind, but I would ideally like to do this through SQL and see if that amounts to any performance improvement.
You did not mention your DBMS so I'm assuming PostgreSQL:
SELECT id,
min(owner_id) as lowest_id,
string_agg(name, ', ') as name_list
FROM the_table
GROUP BY id