How to sum the multiplied columns from subquery in linq - sql

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.

Related

SQL Server query for calculating sum

I am trying to write a SQL query for calculating sum without success.
Let's say that we have:
table A with columns id and type
table B with columns id, a_id (relation to table A) and amount
I succeed to calculate number of records by type like in the following example:
SELECT DISTINCT
type,
COUNT(A.id) OVER (PARTITION BY type) AS numOfRecords
FROM A;
How to calculate sum of amounts also per type (to sum up all amounts from table B for all distinct types in A)?
Your query would normally be written as:
select type, count(*) as num_records
from A
group by type;
Then, you can incorporate b as:
select a.type, count(*) as num_records, sum(b.amount)
from A left join
(select a_id, sum(amount) as amount
from b
group by a_id
) b
on b.a_id = a.id
group by a.type;
You can also join and aggregate without a subquery, but this will throw off the count. To fix that, you can use count(distinct):
select a.type, count(distinct a.id) as num_records, sum(b.amount)
from A left join
from b
on b.a_id = a.id
group by a.type;

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().

Select specific columns from subquery in Nhibernate Criteria

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!

SQL: select multiple columns based on multiple groups of minimum values?

The following query gives me a single row because b.id is pinned. I would like a query which I can give a group of ids and get the minimum valued row for each of them.
The effect I want is as if I wrapped this query in a loop over a collection of ids and executed the query with each id as b.id = value but that will be (tens of?) thousands of queries.
select top 1 a.id, b.id, a.time_point, b.time_point
from orientation_momentum a, orientation_momentum b
where a.id = '00820001001' and b.id = '00825001001'
order by calculatedValue() asc
This is on sql-server but I would prefer a portable solution if it's possible.
SQL Server ranking function should do the trick.
select * from (
select a.id, b.id, a.time_point, b.time_point,
rank() over (partition by a.id, b.id
order by calculatedValue() asc) ranker
from orientation_momentum a, orientation_momentum b
where a.id = '00820001001' and b.id between 10 and 20
) Z where ranker = 1

SQL: How to add a column in the SELECT query result?

Usually we will select the field(s) in the SQL query. e.g.
SELECT A.id FROM Member A
But what if I want to align a column which elements correspond to the other selected field?
For example I want to select the member ID from a member table, and the COUNT that count how many times the member appear in the tuple of other table
So how do I make the COUNT column that align together with the select result?
If I understood you correctly, this is what you want:
SELECT A.id, count(B.MemberID)
FROM Member A
LEFT JOIN TableB B on A.id = B.MemberID
group by A.id
The LEFT JOIN will include records in A that do not have any corresponding records in B. Also, COUNT only counts non-null values, so you need to use it with B.MemberID. This way the count for records in A that do not have any corresponding records in B will be 0, since B.MemberID will be NULL.
I agree with #Adrian's solution, but if there were many columns in the original SELECT list, they all would have to be listed in GROUP BY. I mean something like this:
SELECT
A.id,
A.name,
A.whatever,
...
COUNT(B.member_id)
FROM Member A
LEFT JOIN Member_Something B ON A.id = B.member_id
GROUP BY
A.id,
A.name,
A.whatever,
...
It is not always convenient, especially when the columns are actually expressions. You could take another approach instead:
SELECT
A.id,
A.name,
A.whatever,
...
COALESCE(B.member_count, 0)
FROM Member A
LEFT JOIN (
SELECT member_id, COUNT(*) AS member_count
FROM Member_Something
GROUP BY member_id
) B ON A.id = B.member_id
select member_id, count(*)
from table
group by member_id;