SSRS - Trend Indicator regarding the last month - sql-server

I've the following dataset:
System_Name Description Month Year Value_used YTD
APP01 PC A 4 2016 93 -
APP01 PC A 5 2016 90 1
APP01 PC A 6 2016 97 1
APP01 PC A 7 2016 82 -1
APP01 PC A 8 2016 99 1
CCD12 PC B 4 2016 81 -
CCD12 PC B 5 2016 83 1
CCD12 PC B 6 2016 83 0
CCD12 PC B 7 2016 87 1
CCD12 PC B 8 2016 88 1
This dataset is result from the following query:
SELECT *,
CASE WHEN Collect_Usage > PreviousQuota THEN 1
WHEN Collect_Usage = PreviousQuota THEN 0
ELSE -1
END AS [Flag Indicator]
FROM (
SELECT System_Name,
[Description],
[Monh],
[Year],
LAG(Value_used, 1,0) OVER (ORDER BY System_Name, [Description],[Year],[Monh]) AS PreviousQuota
FROM [CAPM_Reporting_Analytics].[dbo].[Capacity_Data_Reds]
) TB1
I am creating the YTD to use in my matrix on SSRS that is creating the pivot to show the evolution from month.
My problem is: Imagine that I want to create the report from Month 8. I want to add a indicator that shows
- If the last month (only consider the month 8 in this case) is above the previous month then Green
- If the previous month is null then Grey
- Else Red.
I want to get anything like this:
Where the trend is only valid between the value of the last and penultimate column (last and penultimate)
How can I do this?
Many thanks!!

Related

SQL query to calculate summation of row with previous row along with missing rows

This is my table
Month Year count of subscription per date
---- ---- ----------------------------
1 2010 10
2 2010 30
4 2010 20
7 2010 40
2 2011 60
Please need your help, I need to get the result like below table, summation second row with first row, same thing for another rows: Here some months are missed in above table got added with 0 as count of subscription column
Month Year count of subscription per date sum of subscription
---- --- ---------------------------- ---------------------
1 2010 10 10
2 2010 30 40
3 2010 0 40
4 2010 20 60
5 2010 0 60
6 2010 0 60
7 2010 40 100
8 2010 0 100
9 2010 0 100
10 2010 0 100
11 2010 0 100
12 2010 0 100
1 2011 0 100
2 2011 60 160
3 2011 0 160
and so on till
12 2011 0 160
You can try this way
create table #temp
(
[Month] int,
[year] int,
[count of subscription per date] int,
[sum of] int
)
declare #id int,#sumall int
set #id=1
set #sumall=0
while(#id<=12)
begin
if exists(select month from [your table] where month=#id)
begin
set #sumall=#sumall+(select count from [your table] where month=#id)
insert into #temp
select month,year,count,#sumall as summ from [your table] where month=#id
end
else
begin
insert into #temp
select #id,'2010',0,#sumall
end
set #id=#id+1
end
select * from #temp

Data year wise sql

I try to get data year wise. I tried this query
select
year(SStartDate) as joinedyear ,
count(*) joined_total
from
Detail
where
client = 81
Group By
YEAR(StartDate)
union all
select
year(SEndDate) as leftyear,
count(*) left_total
from
Detail
where
client = 81
Group By
YEAR(SEndDate)
This shows correct data but this shows data like this
1900 12
2001 1
2012 3
2013 3
2016 45
1900 23
2002 34
2004 34
2015 1
2016 56
where as I want data like this
joinedyear joined_total leftyear left_total
1900 12 1900 45
2001 1 2002 34
2012 3 2004 34
2013 3 2015 1
2016 45 2016 56
Try This below query..it mat help you
select * from (
select year(SStartDate) as joinedyear ,
count(*) joined_total from Detail
where client=81
Group By YEAR(StartDate)
) as a
full outer join
(
select year(SEndDate) as leftyear , count(*) left_total from Detail
where client=81
Group By YEAR(SEndDate)
) as b
on a.joinedyear=b.leftyear

ROW_NUMBER on a specific query

I have the below data, and I performed a ROW_NUMBER(partition by ID order by YEAR) function based on year which's ranking my data as below.
I want to bring in name for every id based on their latest year. I want to bring in NULL data if that's the only data available and bring in latest NON NULL data for every other record. But rownumber only lets me bring in recent name which could be NULL. How do I query below data to bring in most recent NON NULL name?
ID year name rownum
10 2011 abc 1
10 2010 abc 2
11 2011 ghi 1
11 2010 ghi 1
13 2010 NULL 1
13 2009 jkl 2
14 2014 NULL 1
14 2014 mno 2
15 2015 NULL 1
I want to bring in names jkl, mno for ID's 13 and 14 and not NULLS in my final result. Any suggestion on how to achieve that?
The output I desire is below - I want to display data for ROW NUM=1
10 2011 abc
11 2011 ghi
13 2009 jkl
14 2014 mno
15 2015 NULL
Sort non-null rows ahead of null rows:
select ID, year, name
from (select *,
row_number() over (partition by ID
order by case when name is null then 1 else 0 end, year desc) as RN
from #t) _
where rn = 1
See also SQL Server equivalent to Oracle's NULLS FIRST?, SQL Server ORDER BY date and nulls last &

SQL query sorted by year, counting values per year

I have this SQL query, which gives me the numbers of orders within a date range, and that is OK:
SELECT CardName, ItemNumber, COUNT(*) AS antal, year(date) as aarstal, SUM(Numbers) AS total, sum(case when year(date)=2012 then 1 else 0 end) as newtest
FROM tblOrders
WHERE (Publisher = 3) AND (Date > '01-01-2012 00:00:00') AND (Date < '30-09-2014 23:59:59') AND (Status = 3) and ItemNumber!=9130 and ItemNumber!=9180 and ItemNumber!=9170 and ItemNumber!=9190
GROUP BY ItemNumber, CardName, year(date)
This gives me ie
CompanyA 0 76 2012 10900 76
CompanyA 0 42 2013 6300 0
CompanyB 0 1 2012 100 1
CompanyB 0 7 2013 1100 0
CompanyC 0 1 2014 300 0
CompanyD 0 7 2014 700 0
CompanyE 0 2 2012 300 2
CompanyE 0 1 2013 200 0
From this, I can see that CompanyA bought 10900 units in 2012, from a total of 76 orders that year, and in 2013, they bought 6300 units. For sales reasons, we would like (or need) this output as
CompanyA 0 76 2012 10900 42 2013 6300
CompanyB 0 1 2012 100 7 2013 1100
CompanyC 0 1 2014 300 0 2013 0
CompanyD 0 7 2014 700 0 2013 0
CompanyE 0 2 2012 300 1 2013 200
So you could see if a customer is going up or down in sales. I've tried various things, but I don't know if there is any possibility to have the Numbers column SUM'ed up per year in a query like this, any suggestions?
PS: The
sum(case when year(date)=2012 then 1 else 0 end) as newtest
was just for a test, I tried something like SUM(Numbers case...) but that was not accepted!
EDIT: I just made a demo in SQL Fiddle, just to give a better understanding of my problem:
http://sqlfiddle.com/#!3/072a1/5
In there, you can see that my customer A has bought a total of 300 in 2012 (on 2 orders), 700 in 2013 (on 2 orders), and finally 200 in 2014 (on 1 order). The pivot examples I've found only counts the number of orders, and not the value of my Numbers - could somebody edit the SQL Fiddle, making that one work, then I'm sure I could learn and adapt the solution into my "real" database?
/Tommy

PIVOT Table Date & Year wise data display sql server 2005

this is my query
SELECT *
FROM (SELECT CurDate, YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main where year(CurDate)>=2009 and year(CurDate)<=2011) AS D
PIVOT(SUM(Warranty_Info) FOR orderyear IN([2009],[2010],[2011])) AS P
the above query return data but CurDate return date it is is return multiple date for same month.
i want that SUM(Warranty_Info) should return only once for every month and year
output should look like
Month 2009 2010 2011 2012 2013
----- ---- ---- ---- ---- -----
1 10 0 11 32 98
2 20 10 21 11 44
3 0 224 33 77 31
some kind of problem is there in my query and that is why it is returning multiple data for same month like
please help me to have the right query. thanks
Instead of using CurDate as the full date with year, month and day, I would suggest using the Month() function to return the numeric value of each month. This will allow you to group by month instead of the full date:
SELECT dateMonth, [2009],[2010],[2011]
FROM
(
SELECT month(CurDate) dateMonth,
YEAR(CurDate) AS orderyear, Warranty_Info
FROM eod_main
where year(CurDate)>=2009
and year(CurDate)<=2011
) AS D
PIVOT
(
SUM(Warranty_Info)
FOR orderyear IN([2009],[2010],[2011])
) AS P;
See SQL Fiddle with Demo

Resources