Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I need to make a query to another table and add the result to the current one as a new record if there is no record with an id in it, but if the current table has a value with such an id then add the value.
table 1:
id
name
date
1
alex
2022-01-01
2
tom
2022-01-01
table 2:
id
name
pass
1
alex
111
3
mitch
222
expected result:
id
name
date
value
1
alex
2022-01-01
111
2
tom
2022-01-01
3
mitch
222
Its a merge results for both the tables so you query will be
select * from table1 full outer join table2 on table1.(some column name) = table2.(some column name)
here some column name represents you id in table1 and table2 respectively.
so your query will be
select * from table1 full outer join table2 on table1.id = table2.id
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
There is only one table.
Table name Family_tree.
Query all children for Nick. Nick can be also mother!
NAME ID FATHER_ID MOTHER_ID
--------------- ---------- ---------- ----------
Nick 23 25 24
Jane 10 27 26
Perl 15 9 13
Katrin 50 6 12
Sandra 1 3 8
Demi 2 3 8
Deimar 3 7 5
Gandalf 4 6 5
Bill 5 10 23
Kelly 6 22 43
Dolmar 7 11 20
May be like this:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
or mother_id in
(select id from family_tree
where name = 'Nick')
or if you need Nick only as a FATHER, then:
select name, father_id, mother_id from family_tree
where father_id in
(select id from family_tree
where name = 'Nick')
Also, if you need only children , you don't need to use connect by...prior for grandchildren, grandgrandchildren, etc.
select t1.name as child, t2.name as father
from table t1
join table t2
on t1.code = t2.father_id
This will give the following o/p
Child Father
______________________________
Nick fill from your tab
Calvin fill from your tab
Sofia Nick
If you're saying that Code = 3 means that Nick is that person's father:
If you only want to return the name:
SELECT Name FROM table WHERE code = 3
IF you want to return everything:
SELECT * FROM table WHERE code = 3
The '3' might have to be in single-quotes as I typed in this sentence, depending on the data type of the Code field + the particular SQL dialect.
EDIT: not quite sure I understand the ask, but Rajee's answer might correct. You can join a table to itself with aliasing i.e. Family_Tree A, Family_Tree B
Another possibility would be a subquery. Because I'm not sure understand the ask, I don't know if this is what you're looking for but consider it an example of what a subquery would look like:
SELECT * FROM Family_Tree A WHERE FatherID = (SELECT ID From Family_Tree B WHERE Other Conditions = Other Conditions)
Something like that, but same thing could probably be accomplished with my first answer or Rajee's answer with some more clarification.
You can try below code -
SELECT
FROM Family_tree T1
JOIN (SELECT ID, FATHER_ID
FROM Family_tree
UNION ALL
SELECT ID, MOTHER_ID
FROM Family_tree) T2 ON T1.ID = T2.FATHER_ID;
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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Update table based on self join on customer_po = ID
Table XYZ
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 null null
125 10-01-2018 null 123
I want Output record like below in Oracle SQL
ID Invoice Date Delivery_date Customer_po
123 01-01-2018 01-01-2018 null
125 10-01-2018 01-01-2018 123
You need an update statement like this.
UPDATE xyz
SET Delivery_date =
(SELECT MAX (invoice_date)
FROM xyz
WHERE Customer_po = 123);
Note that I have used MAX to avoid the errors due to multiple values for Customer_po = 123
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
how to Increment Data of the Same table with AutoIncrement .i have a table with 4 columns
id type no amount
1 type1 a1 1000
2 type1 a2 2000
3 type2 b1 3000
4 type3 c1 4000
using Loop how can i increment them to hundered rows same data .
id type no amount
1 type1 a1 1000
2 type1 a2 2000
3 type2 b1 3000
4 type3 c1 4000
5 type1 a1 1000
6 type1 a2 2000
7 type2 b1 3000
8 type3 c1 4000
9 type1 a1 1000
10 type1 a2 2000
11 type2 b1 3000
12 type3 c1 4000
Please suggest me the best using loop .How can i move forward on this one
Use a table with sufficient number of rows in a cross join.
This will add the rows you already have in your table 100 times.
insert into YourTable(type, no, amount)
select type, no, amount
from YourTable
cross join (
select top(100) 1
from sys.all_objects
) as T(X)
This is the way of inserting part of your table into same table.
INSERT INTO TABLE1
SELECT TOP 4 type,no,amount
FROM TABLE1
ORDER BY ID
There are two ways in which you can achieve the requirement:
If the insert statements are easy to script out and there are less records:
USE <database_name>
Go
INSERT INTO looprecords([type],[no],[amount]) VALUES('type1','a1',1000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type1','a2',2000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type2','b1',3000);
INSERT INTO looprecords([type],[no],[amount]) VALUES('type3','c1',4000);
Go 5 -- You can control this number on how many times you want to loop.
If the insert statements cannot be scripted easily then:
insert into looprecords([type],[no],[amount])
select [type],[no],[amount] from looprecords;
Every time you run it it inserts records in this sequence 1,2,4,8,16,32........so on.
(For example if there are 16 records existing, in the next run it will give you 32 records)
Put the T-SQL in any valid loop to get the desired results.
I hope it has answered your question.
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.