How to generate automatic Rows - sql-server

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

Related

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)

SQL Query-Multiple date ranges

I want to fetch some data from DB by giving multiple date ranges. Example,in February I want to get weekly report from a table in this order Feb 01 to 07, Feb 07 to 14, Feb 14 to 21, Feb 21 to 28 and Feb 28 to Mar 01. In DB the records are stored in a daily wise not in weekly wise. I want to cluster it as weekly wise and calculate sum then show the result. Please help me if you know this case.
For clear cut view, consider 3 tables & its columns.
Table A:id,timestamp (comment-data is inserted daily)
Table B:id,fruits
Table C:id,fruits_type
Result:
fruits_type count(id) timestamp
apple 3 01-02-2016 to 07-02-2016
orange 5 01-02-2016 to 07-02-2016
pineapple 8 01-02-2016 to 07-02-2016
apple 4 07-02-2016 to 14-02-2016
orange 5 07-02-2016 to 14-02-2016
Conditions:id should match among 3 tables;fetch data by providing group by fruits_type and timestamp should be in weekly wise.
Please help if you know this
To get the sum of all values between two dates you would do it like this:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN '2/1/2016' AND Date1 <'2/7/2016'
If you want to make it more flexible and have the query get the last week's sum you can use the DATEADD function to lag by one week:
SELECT SUM(Column1)
FROM Table1
WHERE Date1 BETWEEN DATEADD(week, -1, GETDATE()) AND Date1 < GETDATE()
If you want the result set to include a row for each week, you can use UNION to merge the queries.

sum up every 12 months in a table

i have a table calculating the installments. in that table i'm saving all the data recording to that. For example if i'm calculating for 60 installments and saving all the data,so it is like 60 months. so now i need to sum up the value of one column for every 12 months. sometimes v start paying the installments from the middle of the year also.
my DB looks like this.the highlighted column must sum up for every 12 months. two images are one table only
suppose i have 30 installments from starting on jun 2012.suppose i started paying installment from jun 2012 then should sum up the installments from jun 2012 to may 2013. v can't use group by year. i must sum up like this ................................................................................‌​
sum jun 2012 to may 2013
sum jun 2013 to may 2014
sum jun 2014 to nov 2014 ( only 6 months left)
You can use ROW_NUMBER to generate a group of 12 months:
WITH Cte AS(
SELECT *,
RN = (ROW_NUMBER() OVER(ORDER BY InstallmentMonth) - 1)/ 12
FROM your_table
)
SELECT
SUM(InteresetPerInstallment)
FROM Cte
GROUP BY RN

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

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.

Sql Query for an ssrs report

I have the following report I need to generate . I am new to SSRS.
Number of Stores with June July Aug
0 sales 12 34 32
1 to 9 Sales 12 34 45
10+ sales 15 45 54
The tables I have are as follows :
Store :
YEAR MONTH StoreName
2013 10 ABC
2013 10 DEF
2013 09 JKL
2013 06 FGH
Store Sales : (Contains only stores whose sales are > 0)
YEAR MONTH StoreName NumberOfSales
2013 10 ABC 3
2013 09 JKH 14
2013 10 FRH 9
I am not really sure what I need to do to get the report in the above format ?
I can write a query for sales in a single month , but how do I write a query to
get the report for all 3 months ? Or is there a better way to do these reports using ssrs ?
If you can direct me on what to do it will be great ?
You want to set up your query like this:
SELECT Year, Month, (CASE
WHEN NumberOfSales = 0 THEN '0 Sales'
WHEN NumberOfSales BETWEEN 1 AND 9 THEN '1-9 Sales'
WHEN NumberOfSales > 9 THEN '10+ Sales' END) [SaleGroup]
FROM FactStoreSales
Then you basically want to go into SSRS and just throw it into a table with the month in the columns and the group in the rows and let it pivot the data itself rather than trying to do that in SQL.

Resources