sql sub and grand total - sql

Is there a nice consise way to get grand totals here-- this query shows sub totals for each day, but how can I get the grand total of each sub total without a sub query or another query
also is there something better to use than compute sum?
SELECT loguser,logdate,logaction,logtime
FROM log WHERE loguser IN
('scott') AND logdate
BETWEEN '2011-06-01' AND '2011-06-15'
ORDER BY logdate DESC
COMPUTE SUM(logtime) BY logdate
SQL Server 2008 R2

I'm not familiar with COMPUTE as you've used it, but this (or a variation on it) should work in most SQL dialects...
SELECT loguser,logdate,logaction,SUM(logtime)
FROM log WHERE loguser IN
('scott') AND logdate
BETWEEN '2011-06-01' AND '2011-06-15'
GROUP BY loguser,logdate,logaction WITH ROLLUP
ORDER BY logdate DESC

Better way to get grand total is to use front end..instead of query.

I think you should look at using ROLLUP over COMPUTE as Dems suggests, but if what you want is 3 things returned- 1) the daily logtime data, 2) the daily subtotal; and, 3) the grand total (total of subtotals) then I think you will need to UNION a query that finds the first two like the one you have with a query that finds the grand total.

Related

Nesting Queries in Access SQL

I have recently started using SQL and am stuck when applying it to Access. My previous experience (limited) has been with PostgreSQL, and I understand that SQL in Microsoft Access requires you to nest queries into sub-queries, which I am not familiar with.
I believe the code in SQL (not for access would look something like this...)
select weeks, sum(sweets_eaten), count(distinct (sweet))
from table
group by weeks;
This would then give me a table where I would have the unique weeks, the sum of sweets eaten per week and the number of sweets per week.
Ideally, what the query would then do is also tell me the average sweets eaten per week by dividing the total sweets eaten per week by the number of sweets.
Does anyone know how to write a query so this will work in Microsoft Access?
Thanks!
Edited code, this is what I am entering
select f15, sum(f16), count(*)
from (select f15, sum(f16) as sum_sweets_eaten
from table1
group by f15, f16
) as t
group by f15;
For the average, would it be possible to do this in addition to the sum.
The query you have written will not work in MS Access, because it does not support count(distinct).
You can pre-aggregate to get the result you want:
select weeks, sum(sum_sweets_eaten), count(*)
from (select weeks, sum(sweets_eaten) as sum_sweets_eaten
from table
group by weeks, sweet
) as t
group by weeks;
To get the average, use avg() rather than sum().

Get The Total of all columns after Pivoting Access Query

Below is my access query in which i am uing Pivot to get the desired result. I am getting what i want but now just need to show one additional column in the end i.e. Sum of all the hours. Below is my query and result as well.
Query :
TRANSFORM SUM(TIBWRKHRS.WRKD_HRS_CNT)
Select TIBWRKHRS.WRKD_BY_USER_ID From TIBWRKHRS
Where CREATE_TS between Date()-5 and Date()+1
GROUP BY TIBWRKHRS.WRKD_BY_USER_ID
PIVOT TIBWRKHRS.REPORT_DATE
TRANSFORM SUM(TIBWRKHRS.WRKD_HRS_CNT)
Select TIBWRKHRS.WRKD_BY_USER_ID From TIBWRKHRS, sum(TIBWRKHRS.WRKD_HRS_CNT) as Total
Where CREATE_TS between Date()-5 and Date()+1
GROUP BY TIBWRKHRS.WRKD_BY_USER_ID
PIVOT TIBWRKHRS.REPORT_DATE
This puts the total before the individual columns - close enough?

Need Alternative to SQL AVG() for Obvient BI tool

I am reading 2 fields from 1 table.
StartKey and Mins
Image below shows my current output result on left and what I need on right.
Here is my Query
Select
StartKey,
Duration as Mins
From TableA
Where Flag = 0
Order by StartKey
I know I can use avg(duration), but if I use that, Obvient, the software I am using to write and display the query, won't let me take the average of column Mins Avg itself.
This error I get after I manually insterted average code of column in CS file and then I try to edit column properties.
First, let me make sure I understand your problem.
You are using the SQL from your post while building something in Obvient which appears to be a Business Intelligence platform. The problem you are having is that you are unable to perform an average function in Obvient on the column of averages in your SQL query.
If that is correct, you should use your SQL query to create a view in the database which should appear to Obvient as a table and may allow you to perform the averaging function. I can't say for certain that this will solve your issue having never used Obvient, but give that a try and let us know how that works for you.
Seems like I'm missing something, but to get your desired results, this should work:
Select
StartKey,
AVG(Duration) as Mins
From TableA
Where Flag = 0
Group By StartKey
Order by StartKey
And the SQL Fiddle.
If your goal is to get the AVG(Mins) from the above query, you could use a subquery to return that:
Select AVG(Mins)
FROM (
SELECT
StartKey,
AVG(Duration) as Mins
From TableA
Group By StartKey
) t
Here is the Fiddle:
Good luck.

How do I Sum the values of a Count Distinct Aggregate of Contained Groups

I've got a bit of a problem with a grouping. At the moment I have a grouping that does a CountDistinct(Fields!AccountName.Value). This grouping is on a Financial Time Period.
I need to be able to do a Sum on the values brought forward by this CountDistinct at the end of this report, but I can't put an aggregate function within an aggregate function.
Don't suppose you guys have any idea's / help?
Thanks.
You can do a select and aggregate your set of aggregates. Something like:
SELECT sum(total)
FROM (Select count(Fields!AccountName.Value) as total, name FROM x group by name) totals

Sql Server : Column wise total SQl Query

Is it possible to get column wise total using query?
in my grid there are 20 columns. i have to display each columns total value in its footer. now im using TemplateField field and javascript function to get the total value.if it is possible to get it from sql query i can reduce the code
Try something like:
SELECT *, SUM(SalesAmount) OVER() as TotalSales
FROM YourTable
But if you only need the sum and nothing else, just do:
SELECT SUM(SalesAmount) as TotalSales
FROM YourTable
And in future, please try to give more information in your question.
Rob
To sum columns, it's best to use whatever client you're dealing with (Reporting Services, Datagrid, whatever), and just tell that to display a totals row.
If you were to do it within the same query, then you'd end up with rows that meant something different, and displaying it becomes quite awkward.
You CAN do it in the query, but you probably shouldn't.
Rob
I think you are looking for SUM function
Eg:
SELECT SUM(salary) as "Total Salary"
FROM employees
select MAX([p-1]) p1,MAX([p-2]) p2 from #temp