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.
Related
Very simple question here, but a quick google search didn't seem to be definitive (and I do not have access to a DB to test right now). I would like to check whether you can do "on the fly" grouping in Postgres (as is possible in SQL Server). best way to clarify is an example i.e. can I do this to group by weekly periods:
select ...
from ...
group by cast((current_date - transaction_date)/7 as int)
or is it necessary to first define a week column in a subquery (as per the calculation above) and then do the grouping?
Thanks in advance for your help.
You can include most expressions in the GROUP BY, so your code is fine. This is true in Postgres and in almost any database.
It is unusual to have an aggregation query where the aggregation expressions are not part of the GROUP BY. But if you have data on every day, then this is a sensible query:
select min(date_trunc(transaction_date)) as week_start, count(*)
from ...
group by cast((current_date - transaction_date)/7 as int)
You surely can. I would slightly modify your example like
select cast((current_date - transaction_date)/7 as int) as wp, ...
from ...
group by wp;
I'm working on a query that pulls a date from another query, I have my reasons for the nesting. The problem I'm facing is that there is a field that is called DueDate.
My SQL is
SELECT DueDate
FROM qryDueDates
WHERE DueDates <= DateAdd("d",60,Date())
The data causing the issue is when it equals something like "1/25/2019", "11/19/2019" or any date in 2019.
Goal
I need to limit the results to show dates that are expired or expiring within 60 days or less.
I'm trying to prepare the dataset for the conditional formatting.
if you can put your nested sub-query in your post that may give better picture, and if you can mention what is the error you are getting that may also help. Since you mentioned that you are getting error only when sub-query returns certain dates, I would suggest that cast your sub-query result to DATE if you have not already done.
Below is my attempt to help you with limited information I could extract from your post. I have used some of MS-SQL function below, please replace with your DB specific function.
SELECT myDates.* FROM (select COLUMN_NAME DueDates from TABLE_NAME) as myDates WHERE myDates.DueDates <= DateAdd("d",60, GETDATE())
Turns out that the original query was screwing it up. I moved the query into the main one and it worked.
Im trying to make a small report for myself to see how my much time I get inputed in my system every day.
The goal is to have my SQL to sum up the name, Total time worked and Total NG product found for one specific day.
In this order:
1.) Sort out my data for a specific 'date'. I.E 2016-06-03
2.) Present a DISTINCT value for 'operators'
3.) SUM() all time registered at this 'date' and by this 'operator' under 'total_working_time_h'
4.) SUM() all no_of_defects registered at this 'date' and by this 'operator' under 'no_of_defects'
date, operator, total_working_time_h, no_of_defects
Currently I get the data I want by using the Query below. But now I need both the DISTINCT value of the operator and the SUM of the information. Can I use sub-queries for this or should it be done by a loop? Any other hints where I can learn more about how to solve this?
If i run the DISTINCT function I don't get the opportunity to sum my data the way I try.
SELECT date, operator, total_working_time_h, no_of_defects FROM {$table_work_hours} WHERE date = '2016-06-03' "
Without knowing the table structure or contents, the following query is only a good guess. The bits to notice and work with are sum() and GROUP BY. Actually syntax will vary a bit depending on what RDBMS you are using.
SELECT
date
,operator
,SUM(total_working_time_h) AS total_working_time_h
,SUM(no_of_defects) AS no_of_defects
FROM {$table_work_hours}
WHERE date = '2016-06-03'
GROUP BY
date
,operator
(Take out the WHERE clause or replace it with a range of dates to get results per operator per date.)
I'm not sure why you are trying to do DISTINCT. You want to know the data, no of hours, etc for a specific date.
do this....
Select Date, Operator, 'SumWorkHrs'=sum(total_working_time_h),
'SumDefects'=sum(no_ofDefects) from {$table_work_hours}
Where date='2016-06-03'
Try this:
SELECT SUM(total_working_time) as total_working_time,
SUM(no_of_defects) as no_of_defects ,
DISTINCT(operator) AS operator FROM {$table_work_hours} WHERE
date = '2016-06-03'
I am doing a Union Query to add together the results of two separate queries that give me data from two different fiscal periods, to get a rolling 12 months number.
I get the message "Your query does not include the specified expression "Report_Header" as part of an aggregate function". I have read that the field needs to be included in a GROUP BY statement at the end, but when I add the field from either query or with both queries as shown below I still get the message. Help? I'm not a programmer, I'm an Access user, so I need to simple please :).
SELECT [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].Report_Header,
Sum([JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].SumOfCASES) AS CASES,
Sum([JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].SumOfPurchases) AS PURCHASES
FROM [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB]
UNION ALL
SELECT [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].Report_Header,
Sum([JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].SumOfCASES) AS CASES,
Sum([JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].SumOfPurchases) AS PURCHASES
FROM [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2]
GROUP BY [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].Report_Header,
[JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].Report_Header
Thanks!
You can aggregate both subqueries:
SELECT [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].Report_Header,
Sum([JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].SumOfCASES) AS CASES,
Sum([JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].SumOfPurchases) AS PURCHASES
FROM [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB]
GROUP BY [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB].Report_Header
UNION ALL
SELECT [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].Report_Header,
Sum([JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].SumOfCASES) AS CASES,
Sum([JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].SumOfPurchases) AS PURCHASES
FROM [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2]
GROUP BY [JOIN_IB_FREIGHT&PURCHASES_Rolling12_SUB2].Report_Header;
This may be what you want. But, it will not combine information under the same header from both tables. For that, the simplest method is probably a view.
Place GROUP BY [JOIN_IB_FREIGHT&PURCHASES_ROLLING12_SUB].Report_Header under the first query instead of the second.
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