I have two tables like this:
ID Name Value
----------------------
1 Book1 A
2 Book2 B
3 Book3 C
Name
----------------------
Book4
Book5
I'm trying to write query to insert rows in the first table so it becomes:
ID Name Value
----------------------
1 Book1 A
2 Book2 B
3 Book3 C
1 Book4 NULL
2 Book4 NULL
3 Book4 NULL
1 Book5 NULL
2 Book5 NULL
3 Book5 NULL
I can't get it to work with this query:
SELECT a.ID,
b.Name,
a.Value
FROM table1 a
LEFT JOIN (SELECT Name FROM table2) b ON a.Name = b.Name
I also tried CROSS JOIN but still couldn't quite get it. Any help will be appreciated.
For this case I will name TableA your first table and TableB the one with only names. But next time try to provide some meaningfull names.
INSERT INTO TableA
SELECT A.ID, B.Name, Null
FROM
(
SELECT DISTINCT ID
FROM TableA
) A, TableB B
Related
First of all, I know that this is a case of a bad design, but I am just wondering how can I get single sql result from the jointure with the desired infos.
So it's a simple exemple, 2 tables connected with ids:
TableA TableB
id code id tableA_id (FK) start end
---- ------- ---- ---------------- ------- ------
1 codeA 1 2 NY null
2 codeB 2 2 null LA
So line 2 in TableA has 2 corresponding lines in TableB.
Desired result:
tableA_code tableB_start tableB_end
------------- -------------- ------------
codeB NY LA
So in the result in need to group the 2 lines from the table B into one line and eliminate the null values.
The query :
select A.code, B1.start, B2.end
from TableB B1, TableB B2, TableA A
where B1.tableA_id = B2.tableA_id
and B1.tableA_id = A.id
and B1.start is not null and B2.end is not null
Let me know if this works for you
SELECT A.code,
MAX(B.STRT) STRT,
MAX(B.END)
END
FROM
(SELECT 1 AS A_ID,'CODE A' AS CODE FROM DUAL
UNION ALL
SELECT 2 AS A_ID,'CODE B' AS CODE FROM DUAL
)A,
(SELECT 1 AS B_ID,2 AS A_ID,'NY' STRT, NULL AS END FROM DUAL
UNION ALL
SELECT 2 AS B_ID,2 AS A_ID, NULL STRT, 'LA' AS END FROM DUAL
)B
WHERE A.A_ID = B.A_ID
GROUP BY A.code;
How can I merge data into one column for different account numbers. Currently, it looks like this.
TableA.Order TableA.Question TableB.Response
1 a Null
1 b James
1 c Null
2 d Zebra
2 T Null
However, I want it to merge like below:
TableA.Order NewColumn
1 a
1 b
1 c
1 James
2 d
2 T
2 Zebra
Base from my understanding of your question. I devised a solution which answers to it. See my query below:
SELECT * FROM TableA
UNION ALL
SELECT B.Order1,A.Response From
(
(SELECT ROW_NUMBER()OVER(ORDER BY Response)PK,* FROM TableB) A Left Join
(SELECT ROW_NUMBER()OVER(ORDER BY Order1)PK,* FROM TableA) B On A.PK=B.PK
)
Where Response IS NOT NULL
Say I have two tables as below where Table A has columns name and type where each name may appear many times and with different type and Table B has unique code, name and sum.
Table A
John Type1
Mark Type2
John Type1
Mark Type3
John Type4
Paul Type5
Table B
1 John 20
2 Mark 33
3 Paul 22
4 Mark 55
5 John 46
Now what I want is something like this:
Table C
1 John 20 Type1
2 Mark 33 Type2
3 Paul 22 Type5
4 Mark 55 Type2
5 John 46 Type1
Normally Table A should contain unique entries with one type for each name and I could do a right join Table B on name to get what I want. But now if I do right join I get duplicate entries on Table C because name has duplicates types in Table A. How do I solve this?
Try this
WITH TableAA
AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY NAME ORDER BY NAME,TYPE) RN
,name
,type
FROM TableA
)
SELECT
B.*
,(
SELECT type from TableAA A WHERE A.name= B.name AND A.RN=
ISNULL(NULLIF((SELECT COUNT(1) FROM TableB C WHERE C.NAME=B.name
and C.no < B.no),0),1)
) AS Type
FROM
TableB B
SQLFiddle Demo
You can try this :
SELECT A.*,B.TYPE
FROM dbo.TABLE_2 A RIGHT JOIN
(
SELECT DISTINCT(NAME),MIN(TYPE)TYPE
FROM TABLE_1
GROUP BY NAME
) B ON A.NAME=B.NAME
ORDER BY CODE ASC
TABLE_1= TABLE A
TABLE_2= TABLE B
I could really use some help with the following SQL Select statement scenario:
I need to select all rows from a table conditionally depending on whether a userID has already entered data into a second table with the same ID.
Example:
Select all rows from TABLE A for idNumber where idNumber not in
TABLE B
but for each idNumber that IS in TABLE B, still return row unless a
specific userID is in that row in TABLE B.
TABLE A
========
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
4 B 01/01/01
5 B 01/01/01
TABLE B
========
idNumber|type|userID
1 A 0000
3 B 0000
4 B 1111
userID to exclude records for = 1111
SQL Query should return:
idNumber|type|Date
1 A 01/01/01
2 A 01/01/01
3 B 01/01/01
5 B 01/01/01
Apologies for the long winded post but i hope it makes sense.
Many thanks in advance,
ukjezza.!!
Select idNumber, type, Date
From TableA
Where Not Exists (
Select 1
From TableB
Where TableB.idNumber = TableA.idNumber
And TableB.userID = 1111
)
Another choice:
Select TableA.idNumber, TableA.type, TableA.Date
From TableA
Left Join TableB
On TableB.idNumber = TableA.idNumber
And TableB.userId = 1111
Where TableB.idNumber Is Null
Looks like a LEFT JOIN and COALESCE could take care of it:
SELECT a.*
FROM TableA as a
LEFT JOIN TableB as b
ON a.idNumber = b.idNumber
WHERE COALESCE(b.userID, -1) != 1111
select A.*
from TableA as A
left outer join TableB as B
on A.idNumber = B.idNumber
where B.idNumber is null or
B.userID <> '1111'
i have 3 tables (A,B,C)
Table A -
ID Name
1 Sam
2 Manuel
3 Jane
Table B
ID Tab_A_ID Name
1 1 Meer
2 1 Kutti
3 2 Mikaro
Table C
ID Tab_B_ID Price
1 1 255.11
2 1 30.52
3 3 125.22
I need a query that shall pick up the top price for TableA-Name from TableC. So only 1 top price for 1 nae record.
e.g.-
Sam - 255.11
Manuel - 125.22
How can i get this?
To get the max price per entry in A:
SELECT a.Name,
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, a.name
To get the max price per entry A per entry B:
SELECT a.Name,
b.Name
MAX(c.price)
FROM a
INNER JOIN b
ON a.id = b.tab_a_id
INNER JOIN c
ON b.id = c.tab_b_id
GROUP BY a.id, b.id, a.name, b.name
Note that entries in A without corresponding entires in B or entries in B without corresponding entries in C will not appear in the result. Use LEFT JOIN if you want to include these in the result.