In the Oracle SQL, why this code doesn't compile? Oracle doesn't support intersect? intersect only takes one column value?
assume two table have same column types.
Thanks
select B.name, B.id from tmp_B B where B.id in (select distinct id from tmp_A);
intersect
select distinct A.name, A.id from tmp_A A;
error message
Error report:
Unknown Command
There is a syntax error in your statement. You have an extra semicolon after the initial SELECT and before the INTERSECT.
select B.name, B.id from tmp_B B where B.id in (select distinct id from tmp_A)
intersect
select distinct A.name, A.id from tmp_A A
should be a valid SQL statement assuming that ID and NAME have the same data types in both tables.
Related
I'm trying to take the distinct IDs that appear in table a, filter table b for only these distinct IDs from table a, and present the remaining columns from b. I've tried:
SELECT * FROM
(
SELECT DISTINCT
a.ID,
a.test_group,
b.ch_name,
b.donation_amt
FROM table_a a
INNER JOIN table_b b
ON a.ID=b.ID
ORDER by a.ID;
) t
This doesn't seem to work. This query worked:
SELECT DISTINCT a.ID, a.test_group, b.ch_name, b.donation_amt
FROM table_a a
inner join table_b b
on a.ID = b.ID
order by a.ID
But I'm not entirely sure this is the correct way to go about it. Is this second query only going to take unique combinations of a.ID and a.test_group or does it know to only take distinct values of a.ID which is what I want.
Your first and second query are similar.(just that you can not use ; inside your query) Both will produce the same result.
Even your second query which you think is giving you desired output, can not produce the output what you actually want.
Distinct works on the entire column list of the select clause.
In your case, if for the same a.id there is different a.test_group available then it will have multiple records with same a.id and different a.test_group.
I'm trying to multiply columns from a table and using subquery i have tried to sum the multiplied columns
select B.ID , sum(B) from (
select distinct A.Id,
A.Savings * A.Spent as B,
from A group by A.id ) B
group by B.ID
How can i convert this sql query to linq.
Your query is wrong at this point:
A.Savings * A.Spent as B,
from A group by A.id
a coma before the from clause will error the query out. Try the below:
SELECT
B.ID,
SUM(Total) AS TOTAL
FROM
(SELECT
A.Savings * A.Spent as Total
From A
Group by A.ID)
Group by B.ID
Your query is right, you may just need to check in future.
I have a simple question on how to read the number of occurences of some values in a table B that references to a value in a table A. It's all explained better in the following example:
Let's say I have two simple tables, A with an attribute ID and B with an attribute ID that references A.ID; I use this code to find the number of occurences of a A.ID value into B:
SELECT A.ID, COUNT(*)
FROM A JOIN B ON A.ID = B.ID
GROUP BY A.ID
Is it possible to achieve the same result using something like the following code...
SELECT ID, -- SOMETHING --
FROM A
....
... using this subquery?
SELECT COUNT(*)
FROM B WHERE B.ID = A.ID
Thank you very much
I think you might be referring to a correlated subquery:
select a.id, (select count(1) from b where id=a.id) cnt from a;
The a.id term in the subquery binds with table a in the outer query.
Is it possible to select multiple values from a subquery in SELECT block?
Selecting one value works fine like this:
SELECT
a.id,
(SELECT b.id FROM b WHERE b.a_id = a.id) AS b_id
FROM
a
But if i also want to get the b.name and i change the query to this:
SELECT
a.id,
(SELECT b.id, b.name FROM b WHERE b.a_id = a.id)
FROM
a
... it doesn't work anymore. One possibility would be to put the subquery to FROM block and take values from there but in my particular query that doesn't work so i would like to solve in SELECT block. Thank you!
This will help you
SELECT A.ID,
B.ID,
B.NAME
FROM A INNER JOIN B ON B.A_ID=A.ID;
How Can I translate the following SQL query to Criteria query:
Select A.TextProperty, B.TextProperty, Count(C.Id)
From A, B, C
Where A.Id In
(
Select A.Id, C.Id, Count(C.Id)
From A, C
Having Count(C.Id) > 1
)
I know the Where part is incorrect, but I need to find a way to get only the A.Id after the inner/Sub-query select statement is executed.
Is there any way to select only the A.Id property from the sub-query then apply the Where condition on it?!
Please help me figure this out!