I do have SSRS matrix as below, and it will include for column group months for current year, for row group it does have current and future months (in my case it's for 2 years):
current with future months
current year months
current with future months
Sum(Values)
I want to add total to sum Values per year and show right after December month. Example in below table. Idk is this possible to do in SSRS
Output :
current with future months
Jan 2022
Feb 2022
Mar 2022
and till..
Dec 2022
Jan 2022
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Feb 2022
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Mar 2022
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
... till
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Dec 2022
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
2022 Total
Total Jan
Total Feb
Total March
Total...
Total Dec
Jan 2023
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Feb 2023
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Mar 2023
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
... till
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Dec 2023
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
Sum(Values)
2023 Total
Total Jan
Total Feb
Total March
Total...
Total Dec
Basically I want to show Totals for 2 years separately just after December for each year.
Note: I was able to do/get total for each year in the end of table, but I need after each December.
Related
I'm trying to fix the order by and group by year and month format. This is my sql query
SELECT DATE_FORMAT(created_at, "%Y %b") ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, "%Y %b")
ORDER BY DATE_FORMAT(created_at, "%Y %b")
But the sql query result is this:
2018 Apr
2018 Aug
2018 Dec
2018 Feb
2018 Jul
2018 Jun
2018 Mar
2018 May
2018 Nov
2018 Oct
2018 Sep
2019 Apr
2019 Aug
2019 Dec
It seems that only the year format has been ordered but not the months in it. How can I alter my query to order by year and then months?
You can order by the minimum date in each range:
SELECT DATE_FORMAT(created_at, '%Y %b') ym
FROM `book_summaries`
GROUP BY DATE_FORMAT(created_at, '%Y %b')
ORDER BY MIN(created_at);
Note that I also replaced the double quotes with single quotes. Single quotes are the SQL standard for strings (although some database do support double quotes as a string delimiter).
I have a payments table, Where I update the subscription renewal date and amount, now I have a query to select previous 13 months sum of amounts column, But how do I add zero if no payment data is available for any month in the selected last 13 months of record.
SELECT DATE_FORMAT(dtSubscriptionRenewalDate, "%b - %Y") AS month, SUM(intPaymentAmount) as usd13mon FROM `tbl_pi_payment` WHERE strCurrencyCode = 'USD' and dtSubscriptionRenewalDate <= NOW() and dtSubscriptionRenewalDate >= Date_add(Now(),interval - 13 month) GROUP BY DATE_FORMAT(dtSubscriptionRenewalDate, "%Y-%m")
month usd13mon
Oct - 2018 55
Dec - 2018 79
Jan - 2019 16
Feb - 2019 93
Mar - 2019 80
Apr - 2019 83
May - 2019 34
Jun - 2019 23
Jul - 2019 25
Aug - 2019 37
Sep - 2019 17
Oct - 2019 44
In the above-mentioned output the nov 2018 month is missing as there was data available in the table.
You may try introducing a calendar table which represents every month which you want to appear in your report:
SELECT
m.my,
COALESCE(SUM(t.intPaymentAmount), 0) AS usd13mon
FROM
(
SELECT 'Nov - 2018' AS my UNION ALL
SELECT 'Dec - 2018' UNION ALL
SELECT 'Jan - 2019' UNION ALL
...
SELECT 'Nov - 2019'
) m
LEFT JOIN tbl_pi_payment t
ON m.my = DATE_FORMAT(t.dtSubscriptionRenewalDate, '%b - %Y') AND
t.strCurrencyCode = 'USD' AND
t.dtSubscriptionRenewalDate BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL - 13 MONTH)
GROUP BY
m.my;
I made reports with filter. The options are this week, this month and lifetime. My problem is it also picked up the next day. For example, the date today is Sep 04. If I select this week the output will be this Sep 02 Mon, Sep 03 Tue, Sep 04 Wed, Sep 05 Thu, Sep 06 Fri, Sep 07 Sat, Sep 08 Sun
I want this to happen...
Sep 02 Mon
Sep 03 Tue
Sep 04 Wed
Same with this month and lifetime...
$sql="SELECT DATE_FORMAT(tbl_date.date, '%b %d') AS Date_Request,
COUNT(tbl_request_order_details.request_order_id) AS Total_Order,
tbl_supplier.supplier_name AS Supplier FROM (tbl_date LEFT JOIN
tbl_request_order ON tbl_request_order.date_request = tbl_date.date AND
tbl_request_order.barangay_id = $_SESSION[barangay_id]) LEFT JOIN
tbl_request_order_details ON tbl_request_order.request_order_id =
tbl_request_order_details.request_order_id LEFT JOIN tbl_supplier_medicine
ON tbl_supplier_medicine.supplier_medicine_id =
tbl_request_order_details.supplier_medicine_id LEFT JOIN tbl_supplier ON
tbl_supplier.supplier_id = tbl_supplier_medicine.supplier_id LEFT JOIN
tbl_barangay ON tbl_barangay.barangay_id = tbl_request_order.barangay_id
WHERE
(";
switch($search){
case 'lifetime':
$sql .= "(tbl_date.date between DATE_FORMAT(NOW()
,'%M-%d-01') AND NOW())) GROUP BY tbl_date.date
ORDER BY tbl_date.date;";
break;
case 'this_week':
$sql .= "YEAR(tbl_date.date) = YEAR(NOW())
AND WEEKOFYEAR(tbl_date.date) = WEEKOFYEAR(NOW()))
GROUP BY tbl_date.date
ORDER BY tbl_date.date;";
break;
case 'this_month':
$sql .= "YEAR(tbl_date.date) = YEAR(NOW())
AND MONTH(tbl_date.date) = MONTH(NOW()))
GROUP BY tbl_date.date
ORDER BY tbl_date.date;";
break;
}
")";
I'm trying to select data between july 2017 to jun 2018 using sql query but not getting expected output. Let's say m having data from jan 2017 up to dec 2018,
and i want to select data between july 2017 to jun 2018. how can I achieve this?
This is the data i'm having
And Expected Output is
See MySQL STR_TO_DATE() Function
SELECT * FROM <YOUR TABLE> WHERE STR_TO_DATE(CONCAT('1 ',`Month`,' ',`Year`), '%d %M %Y')
BETWEEN STR_TO_DATE('1 Jun 2017', '%d %b %Y') AND STR_TO_DATE('31 Jul 2017', '%d %b %Y');
I have a table with two Date columns. DATE1 is sometimes NULL and sometimes contains duplicate values. DATE2 is always populated and unique. My table is sorted by latest DATE2 date.
I'd like to create a new date column where DATE1 will be selected unless its value is duplicated from the next row or it's NULL. In this case, I want to take the value of DATE2. I also need two boolean columns that tell me when either of those conditions were met. Let me demonstrate using an example so it's clearer.
In the table below, row 5 and 6 have a value of Jul 27, 2011 so I'd like to set the new date column of row 5 to Aug 4, 2011 (which is DATE2). In row 3, the value of DATE1 is NULL so I want to take the value of DATE2.
I've tried a few inner select statements but can't get this to work. Any ideas?
My table as it currently stands in the database:
Row DATE1 DATE2
--------------------------------------
1 Oct 10, 2011 Nov 13, 2011
2 Oct 10, 2011 Oct 10, 2011
3 NULL Oct 8, 2011
4 Aug 12, 2011 Aug 12, 2011
5 Jul 27, 2011 Aug 4, 2011
6 Jul 27, 2011 Jul 28, 2011
7 Jul 1, 2011 Jul 26, 2011
8 May 24, 2011 Jun 13, 2011
What I expect the final result to look like:
Row FINAL_DATE DATE1_DUPLICATE DATE1_WAS_NULL
----------------------------------------------------------
1 Nov 13, 2011 TRUE FALSE
2 Oct 10, 2011 FALSE FALSE
3 Oct 8, 2011 FALSE TRUE
4 Aug 12, 2011 FALSE FALSE
5 Aug 4, 2011 TRUE FALSE
6 Jul 27, 2011 FALSE FALSE
7 Jul 1, 2011 FALSE FALSE
8 May 24, 2011 FALSE FALSE
Thanks so much!
This can be handled with sequential scanning of the table and using MySQL variables. You can test in (updated) SQL-fiddle:
SELECT date2
, dd
, DATE_FORMAT(dd, '%b %e, %Y') AS final_date
, date1_duplicate
, date1_was_null
FROM
( SELECT date2
, COALESCE( (date1 = #d OR date1 = #prev), FALSE)
AS date1_duplicate
, (date1 IS NULL) AS date1_was_null
, #d := CASE WHEN (date1 = #d OR date1 = #prev)
THEN date2
ELSE COALESCE(date1, date2)
END AS dd
, #prev := date1 AS pre
FROM tableX AS t
CROSS JOIN
( SELECT #d := DATE('1000-01-01')
, #prev := #d
) AS dummy
ORDER BY date2 ASC
) AS tmp
ORDER BY date2 DESC
;