ordertype actual actual YTD
A 900 1500
B 500 2000
C 200 2200
D 300 2500
Actual column should calculate value from (present date of month minus start date).
Actual YTD should calculate based on present date minus start date of year
Here start date month/year should be april 1 (Financial Year)
In your script use InYearToDate and InMonthToDate
use-case example:
Load distinct
Date,
if(InYearToDate(Date,today(),0,4),1,0) as YTD, // the "4" mean that The year start at the 4th month,
if(InMonthToDate(Date,today(),0),1,0) as MTD
resident DataTable
Now in your Calendar table you the YTD and MTD fields and you can use them like this:
sum({<YTD={1}>} Actual)
for more information about these functions visit here
Related
I have the following table
image of database in use
i want to get the following kind of results
jan 12500
feb 16500
mar 4500
apr 6500
the query should return a total for each month for desired months.
i know how to do this..
$sql = "SELECT SUM(cost) as january FROM earnings WHERE month= 1 and year= '$2022" ;
to get the sum for a given month but I cant find anything on how to get multiple months at once.
am still new to this
SELECT
SUM(cost) as cost,
month
FROM earnings
WHERE year = :year
GROUP BY month
Sum all entries of cost, per month (GROUP BY) found in year (:year)
Each ROW will have a column cost and month.
If you want to "further" filter the months you can apply another AND clause
AND (month >= 1 OR month <= 6) for January to June
Useful Source:
https://www.mysqltutorial.org/mysql-group-by.aspx
How to calculate the price difference between the chosen date and 1 month before.
Example if user chooses 2/1(Feb 1), it will return the difference between 2/1(Feb 1) and 1/1(Jan 1). This is in SQL and data is daily. Date column has YYYY-MM-DD format.
Sample layout:
Date (YYYY-MM-DD) , Product Name (text), Price (int)
You can use a self-join for this. This query uses the same table twice - t_now includes prices this month; t_then shows last month's prices.
In the standard SQL you can write this
SELECT t_now.product, t_now.price - t_then.price
FROM prices t_now JOIN
prices t_then ON (t_now.date - INTERVAL 1 MONTH = t_then.date
AND t_now.product=t_then.product)
WHERE t_now.date='2018-05-01'
It works in MySQL http://sqlfiddle.com/#!9/f208c5/4
You will need changes to the month calculation for SQL Server - You can use DATEADD(MONTH, -1 date)
If you have many prices per month there will be there will be multiple answers at the end of a short month. There are four days one month before 28th Feb - namely 28th Jan, 29th Jan, 30th Jan and 31st Jan
Input
date product price
2018-05-01 Gadget 100
2018-05-01 Widget 50
2018-04-01 Gadget 20
2018-04-01 Widget 10
Output
product t_now.price - t_then.price
Gadget 80
Widget 40
In a form on Ms ACCESS, a user can select a commodity (such as copper, nickel, etc.) from a list and a commodity price date from a list. A trailing 12 month average commodity price should then be calculated.
For example: the user selects Copper as commodity and February 1st 2010, 02/01/2010. I then want the average price to be calculated over the time period: [02/01/2009 - 02/01/2010].
I'm not sure how to write this in query form. This is the current incomplete code;
SELECT Avg(CommPrices.Price) AS Expr1,
FROM CommPrices
WHERE (((CommPrices.Commodity)=[Forms]![Tool Should Cost]![List243]))
AND CommPrices.DateComm = [Forms]![Tool Should Cost]![List55];
List243 is the list of commodities the user can select from, list55 is the list of dates the user can select. All data is obtained from the table CommPrices.
Note: the earliest dates in the column DateComm is 01/01/2008. So if the user selects a date for example 02/01/2008, then calculating the average over the past 12 months before 02/01/2008 won't be possible. I do want the code to still calculate the average using the dates available. (in the example it would just be the average over the past month)
Second Note: the column DateComm only has monthly dates for the first day of every month (e.g 01/01/2008, 02/01/2008, 03/01/2008). The dates listed in list55 can refer to different days in the month (e.g 03/16/2009), in that case I want the code to still calculate the past 12 month average using the closest commodity dates possible. So if the user selects date 03/16/2009, I want the code to calculate the 12 month average for 03/01/2008 - 03/01/2009.
For "integer" months it would be:
SELECT
Avg(CommPrices.Price) AS AveragePrice,
FROM
CommPrices
WHERE
CommPrices.Commodity=[Forms]![Tool Should Cost]![List243]
AND
CommPrices.DateComm = BETWEEN
DateSerial(Year([Forms]![Tool Should Cost]![List55]) - 1, Month([Forms]![Tool Should Cost]![List55]), 1)
AND
DateSerial(Year([Forms]![Tool Should Cost]![List55]), Month([Forms]![Tool Should Cost]![List55]), 1)
I have this query to find out item count for every month of year. But I am looking for a optimized query for cumulative result
SELECT
COUNT(ITM.ID) AS ItemCount,
Month(ITM.ItemProcureDate),
Year(ITM.ItemProcureDate)
FROM
Rpt_Item ITM
WHERE
ITM.ItemProcureDate IS NOT NULL
AND
ITM.ItemStatusID = 2 -- Item sold, Item Rejected
AND
ITM.ItemProcureDate >= CONVERT(DATETIME,'02/01/2014',1) --#Beg_Date
AND
ITM.ItemProcureDate <= CONVERT(DATETIME,'04/12/2014',1) --#End_Date
GROUP BY
Month(ITM.ItemProcureDate),
Year(ITM.ItemProcureDate)
Query result should be like this:
Item sold In month 2
Item Sold Till Month 2
Item Rejected 1
Item Rejected Till Month 1
Year 2014
Month Feb
Last Date of Month 02/28/2014
-----------------------------------------------
Item sold In month 2
Item Sold Till Month 4
Item Rejected 1
Item Rejected Till Month 2
Year 2014
Month March
LastDate of Month 03/31/2014
-----------------------------------------------
Item sold In month 2
Item Sold Till Month 6
Item Rejected 1
Item Rejected Till Month 3
Year 2014
Month April
Last Date of Month 04/30/2014
I have to find out Item_Sold, Item_Rejected, Item_Added for last three months where every next month it should be cumulative of all previous months values of Item_Sold, Item_Rejected, Item_Added
In SQL Server 2008, you can do this using a correlated subquery or using a non-equijoin. SQL Server 2012 supports a cumulative sum function. Here is a way to do it with a correlated subquery:
with ym as (
SELECT COUNT(ITM.ID) AS ItemCount,
Month(ITM.ItemProcureDate) as mon, Year(ITM.ItemProcureDate) as yr,
Month(ITM.ItemProcureDate) + 100*Year(ITM.ItemProcureDate) as yyyymm
FROM Rpt_Item ITM
WHERE ITM.ItemProcureDate IS NOT NULL AND
ITM.ItemStatusID = 2 AND
ITM.ItemProcureDate >= CONVERT(DATETIME,'02/01/2014',1) AND
ITM.ItemProcureDate <= CONVERT(DATETIME,'04/12/2014',1)
GROUP BY Month(ITM.ItemProcureDate), Year(ITM.ItemProcureDate)
)
select ym.*,
(select sum(ItemCount)
from ym ym2
where ym.yyyymm <= ym.yyyy.mm
) as cumsum
from ym;
Note that this puts the year-month into a YYYYMM format. This is just a convenience so the comparison on the time period uses only one column.
Also, if the ITM table is really big or is a view, then this might not perform as well as one would like. If performance is an issue, use a temporary table instead of a CTE. (SQL Server tends not to materialize CTEs so it is likely to run the code twice.)
I have a SQL table with the following schema:
fruit_id INT
price FLOAT
date DATETIME
This table contains many records where the price of a given fruit is recorded at a given time. There may be multiple records in a single day, there may be
I would like to be able to fetch a list of prices for a single fruit over the last 12 months inclusive of the current month. So given a fruit_id of 2 and datetime of now(), what would the price values be for December, January, February, ... October, November?
Given the above requirements, what strategy would you use to get this data? Pure sql, fetch all prices and process in code?
Thanks for you time.
Are you talking about min price, max price, average price, or something else?
Here's a quick query to get you started, which includes min, max, and average price for each month for fruit_id 2:
select left(date,7) as the_month, min(price),max(price),avg(price)
from fruit_price
where fruit_id = 2
and date >= concat(left(date_sub(curdate(), interval 11 month),7),'-01')
group by the_month;
If I understand it correctly from -
I would like to be able to fetch a list of prices for a single fruit over the last 12 months inclusive of the current month. So given a fruit_id of 2 and datetime of now(), what would the price values be for December, January, February, ... October, November?
You want the total price for every month for a single year based on the date and fruit_if you pass in.
So,this won't give all months of an year but all months which had a price for year..in case you want all months..you would need to create a dimdate table which will have all the dates...and then join with it..
declare #passeddate=Now() --date to be calculated
declare #fruit_id=2 --fruit id to be calculated
Select
fruit_id as FruitId,
Sum(price) as MonthPrice,
Month(date) as FruitMonth
from SQL_Table
group by FruitMonth,FruitId
where fruit_id=#fruit_id and
Year(date)=Year(#passeddate)
select month(date) as "Month", distinct(price) as "Unique Price" where fruit_id = 2 group by month(date);
I'd try to state as much as possible in SQL that does not require unindexed access to data because it's usually fast(er) than processing it with the application.