Select specific columns from subquery in Nhibernate Criteria - sql

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!

Related

Postgres query to combine multiple rows with same value of a column into a single row

I have this table:
and would like to convert it to the following:
Please help me, been stuck on it for way too long. Doesn't working for me using group by
WITH A as (SELECT id, a FROM XXX WHERE a is not null),
B as (SELECT id, b FROM XXX WHERE b is not null)
SELECT A.a, B.b, A.id FROM A
INNER JOIN B on A.id = B.id;
For this dataset, simple aggregation would do what you want:
select min(a) a, min(b) b, id
from mytable
group by id
This takes advantage of the fact that aggregate functions ignore null values; we could get the very same result with max() as we did with min().

How to sum the multiplied columns from subquery in linq

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.

Sql - Simple query with count

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.

sql - select multiple values from subquery

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;

oracle intersect doesn't work

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.