Sql query with selected and splited column value from another table - sql

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)) + '%,';

Related

How can I compare columns from 2 different tables?

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')

How to populate values in a cell from different table, separated by comma [duplicate]

This question already has answers here:
How to concatenate text from multiple rows into a single text string in SQL Server
(47 answers)
Closed 4 years ago.
I have six tables
a. Employee
EmployeeId EmployeeName
123 John
125 Peter
129 Jack
b. EmployeeParameterValue
EmployeeParameterValueId EmployeeId ParameterValueId
1 123 1
2 125 2
3 129 3
c. ParameterValue
ParameterValueId ParameterId Value
1 2 1, 2, 3
2 3 1, 2
3 2 3
d. Parameter
ParameterId Name
2 WorkedStates
3 WorkedType
e. WorkedStates
WorkedStatesId WorkedStatesName
1 CA
2 WA
3 NY
f. WorkedType
WorkedTypeId WorkedTypeName
1 Hourly
2 Salaried
I need to write a report in following format:
EmployeeId EmployeeName Parameter ParameterValue
123 John WorkedStates CA, WA, NY
125 Peter WorkedType Hourly, Salaried
129 Jack WorkedStates NY
I am able to write a query which fetches first 3 columns. But I am not able to write a query which fetches the data in the fourth column.
Please advise.
Clearly the design could be improved.
Now, you could split the delimited string, join to the appropriate table, and then re-aggregate the results, or you can consider a UDF
Example
Select [dbo].[ParameterValue](2,'1, 2, 3')
Returns
CA, WA, NY
The UDF if interested
CREATE FUNCTION [dbo].[ParameterValue] (#P int,#S varchar(max))
Returns varchar(max)
AS
Begin
Select #S = replace(#S,MapFrom,MapTo)
From (Select Top 10000
MapFrom = WorkedStatesId
,MapTo = WorkedStatesName
From WorkedStates
Where #P=2
Order By 1 desc
Union All
Select Top 10000
MapFrom = WorkedTypeId
,MapTo = WorkedTypeName
From WorkedType
Where #P=3
Order By 1 desc
) A
Return #S
End
-- Syntax : Select [dbo].[ParameterValue](2,'1, 2, 3')
I would not suggest this on a large table, but the performance may not be so bad.
Use JSON
save your data with json format.
If you save data with json format you have a object into any col of table that help you better organized your data in tables.
Save two or tree or more table fields in one json and save this json into one col of table.

Join SQL Table where join on column has user submitted entries

I'm trying to join two tables together. 1 table is Users and their responses and the 2nd table is Answer ID that corresponds to different options that the User can select.
In Table 2, there are some user entered entries.
In table 1, any user entered value is Answer ID = 1. Other than the Answer ID =1, the response id and the answer id will match.
How do I join the two tables together?
I joined on the identifier but as there are multiple of each one, it creates duplicates.
Snippet 1:
Select *
from Table1
Join Table2 on Table1.identifier = Table2.identifier
Otherwise, Snippet 2:
select *
from Table1
Join Table2 on (Table1.identifier = Table2.identifier AND
table2.response_id = table1.answer_id)
This fails because Response_id is var and answer id is INT.
When I do an AND condition for the join, it fails because user entries like 91.6 and then the answer id = 1.
With this snippet I get nothing because
Conversion failed when converting the varchar value '91.6' to data type int.
For example, I want the table join to skip the matching when answer id = 1 (because answer_id=1=user entered) and match everything else.
Table 1
Identifier Answer_iD Text
-------------------------------------
1 2 Male
1 3 Female
2 1 User Entered
3 2 Answer1
3 3 Answer2
3 4 Answer3
Table2
User Identifier Response_id
---------------------------
Andy 1 2
Andy 2 91.6
Andy 3 2
I want this output:
User Identifier Response_id Answer_Id Text
--------------------------------------------
Andy 1 2 2 Male
Andy 2 91.6 1 User entered
Andy 3 2 2 Answer1
Right now with my SQL snippet 1 I get
User Identifier Response_id Answer_Id Text
--------------------------------------------
Andy 1 2 2 Male
Andy 1 2 3 Female
I don't have access to edit any of the tables and right I basically look up each identifier and answer_id in Table 1 manually to see what it stands for in the table. There's a 100 of identifiers for each person so it gets pretty tiring quick.
Any workarounds welcome.
The answer to your question is this. Don’t understand what you want, but...
Select user, t2.identifier, t2.response_id, isnull(t1.answerid,t3.answerid)
From table2 t2
Left join table1 t1 on t1.identifier=t2.identifer and t1.answerid=t2.responseid
Left join table1 t3 on t3.identifer= t2.identifier and t3.answerid=1

Write nested SQL query

I have 2 tables
Table1
ID Name
--------------------
1 John Carter
2 Jack Hammer
3 John Adams
4 John Doe
5 Brian Adams
Table2
ID ID_FromTable1
-----------------------------
1 2
2 3
3 1
4 1
5 1
6 2
7 3
8 1
9 1
10 5
11 4
12 5
13 4
ID in both tables is the primary key
ID_FromTable1 is the foreign key pointing to ID of Table1.
Now I do something like this:
SELECT ID
FROM Table1
WHERE Name like '%John%'
This will give me the IDs 1, 3, 4.
Now using these IDs, I want to write a query on Table2 to delete all entries where ID_FromTable1 are 1, 3, 4.
Please help me write one single SQL query to get all the IDs from Table1 where Name is 'John' and then using those IDs to delete entries from Table2.
I hope I have made the question clear. Do let me know if you need any clarification.
You can use IN with subquery:
DELETE FROM Table2
WHERE ID_FromTable1 IN ( SELECT ID
FROM Table1
WHERE Name LIKE '%John%' )
In MySQL you can do it with this join
delete table2
from table2
join table1 on table2.id_fromtable1 = table1.id
WHERE t1.Name like '%John%'

How to get all the records from the Table A and only common records from table B in sql

I have two tables Table A and Table B as follows.
TableA:
Id name
1 abc
2 john
3 jack
4 jill
Table B:
Id city phn
1 london 9876345
5 bangalore 2345678
3 chennai 5637473
I want records which are present in tableA but not in Table B.But the result should be
TableA:
Id name
1 abc
2 john
3 jack
4 jill
i.e even though 1 and 3 ids are present in Table B but they are still in table A.I want those records too.
5 bangalore 2345678
this records is not present in Table A.so i should not take this.
Really -- this simple? Don't think you need any joins then...
SELECT * FROM TableA
Good luck.
You need a left outer join.
Look into it here: http://en.wikipedia.org/wiki/Join_(SQL) and here: http://www.w3schools.com/sql/sql_join_left.asp
EDIT:
Your question makes no sense to be honest. In the heading you mention: "All the values in A and only common values in B" and then, you go on to state in the explanation that you need values from 'A' only and not B.. for that
select * from TableA will do.