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?
Related
I am trying to write sql requests to my database in order to get the last occurence of a certain item, here's what I mean illustrated by an example:
pigc=# SELECT date FROM msr_history WHERE pos = 'DV' AND msr_id = 177;
date
----------------------------
2018-06-20 08:04:09.724103
2018-06-20 08:09:49.484921
(2 rows)
The first line of my example is my sql request and I am trying to get only the last date, in this example it would be this one: 2018-06-20 08:09:49.484921
Note that the number of dates can differ so I can't just manualy select the second date each time. Thank you for your help.
Use select max(column) to retrieve the highest value for that specific data type for all rows included in the where clause.
In this instance, no aggregation is required since no non-aggregated columns are being retrieved.
See https://www.postgresql.org/docs/9.5/functions-aggregate for a complete list of aggregate functions and specifically https://www.postgresqltutorial.com/postgresql-max-function
The fastest way to achieve it to use the LIMIT clause in your query. This will prevent using aggregate function in your query and will run fastest -
SELECT date
FROM msr_history
WHERE pos = 'DV'
AND msr_id = 177
ORDER BY date DESC
LIMIT 1;
I have written a query which is bring following data:
I want to further group it by month and year to bring total quiz and total pass for each month in the result set, I am stuck and i need help with this,
thank you
You can group by multiple columns
select QuizMont, QuizYear, .. aggregate values here
...
group by QuizMont, QuizYear
I want to get the sum on a specific date given e.g from 18/12/04 to 18/12/15 then calculate the total amount
The key is in the GROUP BY statement.
If you want to get the sum per tenant and date, group by them both.
If you want to get the sum per date, group by date only (and remove the tenant colums [or select MAX] from the SELECT).
Paste your code as text and I can edit it for you if you want.
If you want to get the total as additional column, you can use WINDOW functions:
SELECT tenant_id
,SUM(total_amount) OVER (PARTITION BY tenant_id)
,SUM(total_amount) OVER ()
FROM tenant_reeipits
WHERE ...
ORDER BY ...
I am trying to create a running balance column in Spotfire that's supposed to look like the picture attached below. In essence,I want to calculate the cumulative total of the "amount" column row by row, and that I want it to start from 0 as the date changes.
I have tried several OVER functions:
Sum([AMOUNT]) OVER AllPrevious([Date])
Sum([AMOUNT]) OVER Intersect([CURRENCY],AllPrevious([SETTLEDATE]))
Sum([AMOUNT]) OVER Intersect([Calculation Date],AllPrevious([SETTLEDATE]))
Any help is greatly appreciated.
You were very close with your first over statement. The problem is, when you use over (AllPrevious([Date])) and you don't have 1 row for each date, then you will skip rows. So, the last row of your data would only sum it over the rows where 6/1/2017 is in the Date column. Instead, we need to apply a RowID to your data set and then sum over it. This will ensure we sum over all previous rows.
Assuming your data set is in the order you want it to be in when you bring it into SpotFire, do the following:
Insert a calculated column RowID() and name it RowID
Use this calculation: Sum([amount]) over (Intersect([Date],AllPrevious([RowID])))
This will give you the running sum you are looking for.
#scsimon- I modified your custom expression slightly to include date as requested in the question.
Modified expression:
Sum([Amt]) over (intersect(Allprevious([rowID]),[Date]))
Final output table:
#LeoL - Hope this answers your question.
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'