SQL Server : pivot date to month quarter and year subtotal format - sql-server

I have a table that contains datefield and product. I want the below pivot query to show the month total, quarter total and year total.
Year Month A B C
-----------------
2014 Jan
2014 Feb
2014 Mar
2014 Q1
2014 Apr
2014 May
2014 Jun
2014 Q2
2014 Jul
2014 Aug
2014 Sep
2014 Q3
2014 Oct
2014 Nov
2014 Dec
2014 Q4
2014 Total
2015 Jan
2015 Feb
2015 Mar
2015 Q1

Refer this Simple-Way-To-Use-Pivot-In-SQL-Query from CodeProject, the code given in the example is more similar to your requirement.

I think this is what you are talking about.
SELECT 'Count' AS Month,
[Jan], [Feb]
FROM
(SELECT year, month
FROM Product) AS SourceTable
PIVOT
(
count(year)
FOR Month IN ([Jan],[Feb])
) AS PivotTable;
Out Put
Month Jan Feb
Count 2 2
This is what you are expecting.
Count Month quarter Year
Count 24 8 2
But We cannot categorise month, year unless they are designated as months, quarter etc. If there is any flag which denotes that Jan and Feb are months then we can categorise them based on Months.

Related

MonthName-Year as SSRS parameter

I have dimTime table below:
TheYear Fiscalyear TheMonthName TheMonth
2022 2022 - 2023 November 11
2022 2022 - 2023 October 10
2022 2022 - 2023 September 9
2023 2022 - 2023 February 2
2023 2022 - 2023 January 1
How to implement the MonthName and Year as SSRS parameter and have this in the main dataset? Like below:
Select Month-Year: January-2023
Dataset: Select
from Table
where date = selected MonthYear from the parameter
This will also goes to same report but the selection is Fiscal year:
*Select Fiscal Year: 2022-2023
Dataset: Select
from Table
where date = selected FiscalYear from the parameter
Im trying to convert it to date but dont know where to start.

How to show the history records up to financial year end day. POWER BI

I have a data set like this
Year
Records
2020
1
2020
2
2021
3
2021
4
2022
5
2022
6
I want show all the history records up to the selected year in Slicer:
2020
2021
2022
For example, if I select 2020, it should show:
Year
Records
2020
1
2020
2
And if I select 2021, it should show:
Year
Records
2020
1
2020
2
2021
3
2021
4
If I select 2022, it should show:
Year
Records
2020
1
2020
2
2021
3
2021
4
2022
5
2022
6
I am very appreciated if you have some ideas for me.
Regards
Use a "less than or equal to" Numeric Range Slicer on the Year

SQL SERVER get datetime from a custom formatted string

I have a table that contains some DateTime values stored as text, and I want to loop through them and get the DateTime object for each one.
Here are some examples of the data that I need to convert it.
26 Tuesday November 2019 - 09:00 AM
25 Monday November 2019 - 11:20 AM
06 Friday December 2019 - 10:00 AM
22 Sunday December 2019 - 01:05 PM
22 Sunday December 2019 - 04:00 PM
22 Sunday December 2019 - 02:30 PM
23 Monday December 2019 - 11:00 AM
I tried to use the SQL built-in functions (e.g. cast, convert,...) but didn't work.
Remove the name of the weekday and the hyphen and it should work:
WITH cte
AS
(
SELECT DATENAME(weekday,datefromparts(2020,1,dow.dow)) as WeekDay
FROM (values (1),(2),(3),(4),(5),(6),(7) ) dow(dow)
)
SELECT TRY_CAST(REPLACE(REPLACE(t.dateInTxt,'-',''),cte.WeekDay,'') as datetime),t.dateInTxt
FROM mytable t
JOIN cte
ON t.dateInTxt LIKE '%'+cte.WeekDay +'%'

How to generate automatic Rows

I have Below Sample Table
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
I am executing following query
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
I need following output
month year budget_Amt Actual_Amt
feb 2017 25 30
mar 2016 10 5
apr 2016 50 15
QuarterYTD 2016 85 50
4th record will generate automatically when I execute query
Based on the data given, you could UNION in grouping like the following. However, I assume that your data is more complex and some additional code would need to be added to the query.
select month,year,budget_amt,Actual_Amt from Table where month in('Feb','Mar','apr')
UNION ALL
select 'QuarterYTD', Year, sum(budget_amt), sum(Actual_Amt) from Table where month in ('Feb','Mar','apr') group by Year

Capture date range of different dateYear , DateMonth columns

I have a table that captures monthly data using a dateYear and dateMonth to record date. I;m having trouble capturing data between two different years.
For example : between dateYear 2015 and dateMonth 09 through dateMonth 02 and dateYear 2016
In english , between September 2015 and Feb 2016
whats the best way to go about this.. Ive tried grouping through the where clause but the months overlap..
is there a date function or concatenation method i should use?>
SELECT
prodDays
,hours
FROM Table
WHERE 1=1
AND date_year BETWEEN 2015 AND 2016
Sounds like you are looking for this where clause:
where (date_year = 2015 and date_month >= 9) or (date_year = 2016 and date_month <= 2)

Resources