I am calculating weighted formula for a field as sum(revenue)/ Sum(qty) and this is as per the below query. Now I will be creating a view that would store these results as I shown in code below.
My question is, if I select this w_revenue out of the view and want to see per year, how will I aggregate to show it per year?
select month, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year;
create view xyz_rev as
Select month, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by month, year;
select year, w_revenue
from
xyz_rev ;
You'll aggregate W_REVENUE once again:
select year, sum(w_revenue)
from xyz_rev
group by year
Since there is no real possibility to get the w_revenue for the year from just knowing these values for the months i would suggest to change the view as following:
create view xyz_rev as
Select month, sum(revenue)/ Sum(qty) as w_revenue,
year
from my_revenue_table
group by ROLLUP(MONTH), year;
This way you'll already have to year data along with the data for the single months.
Related
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 don't know anything about SQL. I currently have a query that gives me this. Sales for some products by channel/etc (please note this is a very simplified version, there's more fields) by week/period/year:
Basically what I would need is to add a column that gives me the sales for prior year. Basically, transform the table as below. In Excel it would be a simple sumifs that would just sum the same exact criteria aside from the year which would be the previous year.
Is it possible to do this within SQL? The dataset is too large to do it within Excel.
I think you just want lag():
select t.*,
lag(sales) over (partition by channel, product, weekno order by yearno) as prev_sales
from t;
If I understand the data, then periodno is redundant with weekno.
just trying to use the window function of doing a cumulative sum against a month as follows.
sum(MeterReading) over (partition by Serial, code order by month(MeterReadingDate)) as cumulative
this seems to be way to slow to run and doesn't bring any results after waiting, is there something i am doing wrong?
Basically I want to see the sum against each month for each serial/code.
Select
serial,
code,
DATEPART(YEAR,MeterReadingDate) as Year,
DATEPART(MONTH,MeterReadingDate) as Month,
sum(MeterReading) over (
partition by
Serial,
code,
Datepart(YEAR,MeterReadingDate),
Datepart(MONTH,MeterReadingDate)
) as cumulative
from table
First making a sum with an order by claus makes no sense since you want to add all results for one month together.
Second, adding the two dateparts for year and month will partition your data so that the sums only will add meter readings from one month.
If you are interested in seeing yearly variations per month then you can remove the Datepart(YEAR,...) and add some average perhaps.
sum(MeterReading) over (
partition by Serial,
code,
DATEADD(MONTH, DATEDIFF(MONTH, 0, MeterReadingDate), 0)
order by MeterReadingDate
) as cumulative
The function with DATEADD and DATEDIFF converts a date to the first of the month.
Then I add this function to the PARTITION to group by Serial, Code and Month.
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