I have the below simple query:-
SELECT *
FROM dbo.vwPAProjects_Summary_New_2
which generates data in the following output:-
can anyone advise as to the best way to code in that when the 'PACONTNUMBER' and 'Project Type' is the same perform a sum to provide an overall total for each numerical value.
Example:-
You need to list all of your fields (don't use '*').
Then use the SUM() function on what you want summed.
Then, everything beyond the ProjectFeeAmount field will either need to be excluded from your list, or aggregated somehow (sum, min, max, etc.).
SELECT
Contract,
Sum(PAContNumber) as SumOfPAContNumber,
ProjectManagerID,
...
FROM
...
GROUP BY
Contract,
ProjectManagerID,
...
Try this:
SELECT PACONTNUMBER, Project Type,
SUM(Project_Fee_Amount) AS Project_Fee_Amount
FROM dbo.vwPAProjects_Summary_New_2
GROUP BY PACONTNUMBER, Project Type
And then add any other fields you would like to SUM to the select list.
This will get you started, you will need to add other SUM lines for the other values you want totalled
select PACONTNUMBER,
[Project Type],
sum([Project Fee Amount) as TotalProjectFees
from vwProjects_Summary_New_2
group by PACONTNUMBER,
[Project Type]
Related
select dc_id, whse_id, assg_id, START_DTIM,
UNIT_SHIP_CSE*prod_cub as TOTAL_CUBE
from exehoust.aseld
I attached a photo to show how the query currently populates. I want to sum the TOTAL_CUBE for each distinct ASSG_ID. I have tried case where sum and group by but keep failing. Basically want to do a SUM IF for each distinct ASSG_ID
You need to group by the assg_id, but ou need also the define what happens to all the other columns i choose MIN only to give you a hint, you need to choose the function yourself
select MIN(dc_id), MIN(whse_id), assg_id, MIN(START_DTIM),
SUM(UNIT_SHIP_CSE*prod_cub) as TOTAL_CUBE
from exehoust.aseld
GROUP BY assg_id
use select assg_id, sum() over(partition by assg_id order by assg_id) to sum by groupings
I have an existing table (link #1) that I am trying to write a query for so that the query reformats the data as seen in the second link. Basically it is a table listing the completed email types for a group of users. The "Completed Type" is a single column with multiple values. I am trying to parse out the individual values (3 of them) from the "Completed Type" into their own column with a total count. I also would like to add a seperate column called "Completed" which is simply a sum of "Closed without response" and "Replied" for that particular user for that particular month.
I plan on then creating a pivot in Excel that will read off of the new query with the reformated data. For the life of me, I can't figure out how to write this in SQL. I tried creating individual queries to total the different "Completed" types and then tried to union them, but it is not working.
Existing table
Future Query Output
Any advice or guidance you can provide in writing a SQL query in Access that will produce image # 2 would be GREATLY appreciated! Thank you in advance!
You can use case when and sum, for example:
select month,
id,
sum(case when completed_type = "completed" then 1 else 0 end) as completed
from table
group by month, id
Use a crosstab query:
TRANSFORM
Sum([Case Count]) AS [SumOfCase Count]
SELECT
[Month],
ID,
[Adjusted Name],
Mgr,
Sup,
Region,
Region2,
Sum(Abs([Completed Type] Not Like "Closed*")) AS Completed
FROM
Cases
GROUP BY
[Month],
ID,
[Adjusted Name],
Mgr,
Sup,
Region,
Region2
ORDER BY
ID,
[Month] DESC
PIVOT
[Completed Type] In ("Replied","Sent","Closed without response");
Output:
I have to aggregate in my query SUM of AMUNT field according to WERKS, DATUM and UZEIT
I try to make a group by without any success
I have an error like that:
What is the problem in my code?
That is my ABAP code:
DATA: gt_compr TYPE TABLE OF yrt_h_sales
SELECT werks, extnb, datum, uzeit, sumvt, deprt, dpext, SUM( amunt ) AS amunt
INTO CORRESPONDING FIELDS OF TABLE #gt_compr
FROM yrt_h_sales
WHERE werks IN #so_werks
AND datum IN #so_datum
GROUP BY werks, datum, uzeit.
After I corrected it and I did this, the code looks as follows:
SELECT werks, datum, uzeit, extnb, deprt, dpext, SUM( amunt ) AS amunt
INTO CORRESPONDING FIELDS OF TABLE #gt_compr
FROM yrt_h_sales
WHERE werks IN #so_werks
AND datum IN #so_datum
GROUP BY werks, datum, uzeit, extnb, deprt, dpext.
So I don't have the compilation error anymore but the aggregation is still not working! I have a 43 line result without sum on the AMUNT column
P.S. this is the structure of my table:
Your observation is consistent with the documentation (and what I have so far seen in any other RDBMS I've worked with):
If aggregate expressions are used, any column identifiers that are not
included as arguments of an aggregate function must be included after
the addition GROUP BY.
Take for example the time field UZEIT: You can tell the system to aggregate (in your case, sum up) all amounts for the same point in time by adding it to the GROUP BY clause, or you can apply an aggregate function as well (SUM would not make any sense here, but MIN might), or you could omit the field altogether. You can not leave it dangling around without further specification - the field either needs to be part of the new key set created by GROUP BY or has to have an aggregate function applied to it so that the system knows what to do with multiple datasets that might occur in the group.
(This is basic SQL btw and not ABAP-specific knowledge.)
remove the CORRESPONDING FIELD OF and just place results INTO TABLE
I am trying to get a certain expression to work in SSRS. My query pulls up a category, sub category and a count statement grouped by the sub category for tracking incidents. This is my query:
SELECT Category, Sub_Category, CAST(Count(incident) AS decimal) As Total_Incidences
FROM service_req
GROUP BY Sub_Category
I am trying to break these down into percentages, for example a specific sub category accounts for 67% of a certain main category incidences, and would also like to have a percentage of main categories vs total calls, i.e. 12% of all incidences are from this certain category.
My math is not coming out right when trying to make it work, and I cant figure out the best way to do this. Any suggestions? I know I have to divide the count statement by the sum of all incidences in the subcategory to get that percentage, and all sub category sums by the total incidences to get the first percentage however I am not sure how to use a group by with these expressions (or even if I can) to get this to work. Any help would be appreciated!
The problem can be solved not in SSRS but rather in the query that returns data. You need 2 separate groupings - one by category, another by category/subcategory combination. Then you can join them like this:
SELECT Sub.Category, Sub.Sub_Category, Sub.Total_IncidencesSub*100/Main.Total_IncidencesMain AS Percentage FROM
(SELECT Category, CAST(Count(incident) as DECIMAL) As Total_IncidencesMain
FROM service_req
GROUP BY Category) Main
INNER JOIN
(SELECT Category, Sub_Category, CAST(Count(incident) AS DECIMAL) As Total_IncidencesSub
FROM service_req
GROUP BY Category, Sub_Category) Sub
ON Main.Category = Sub.Category
Here is live demo: http://sqlfiddle.com/#!3/eee07/7
I am new to MDX expressions and I am trying to create one that sums the value of a given measure filtered by dimensions.
In my database I have several different dimensions that have the same name: "Answer". To sum them up, I have created the query below:
WITH MEMBER Measures.Total as SUM ({[Activity].[Activity].&[14], [Activity][Activity].&[22]},
[Measures].[Activity time])
SELECT NON EMPTY [Measures].[Total] on COLUMNS from [My Analytics]
This query works, however I had to use the "&[14]" and "&[22]" statments that correspond to two different "Answer" dimensions.
Since I have more than two dimensions with the same name, is there a way to rewrite the query above in a way that I would select all these dimensions without having to add their unique ID? For example, I would re-write the query as something like this:
WITH MEMBER Measures.Total as SUM ({[Activity].[Activity].&["Answer"]},
[Measures].[Activity time])
SELECT NON EMPTY [Measures].[Total] on COLUMNS from [My Analytics]
Is this possible?
Thanks!
You can use the Filter function as following:
with
set [my-answers] as
Filter( [Activity].[Activity].members,
[Activity].[Activity].currentMember.name = 'Answer'
)
member [Measures].[Total] as Sum( [my-answers] )
...