I have this data:
Table A
pk name
1 Peter
2 Jon
Table B
pk trait fk_A
1 funny 1
2 generous 1
3 rude 2
I want to get this result set:
pk name traits
1 Peter funny,generous
2 Jon rude
How can I aggregate the trait column of table B and attach it as a CSV field 'traits' to table A?
Related
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
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 A that contains the following columns
ID(PK) id_1
1 4
2 10
3 15
4 4
Now Im trying to create a table B with columns such that
ID(PK) Description id_1_a_id (composite foreign key(ID,id_1))
1 Apple (1,4)
2 Orange (2,10)
3 Banana (3,15)
4 dog (4,4)
5
Does this design make sense? Or is there a better way to do this? (SQL rookie)
Composite foreign keys are common and useful, but you don't have one. If you did, it would look like this.
ID(PK) Description A_id id_1
--
1 Apple 1 4
2 Orange 2 10
3 Banana 3 15
4 dog 4 4
But you wouldn't ordinarily do that, either. Ordinarily, you'd reference a unique set of columns in table A. The unique set of columns is just the single column A.ID. So your table would usually look like this.
ID(PK) Description A_id
--
1 Apple 1
2 Orange 2
3 Banana 3
4 dog 4
You wouldn't usually duplicate the values of A.id_1 in table B. If you need the values from A.id_1, write a query with a JOIN.
select B.ID, B.Description, A.ID, A.id_1
from B
inner join A on A.ID = B.A_id;
If the only unique constraint you have in your tables is on the ID numbers, you're doing it wrong. But that's a different question.
I have to get “suggested” groupings of records based off of existing data.
Table A has a composite key of Akey+Bkey. Table B has a primary key of Bkey.
Akey is generated from a SQL 2012 Sequence object and there is a one to many relationship between table A and table and Table B on Bkey.
The structure and sample data are listed below.
Table A
Akey Bkey ItemSequence
---- ---- ------------
1 1 1
1 5 2
1 7 3
2 7 1
3 2 1
3 3 2
Table B
Bkey GroupValue Data HashString
---- --------------- -------------- ----------
1 Ford Festiva AIR BAG HASH1
2 Ford Festiva RADIATOR CAP HASH2
3 Ford Festiva FUEL PUMP HASH3
4 Ford Mustang AIR FILTER HASH4
5 Ford Explorer AIR FILTER HASH5
6 Ford Edge RADIATOR CAP HASH2
7 Ford Edge FUEL PUMP HASH3
The query has to insert new groups into Table A that match existing sets of Bkeys where the HASH value matches in Table B. To depict the point I use the example of a car model. The hash values of the items within the model of the car could match exactly ( but they might not ). So if all items exist within of OVER the GroupValue then I want to insert a new set of records into Table A with the values where the exact matches were found within that group.
In the sample data you can see that AKey 3 has BKeys 2 and 3 and Bkeys 6 and 7 are a match so these 2 records would be inserted into Table A and a new sequence # generated for the AKey.
I am not sure I understand the part about the groups and GroupValue field.
According to what I understand, you want to insert into Table A any rows from Table B that match the HashString for the Bkey of each AKey and don't already exist in Table A?
If I understand correctly: for Akey1, row TableB.Bkey 7 should be added to Table A. Is that right?
If the logic is good, the following query should select those rows.
SELECT
a.Akey, a.Bkey, b3.*
FROM b
INNER JOIN a ON a.Bkey=b.Bkey
INNER JOIN b b2 ON b2.HashString=b.HashString and b2.Bkey=a.Bkey
INNER JOIN b b3 ON b3.HashString=b2.HashString AND b3.Bkey <> b.Bkey
WHERE a.Akey=3 --Comment this line for the full matches
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.