Is it possible to group by specific date in ms access? I would like to create a monthly report which will bring me the expenses/income from my database. However, I want this report to group the results NOT BY YEAR (1/1-31/12) but from 1/9-31/8 (my business year is from September-August). Is it possible to group the results the way I want?
You can try:
Select
Format(DateAdd("m", 4, YourDateField), "yyyymm") As FiscalYearMonth,
<other fields>
From
YourTable
Group By
Format(DateAdd("m", 4, YourDateField), "yyyymm")
For your specific table, it could be something like:
Select
Description,
Format(DateAdd("m", 4, [YourDateField]), "yyyymm") As FiscalYearMonth,
Sum([Sum]) AS [Sum]
From
[MyTable]
Where
[MyTable].Category = "Misc"
Group By
[MyTable].Description,
Format(DateAdd("m", 4, YourDateField), "yyyymm")
Related
Show Report - Click Here In my query I have a year field, and I have column grouping on it. I have row grouping on Product group, Product section, and item. If there are no sales in a section for this year and sales were $214 last year, my difference column for that section shows $214 not ($214). Mind you, no sales equal no data in the database, so this year sales is a null value in the report.
This is my expression that I am using, however it is not working:
=IIF(
ISNothing(Sum(IIF(Fields!Year.Value = MAX(Fields!Year.Value),
1,
0) * Fields!Subtotal.Value)) = True,
-1 * (Sum(IIF(Fields!Year.Value = Min(Fields!Year.Value),
1,
0) * Fields!Subtotal.Value)),
Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value),
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
)
As you know your problem is generated in this expression:
Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value),
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
You mentioned in comments there is no rows for Stock Cabinets in 2015 so the maximum year value is 2014 for that group.
Try using this:
Sum(IIF(Fields!Year.Value = Today.Year,
Fields!Subtotal.Value,
-1 * Fields!Subtotal.Value))
It will work if your maximum year is the current calendar year. Today.Year takes the current calendar year of the system at runtime.
UPDATE: I tried to create a minimal and verificable example of your problem.
This is the dataset I've used.
select 10 Value, 2015 [Year], 'Otro' Category
union all
select 20 Value, 2015 [Year], 'Otro' Category
union all
select 30 Value, 2015 [Year], 'Otro' Category
union all
select 10 Value, 2014 [Year], 'Otro' Category
union all
select 7 Value, 2014[Year], 'Stock Cabinets' Category
union all
select 3 Value, 2014 [Year], 'Stock Cabinets' Category
I used the same expression in my total column, the today.Year expression is the only change I made.
=IIF(
ISNothing(Sum(IIF(Fields!Year.Value = max(Fields!Year.Value),
1,
0) * Fields!Value.Value)) = True,
-1 * (Sum(IIF(Fields!Year.Value = Min(Fields!Year.Value)
,1
,0) * Fields!Value.Value)),
Sum(IIF(Fields!Year.Value = Today.Year,
Fields!Value.Value,
-1 * Fields!Value.Value))
)
In a matrix I set this data arrangement:
I get this:
Let me know if this can help you.
I figured it out. I used this for my expression:
=IIF(MAX(Fields!Year.Value)<> MAX(Parameters!Year.Value(0)), Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value), Fields!Subtotal.Value, -1*Fields!Subtotal.Value))*-1, Sum(IIF(Fields!Year.Value = Max(Fields!Year.Value), Fields!Subtotal.Value, -1*Fields!Subtotal.Value)))
I have a donation database and one of the reports I run against it I would like to include the number of donations that equal the months maximum donation. For example the months highest donation may be $100, but there may be 5 people who all donated $100, I would like to get that count.
My current query is:
SELECT SUM(mc_gross) AS Donations,
SUM(mc_fee) AS Fees,
COUNT(payment_date) AS DontationCount,
COUNT(DISTINCT payer_email) AS DonatorCount,
MAX(mc_gross) AS MaxDonation,
#MaxD:=MAX(mc_gross),
(
SELECT COUNT(*)
FROM #__paypal_donations
WHERE MONTH(payment_date) = MONTH(CURDATE())
AND YEAR(payment_date) = YEAR(CURDATE())
AND mc_gross = #MaxD
) as MaxDonationMultiplier,
AVG(mc_gross) AS AverageDonation
FROM #__paypal_donations
WHERE MONTH(payment_date) = MONTH(CURDATE())
AND YEAR(payment_date) = YEAR(CURDATE())
So I think I may be close, but it looks like either the value I am storing in #MaxD for use in my subquery is not working or the comparison itself in mc_gross = #MaxD is not working because if I replace #MaxD with a real value I get a proper count.
You cannot depend on the order of assignment of expressions in MySQL. That makes a query such as yours quite dangerous. Fortunately, you can easily solve this problem with a correlated subquery:
SELECT SUM(mc_gross) AS Donations, SUM(mc_fee) AS Fees, COUNT(payment_date) AS DontationCount,
COUNT(DISTINCT payer_email) AS DonatorCount, MAX(mc_gross) AS MaxDonation,
(SELECT COUNT(*)
FROM #__paypal_donations pd2
WHERE MONTH(pd2payment_date) = MONTH(pd.payment_date)) AND
YEAR(pd2payment_date) = YEAR(pd.payment_date) AND
pd2.mc_gross = MAX(mc_gross)
) as MaxDonationMultiplier,
AVG(mc_gross) AS AverageDonation
FROM #__paypal_donations pd
WHERE MONTH(payment_date) = MONTH(CURDATE()) AND
YEAR(payment_date) = YEAR(CURDATE());
I'm trying to select all customers who have at least one purchase in every one of the three months defined by a function called PrevMonth that accepts 2 parameters and returns a string 'YYYY-MM'. First parameter is a date and the second is the number of months to be subtracted from the date in first parameter.
delimiter #
drop function if exists PrevMonth#
CREATE FUNCTION PrevMonth (
in_date DATE
, in_mn_adjust INT)
RETURNS varchar(20)
BEGIN
DECLARE adjusted_date varchar(20);
set in_date := coalesce(in_date, current_date());
set adjusted_date := in_date;
set adjusted_date := date_format(date_sub(adjusted_date, interval in_mn_adjust month), '%Y-%m');
RETURN adjusted_date;
END;
#
And here's my query to select the customers that made at least one purchase each month (I manually counted and there's only one customer)
select DISTINCT oh.cust_id
, concat(cust_name_last,', ',cust_name_first) as 'customer name'
, order_date
from a_bkorders.order_headers oh
join a_bkorders.customers cu on oh.cust_id = cu.cust_id
where Date_format(order_date, '%Y-%m') in (PrevMonth(current_date(), 4))
AND oh.cust_id IN
(
SELECT cust_id
FROM a_bkorders.order_headers
WHERE Date_format(order_date, '%Y-%m') in (PrevMonth(current_date(), 3)))
AND oh.cust_id IN
(
SELECT cust_id
FROM a_bkorders.order_headers oh
WHERE Date_format(order_date, '%Y-%m') in (PrevMonth(current_date(), 2)))
;#
And some strange reason that I can't figure out, it only show me names of customers from the first month (PrevMonth(current_date(), 4) and that's it. The 'AND's and subqueries didn't work.
Somebody knows why??
Thank you for your help,
Didi
OK, after getting stuck for 3 days on the same problem. I've realized that in this case, an intersection (or AND.. IN for MySQL) wouldn't work. So to get the customer that has purchased at least one order in each of the months, I had to use correlated joins with EXISTS. Now, I get one name.
select DISTINCT cust_id, concat(cust_name_last,', ',cust_name_first) as 'customer name'
from a_bkorders.customers
where exists
(select *
from a_bkorders.order_headers
where order_headers.cust_id = customers.cust_id
and date_format(order_date, '%Y-%m') in (PrevMonth( current_date(), 3)))
and exists
(select *
from a_bkorders.order_headers
where order_headers.cust_id = customers.cust_id
and date_format(order_date, '%Y-%m') in (PrevMonth( current_date(), 4)))
and exists
(select *
from a_bkorders.order_headers
where order_headers.cust_id = customers.cust_id
and date_format(order_date, '%Y-%m') in (PrevMonth( current_date(), 2)));
And I get :
VOILA!!!
EXISTS is when you need to match the results of query with another
subquery. Query#1 results need to be retrieved where SubQuery results
match. Kind of a Join.
E.g. select customers who have placed orders in month1 and say month2 too,
Whereas IN is used to retrieve if the value of a specific column
lies in a list (1,2,3,4,5). In this particular case, it would output
all the customers who have a record in at least one of the previous
months not necessarily ALL.
I have a list of ID’s Shown below.
AT0920130004
AT0920130005
AT0920130006
AT0920130007
AT0920130008
AT0920130009
AT0920130010
AT1020130001
AT1020130002
AT1020130003
AT1020130004
AT1020130005
AT1120130003
AT1120130004
AT1120130005
AT1120130006
Here an example record has the format ATmmyyyyxxxx.
where
AT represents location,
mm represents month eg 10 would be October
yyyy represents year eg. 2013
xxxx represents the increasing seed of numbers.
Now I need to select an ID which is generated in the end of the month.
For ex: last id of September i.e. AT0920130010.
Any suggestions are appreciated.
You can use this query to get max ID for each month and year
select substring([id], 3, 6) MonthYear, max([id]) MaxID
from yourtable
group by substring([id], 3, 6)
To get max ID for one specific month, you can use this query:
select max([id])
from yourtable
where cast(substring([id], 5, 4) as int) = 2013 -- year
and cast(substring([id], 3, 2) as int) = 9 -- month
try this :
select MAX(t.ID) from TableName t
group by SUBSTRING(t.ID, 3, 2)
You basically want to get the max id that matches a location, a month and a year. Something like this should do it:
SELECT max(id)
FROM myTable
WHERE substring(id, 0, 2) = [location]
AND susbstring(id, 2, 2) = [month]
AND substring(id, 4, 4) = [year]
try this(this will return whole field, and just id):
SELECT YOUR_FIELD, MAX(RIGHT(YOUR_FIELD,4)) AS just_id FROM YOUR_TABLE WHERE RIGHT(YOUR_FIELD,LENGTH(YOUR_FIELD)-2) LIKE '09%'
I have a Mysql table with the following fields,
bill_date,bill_no,item,tax,total.
i want to generate a report containing the fields
bill_date,Bill_no,Taxable Item(Count),Nontaxable Item(Count) between to dates.
I try the query like this
select Bill_no,bill_date,count(tax=0),count(tax>0) from bill group by bill_no. The query return wrong values. Please help me.
SELECT bill_no, bill_date,
SUM(IF(tax=0, 1, 0)) AS nonTaxableItems,
SUM(IF(tax>0, 1, 0)) AS taxableItems,
FROM bill
WHERE bill_date BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY bill_no
if you have the number of items stored in a column named count then the query becomes:
SELECT bill_no, bill_date,
SUM(IF(tax=0, count, 0)) AS nonTaxableItems,
SUM(IF(tax>0, count, 0)) AS taxableItems,
FROM bill
WHERE bill_date BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY bill_no