Display By month using select statement - sql

SELECT SUM(Total_A ) FROM Materials_List
This is the snippet of code that I have.
I need it to calculate by month and display by month using SQL.
I also would like it to be a code I can use for any month in the year not just one month at a time.

You seem to be looking for simple aggregation:
select
year(materials_datetime) yr,
month(materials_datetime) mn,
sum(total_a) sum_total_a
from materials_list
group by
year(materials_datetime),
month(materials_datetime)
order by yr, mn
This assumes that column materials_datetime contains the date/time that you want to use to aggregate the data.

Related

Identify “Start” and “End” date of a trip in Netezza?

I want to get how many times a pack has been used in any given month, Each time the pack is activated it can be used for 7 days.
Expected Result
I have tried Lag and lead along with nesting the query.
Here's what you need.
select
max([Date]) as Month_END,
Line_Number,
Pack_Code,
Product_Code,
count(1) as [Number Of Packs]
from
Table
group by
datepart(mm, [Date]),
Line_Number,
Pack_Code,
Product_Code
Some issue with my laptop. Before i complete my answer, it has posted my response :).
My previous answer will give you the total number of times in a month a pack has been used under count variable. You can then divide the aggregated value by 7 to identify the number of active packs. Hope it helps
You can try converting the date to year month using the below syntax and then apply a group by on the required columns to get the count of the pack_code
to_char(to_Date(LOADED_DT,'YYYY-MM-DD HH24:MI:SS'),'YYYYMM') year_month
Sample query:
select a.year_month,a.line_number,a.pack_code,a.product_code,count(a.pack_code)
select to_char(to_Date(LOADED_DT,'YYYY-MM-DD HH24:MI:SS'),'YYYYMM') year_month,line_number,pack_code,product_code,count(pack_code)
from <table_name> ) a
group by a.year_month,a.line_number,a.pack_code,a.product_code;

Sort Month+Year in VB.net using MS-ACCESS as db

I am using following query
select month(datee) as [Month], year(datee) as [Year]
from ledger_broker
group by month(datee),year(datee)
Output :
What's the query to combine both columns and show as
1-2016
2-2016
OR
Jan-2016
Feb-2016
Use a Format() expression to format the month plus year as you wish. For months as digits, this should work:
SELECT DISTINCT Format(datee, 'm-yyyy') AS month_year
FROM ledger_broker
ORDER BY Month(datee), Year(datee);
For abbreviated month names, adjust the Format pattern:
SELECT DISTINCT Format(datee, 'mmm-yyyy') AS month_year

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause

T-SQL average calculation

I want to incorporate two average calculations for a bunch of value columns in my select statement.
see this link for my simplified table structure including the desired output calculation: Pastebin
1) moving average:
Month1 = value of the value1-column for that month, Month2 = if sum == 0 then write 0, else avg(Month1 and Month2) and so on.
So for each product, I want the moving average for each month within one year.
I have this set up in my Excel but I can't transfer the expression to sql.
2) overall average:
for each product, calculate the average over all years and duplicate the calculated value to all rows for that product.
I hope you can help me out with this. It looks like I need a procedure but maybe it is just a simple statement.
SQL-Server 2012 supports the analytic functions required to do this:
SELECT Product,
Month,
Year,
Value,
AVG_YTD = AVG(Value) OVER(PARTITION BY Year ORDER BY Month),
AVG_Year = AVG(Value) OVER(PARTITION BY Product, Year),
AVG_Overall = AVG(Value) OVER(PARTITION BY Product)
FROM T;
Simplified Example on SQL Fiddle

Not using group by in second part of sum

This is my sum clause
Select *,(sum(current_bal-curr_bal_now)/
current_bal from base
Group by month
This gives me an error because I'm not using current_bal in the group by.
Is there a way of not using group by current_bal aswell as month as it completely messes up the output layout.
Thanks
Another guess...
SELECT *,
( sum(current_bal) OVER (PARTITION BY month) ) / current_bal
FROM base
The problem is that the sum will return you only a value, so, in your all select, Current_bal is different. Which one should choose ?
If what you want is add every division, something like this:
Select sum(current_bal/current_bal)
from base
Group by month
will work