I have a table called payhistory, and from this table I need to grab the Year, Month when a payment was made. I then need to get an average of payments grouped by Year and then month.
select AVG(totalpaid) from payhistory
This table has a column called datepaid that is where I need to grab the date from. It has a column called totalpaid which is where I need to grab the average from.
I then need a count of every payment within that month.
And finally a column displaying which Year/Month it was from.
Important columns in the table:
number (account number can use this to get a count)
datepaid (contains the date the payment was entered '2009-09-28 00:00:00.000')
totalpaid (contains the specific payment for that date)
This should work:
select AVG(totalpaid),
COUNT(number),
MONTH(datepaid),
YEAR(datepaid)
from payhistory
group by
MONTH(datepaid),
YEAR(datepaid)
Related
I am using following query to get records count on month wise and it is working fine:
SELECT MONTH(dte_cycle_count) MONTH, COUNT(*) COUNT
FROM inventory
WHERE YEAR(dte_cycle_count)='2021' --OR (MONTH(dte_cycle_count) = '1' OR MONTH(dte_cycle_count) = '12')
GROUP BY MONTH(dte_cycle_count);
Problem:
Now I need to bind rollover calendar so user can scroll or click on next or previous button the next 12 Months record will be visible.
eg. Current month is MARCH, So default records will be from APR2020 to MARCH2021. If user click on previous then records will come MAR2020 to FEB2021.
How I can achieve this?
Please let me know if need more information. I will try my best to provide.
I think what you are after is a date list from which to join to your inventory table.
Like a numbers table, build a static table with columns for date, year, month, populated from whenever you need to far in the future.
You then select from this, applying your filtering range critera, and join to your inventory table.
For an efficient query, ideally your inventory table should have the relevant date portions eg year and month stored to match.
You don't want to be using functions on a datetime to extract the year or month as this is not sargable and will not allow any index to be used for a seek lookup.
Beginner here. I want to have only one row for each delivery date but it is important to keep the hours and the minutes. I have the following table in Oracle (left):
As you can see there are days that a certain SKU (e.g SKU A) was delivered twice in the same day. The table on the right is the desired result. Essentially, I want to have the quantities that arrived on the 28th summed up and in the Supplier_delivery column I want to have the earliest delivery timestamp.
I need to keep the hours and the minutes otherwise I know I could achieve this by writing sth like: SELECT SKU, TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD'), SUM(QTY) FROM TABLE GROUP BY SKU , TRUNC(TO_DATE(SUPPLIER_DELIVERY), 'DDD')
Any ideas?
You can use MIN():
SELECT SKU, MIN(SUPPLIER_DELIVERY), SUM(QTY)
FROM TABLE
GROUP BY SKU, TRUNC(SUPPLIER_DELIVERY);
This assumes that SUPPLIER_DELIVERY is a date and does not need to be converted to one. But it would work with TO_DATE() in the GROUP BY as well.
I am trying to summarize monthly loan originations by month from a table that contains loan level data going back to the late 90s. Every month, the most recent loan-level data are added to the table – the month_key field is used to identify the most recent records. I want to group the loan origination dates by month and sum the total loan commitments originated in the individual months. The table attached depicts how I want to summarize the data in my query, and the code below is what I have written thus far - it outputs data summarized by month dating back to the 90s. Thanks for the help.
Edit: JMB's solution worked. Once I added the month_key field back in, and sorted for the latest month on record, and summed the original loan balance I received the output I needed.
select SUM(INDIVIDUAL_LOAN_BALANCE) AS MONTHLY_LOAN_ORIGINATIONS, ltrim(TO_CHAR(ORIG_OBGN_DATE,'mm-yyyy'), '0') AS ORIG_MONTH
FROM LOAN_TABLE
WHERE MONTH_KEY = 202002
group by ltrim(TO_CHAR(ORIG_OBGN_DATE,'mm-yyyy'), '0')
HAVING ltrim(TO_CHAR(ORIG_OBGN_DATE,'mm-yyyy'), '0') >= '12-2018'
ORDER BY ltrim(TO_CHAR(ORIG_OBGN_DATE,'mm-yyyy'), '0') DESC
Example for how I want the query to depict the data:
How the query currently outputs the data
I would suggest leveraging the date datatype to filter, aggregate and sort: this makes things much easier and safer (typically, your where clause compares strings, and does not do what you expect). You can handle the formatting in the select clause.
select
sum(individual_loan_balance) as monthly_loan_originations,
ltrim(to_char(trunc(orig_obgn_date, 'mm'),'mm-yyyy'), '0') as orig_month
from loan_table
where orig_obgn_date >= date'2018-01-01'
group by trunc(orig_obgn_date, 'mm')
order by trunc(orig_obgn_date, 'mm')
I want to create a report that gives me a total for sales by month so
select customer,month(dated), sum(invtotal)
from salestable
group by customer,dated
gives me the my result but I get multiple lines returned if a customer had three orders for a particular month.
I was expecting the month(dated) to strip out the day part of the date and just return everything for a particular month as one whole, it does not appear to do that.
Any ideas on what I am doing wrong?
select customer, month(dated), sum(invtotal)
from salestable
group by customer, month(dated)
I hope the title of this post makes sense.
The db in question has two columns that are related to my issue, a date column that follows the format xx/xx/xxxx and price a column. What I want to do is get a sum of the prices in the price column based on the month and year in which they occurred, but that data is in the other aforementioned column. Doing so will allow me to determine the total for a given month of a given year. The problem is I have no idea how to construct a query that would do what I need. I have done some reading on the web, but I'm not really sure how to go about this. Can anyone provide some advice/tips?
Thanks for your time!
Mike
I was able to find a solution using a LIKE clause:
SELECT sum(price) FROM purchases WHERE date LIKE '11%1234%'
The "11" could be any 2-digit month and the "1234" is any 4 digit year. The % sign acts as a wildcard. This query, for example, returns the sum of any prices that were from month 11 of year 1234 in the db.
Thanks for your input!
You cannot use the built-in date functions on these date values because you have stored them formatted for displaing instead of in one of the supported date formats.
If the month and day fields always have two digits, you can use substr:
SELECT substr(MyDate, 7, 4) AS Year,
substr(MyDate, 1, 2) AS Month,
sum(Price)
FROM Purchases
GROUP BY Year,
Month
So, the goal is to get an aggregate grouping by the month?
select strftime('%m', mydate), sum(price)
from mytable
group by strftime('%m', mydate)
Look into group by