I am unable to sum of data in cds views. Earlier in Hana calculation view the below query give me result as per my desire
Select t1.ID, t1.Name, t1.qty, t2.amount from T1
Inner join (select ID, Name, Sum (amount) from t2 group by ID, Name) as T2
But sum of amount is not correct in cds.. pls help
You can use an aggregate expression in a SELECT statement. That way you can call an aggregate function from multiple rows on a results set:
#AbapCatalog.sqlViewName: 'SALES_ORDER_VW'
define view sales_order as
select from snwd_so
{ key buyer_guid,
#Semantics.currencyCode
currency_code,
#Semantics.amount.currencyCode: 'currency_code'
sum(gross_amount) as sum_gross_amount }
group by buyer_guid, currency_code
So in your view, you could do something like:
sum(name_of_field) as sum_of_field
Related
I'm stuck with this query. I wonder if somebody can give me some idea how to resolve this.
Here is my table:
I basically want to group by product with the highest value of quality. But at the same time I also need to grab completed column.
select
Product, max(Quality) as Quality
from
[Table]
group by
Product
When I group it, I cannot retrieve completed column.
Any other method to have same result above with completed column? in this case 1, 1 will be displayed.
Thanks in advance
You can take the output of this query and inner join it with the original table...
select t1.*
from table_name t1
inner join
(select product, max(quality) as maxquality
from table_name
group by product) t2 on t1.product = t2.product
and t1.quality = t2.maxquality
In SQL Server, you can only SELECT columns that are part of the GROUP BY clause, or aggregate functions on any of the other columns.
You can either try adding it to the GROUP BY clause:
SELECT Product,
Completed,
max(Quality) AS Quality,
FROM [Table]
GROUP BY Product, Completed
Or use an aggregate function:
SELECT Product,
max(Completed),
max(Quality) AS Quality,
FROM [Table]
GROUP BY Product
For example, i have a table with the data:
Screenshot
This table named "table".
I have the SQL query:
select
kind,
count(kind)
from table
where region = 'eng'
group by kind
And I get the result:
Question: how do I write a query that would return all the values that are in the kind field (or any other field that can be in group by)? Even if this value is 0. For the example above, the desired result is
It is mandatory to use group by in the query.
I use a postgresql 10.
Using a conditional aggregation
select
kind,
count(case region when 'eng' then kind end) cnt
from table
group by kind
select
t1.kind,
coalesce(t2.total, 0) total
from
(
select distinct kind from table
) t1
left join
(
select
kind,
count(kind) total
from table
where region = 'eng'
group by kind
)t2
on t1.kind = t2.kind
db fiddle
I have a CTE that contains lots of joins and condition (mydashboard) and
I am trying to join the CTE to another table and show an additional column with the count of the second table.
What am I doing wrong?
select *, count(t_KPIRespConn.RespID)
from mydashboard
join t_kpirespconn on mydashboard.kpicodeid = t_kpirespconn.kpicodeid
group by mydashboard.KPIcodeID
Column 'mydashboard.code' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Thanks
You will need to group by all non aggregated fields, meaning all fields in the SELECT list that are not used in an aggregate function, in your case, all field besides t_KPIRespConn.RespID.
Solution 1:
select field1, field2, field3,... fieldN, count(t_KPIRespConn.RespID)
from mydashboard
join t_kpirespconn on mydashboard.kpicodeid = t_kpirespconn.kpicodeid
group by mydashboard.KPIcodeID, field1, field2, field3, ...fieldN
Using a window function basically achieves the same thing but it is less verbose. You are not required to GROUP BY as the window function aggregates the values on the partition specified. For COUNT() you can specify OVER(), which means the entire results set in the group.
Solution 2:
select *,
count(*) OVER() //<-- Use this if you want the count of all records
from mydashboard
join t_kpirespconn on mydashboard.kpicodeid = t_kpirespconn.kpicodeid
Use the same window function but narrow the partition to any grouping you would like. In this case, the counts would pertain to all records with matching t_KPIRespConn.RespID values.
Solution 3:
select *, count(*) OVER(PARTITION BY t_KPIRespConn.RespID) //<-- Use this if you want the count of all records with the same t_KPIRespConn.RespID
from mydashboard
join t_kpirespconn on mydashboard.kpicodeid = t_kpirespconn.kpicodeid
select * .. group by mydashboard.KPIcodeID is your true issue. When you run aggregate functions, you must explicitly supply any columns that are non-aggregated in the select list in the group by. This will work
select mydashboard.KPIcodeID, count(t_KPIRespConn.RespID)
from mydashboard
join t_kpirespconn on mydashboard.kpicodeid = t_kpirespconn.kpicodeid
group by mydashboard.KPIcodeID
I have a table like the picture bellow and I need to extract the id,name and date of the max datetime that has been grouped by the name.
I can get the max datetime through this query:
SELECT MAX(Table.Date),Table.name FROM Table GROUP BY Table.name
and the result is:
To extract the information of max datetime that has been grouped by the name, I've had this query:
SELECT t1.name, t1.Date, t1.id
FROM Table t1
WHERE t1.Date in (SELECT MAX(t2.Date)
FROM Table t2
GROUP BY t2.name)
the result is:
but do not need the highlighted data. because between 'D's the 2014-01-12 has the max date.
NOTE: I can solve the problem through JOIN expression.
but because of saving time I need a simple query.
You could use row-number, partition by
Select * from
(Select *,rn=row_number()over(partition by name order by date desc) from table) x
Where x.rn = 1;
I got a SQL statement:
Select
ID, GroupID, Profit
From table
I now want to add a fourth column percentage of group profits.
Therefore the query should sum all the profits for the same group id and then have that number divided by the profit for the unique ID.
Is there a way to do this? The regular sum function does not seem to do the trick.
Thanks
select t1.ID,
t1. GroupID,
(t1.Profit * 1.0) / t2.grp_profit as percentage_profit
from table t1
inner join
(
select GroupID, sum(Profit) as grp_profit
from table
group by GroupID
) t2 on t1.groupid = t2.groupid
One more option with window function
select ID, GroupID, Profit * 1. / SUM(profit) OVER(PARTITION BY GroupID)
from t1
An alternative solution using scalar sub-queries is as follows:
select t1.ID, t1.GroupID, (select sum(t2.Profit) * 1.0 / t1.Profit
from table t2
where t2.GroupID = t1.GroupID) as percentage_profit
from table t1;
To provide an alternate answer, albeit less efficient, is to use a scalar subquery.
SELECT ID, GroupId, Profit, (Profit/(SELECT sum(Profit)
FROM my_table
WHERE GroupId= mt.GroupId))*100 as pct
FROM my_table as mt
From the way it reads I'm not sure if you want "percentage of group profits" or you or want group_profit / individual profit
That's the way this sounds "Therefore the query should sum all the profits for the same group id and then have that number divided by the profit for the unique ID"
Either way just switch the divisor for what you want!
Also if you're using Postgresql >= 8.4 you can use a window function.
SELECT ID, GroupId, Profit, (Profit/ (sum(Profit) OVER(partition by GroupId)))*100 as pct
FROM core_dev.my_table as mt