How to Roll up recursive value in ssrs - reporting-services

I have a table with value for example
ID Name ParentID Value Date
1 ClassA 0 NULL NULL
2 ClassB 0 NULL NULL
7 Jason 1 50 2016-06-01
8 Jason 1 20 2016-06-02
9 Jason 1 30 2016-06-03
10 Kelly 1 20 2016-06-02
11 Kelly 1 30 2016-06-03
and i want to generate a report like this
2016-06-01 2016-06-02 2016-06-03
ClassA 50 40 60
Jason 50 20 30
Kelly 0 20 30
ClassB 0 0 0
as you can see, the classA has been rolled up and total up all the children
i tried few code, it either show me empty or show me recursive value where all the rows is the same value.
for example
=Sum(fields!QTY.Value)
it shows
Result1
Then second code i tried is
=IIF(ISNOTHING(Sum(Fields!QTY.Value, "Name", recursive)),0, Sum(Fields!QTY.Value, "Name", recursive))
, it shows
Recursive result
Anyone can help on this?..

Related

MYSQL : Grouping rows by dates and making fields in to column names

Have have table like this
id date user call_count
1 2020-09-15 Tim 4
2 2020-09-14 Tim 6
3 2020-09-04 Jamie 1
4 2020-09-02 Tim 2
5 2020-09-07 Tim 5
6 2020-09-01 Jamie 1
7 2020-09-01 Tim 5
8 2020-09-10 Jamie 4
9 2020-09-12 Tim 5
10 2020-09-22 Tim 44
11 2020-09-22 Tony 32
I'd like to have a single query that groups by date, putting each user as the name of a column and displaying their count for that date. Right now I am accomplishing this by a few loops of selects, but it is ugly and not efficient.
Result should look like
Date Tim Tony Jamie
2020-09-22 44 32
2020-09-15 4
2020-09-14 6
2020-09-12 5
2020-09-10 4
2020-09-07 5
2020-09-04 1
2020-09-02 2
2020-09-01 5 1
I've searched quite a bit for something like this, but not finding much. Thank you
Use conditional aggregation:
SELECT
Date,
SUM(CASE WHEN user = 'Tim' THEN call_count END) AS Tim,
SUM(CASE WHEN user = 'Tony' THEN call_count END) AS Tony,
SUM(CASE WHEN user = 'Jamie' THEN call_count END) AS Jamie
FROM yourTable
GROUP BY
Date
ORDER BY
Date;

mySQL to count by date and hour giving results as a date headed table

Query I have now:
SELECT DATE(requestdate), HOUR(requestdate), count(*)
FROM timeseries
GROUP BY DATE(requestdate), HOUR(requestdate)
ORDER BY requestdate
Returns eg:
2018-10-12 0 7655
2018-10-12 1 876
2018-10-12 2 1344
...
2018-10-12 23 1009
...
2018-10-13 0 765
2018-10-13 1 9879
2018-10-13 2 2132
...
2018-10-13 23 876
... more days
Desired result eg:
2018-10-12 2018-10-13 ...
0 7655 765
1 876 9879
2 1344 2132
...
23 1009 876
How would I do this? (don't need a particularly performant solution)

Calculate the percentage of success of each measurement values in SQL

I have 3 database tables with sample data given below
Meas_id - integer(Foreign keyed to Measurement.meas_id)
Tool_id - integer(Foreign keyed to Events.Machine_id)
Processdate- Timestamp with timezone (UTC)
CreatedDate- Timestamp with timezone (UTC)
Readings
Meas_id Tool_id Status Processdate
1 13 Completed 2016-01-01 01:34:11
1 28 Failed 2016-01-01 08:37:11
1 54 Failed 2016-01-02 16:04:12
1 32 Completed 2016-01-04 07:13:11
1 39 Completed 2016-01-04 14:14:14
1 12 Completed 2016-01-05 22:10:09
1 9 Completed 2015-12-28 13:11:07
1 17 Completed 2016-01-25 13:14:11
1 27 Completed 2016-01-15 14:15:16
1 31 Failed 2016-01-07 16:08:04
2 113 Completed 2016-01-01 01:34:11
2 128 Failed 2016-01-01 08:37:11
2 154 Failed 2016-01-02 16:04:12
2 132 Completed 2016-01-04 07:13:11
2 139 Completed 2016-01-04 14:14:14
2 112 Completed 2016-01-05 22:10:09
2 90 Completed 2015-12-28 13:11:07
2 117 Completed 2016-01-25 13:14:11
2 127 Completed 2016-01-15 14:15:16
2 131 Failed 2016-01-07 16:08:04
Events
Meas_id Machine_id Event_Name CreatedDate
1 13 Success 2015-12-27 01:34:11
1 17 Error 2015-12-27 08:37:11
1 28 Success 2015-12-27 16:04:12
1 9 Success 2015-12-28 07:13:11
1 54 Success 2015-12-28 14:14:14
1 31 Error 2015-12-28 22:10:09
1 32 Success 2015-12-29 13:11:07
1 39 Success 2015-12-29 13:14:11
1 12 Success 2015-12-31 14:15:16
1 27 Success 2016-01-01 16:08:04
2 113 Success 2015-12-27 01:34:11
2 117 Error 2015-12-27 08:37:11
2 128 Success 2015-12-27 16:04:12
2 90 Success 2015-12-28 07:13:11
2 154 Success 2015-12-28 14:14:14
2 131 Error 2015-12-28 22:10:09
2 132 Success 2015-12-29 13:11:07
2 139 Success 2015-12-29 13:14:11
2 112 Success 2015-12-31 14:15:16
2 127 Success 2016-01-01 16:08:04
Mesurement
Meas_id Meas_name
1 Length
2 Breadth
For each measurement ‘length’ and ‘breadth’ and each day of the week, I am trying to calculate the percentage of success in the first week of 2016 for all completed measurements of tools/machines within 168 hours of thier creation date.
My Desired Output is
Measurement DayofTheWeek PercentageSuccess
Length 1 50
Length 2 0
Length 3 0
Length 4 100
Length 5 100
Length 6 0
Length 7 0
Breadth 1 50
Breadth 2 0
Breadth 3 0
Breadth 4 100
Breadth 5 100
Breadth 6 0
Breadth 7 0
I tried doing it this way but certainly missing some logic and its not working.
Select m.Meas_name,
datepart(dd, Processdate) as DayofTheWeek,
(Count(m.Meas_name)* 100 / (Select Count(Event_Name) From Events where Event_Name = 'Success')) as PercentageSuccess
FROM Readings r JOIN
Measurements m
ON r.Meas_id = m.Meas_id
JOIN Events e
ON e.Meas_id = m.Meas_id
WHERE m.Meas_name IN ('Length', 'Breadth') AND
r.Status = 'Completed' AND
e.CreatedDate >= DATEADD(hh, -168, GETDATE())
GROUP BY m.Meas_name, datepart(dd, Processdate);
Kindly provide inputs on an optimized way of achieving it.
Nice I got downvoted for a correct answer probably because my answer wasn't very clear it is kind of hard to explain so here is an edit aimed at your comment and the downvoter (whom I think was just retaliating).
Anyway, Your joining of 3 tables while valid replicates the data in your events table. Due to that the way you are counting the records will always be exaggerated and incorrect. your calculation for percentage is also happens to be backwards.
On the join it looks like you are just missing the use of the Tool_id in your join. You could try something like the following:
SELECT
m.Meas_name
,DAYOFWEEK(r.ProcessDate) as DayOfTheWeek
,(COUNT(CASE WHEN e.Event_Name = 'Success' tHEN e.Meas_id END)/(COUNT(e.Meas_id) * 1.0)) * 100 as PercentageSuccess
FROM
Measurements m
INNER JOIN Events e
ON m.Meas_id = e.Meas_id
INNER JOIN Readings r
ON e.Meas_id = r.Meas_id
AND e.Machine_id = r.Tool_id
AND r.Status = 'Completed'
AND r.ProcessDate BETWEEN '2016-01-01' AND '2016-01-07'
WHERE
m.Meas_name IN ('Length','Breadth')
GROUP BY
m.Meas_name
,DAYOFWEEK(r.ProcessDate)
Note this is written for mysql because that is what is tagged in you post. if you actually want sql-server as your syntax suggests let me know. Also, I am guessing that you a really want to filter by processdate but if you want to filter by Event.CreateDate then put that in the ON condition of the Events join.

Join Tables Based on Time and ID

I have two tables of time series data that I am trying to query and don't know how to properly do it.
The first table is time series data of device measurements. Each device is associated with a source and the data contains an hourly measurement. In this example there are 5 devices (101-105) with data for 5 days (June 1-5).
device_id date_time source_id meas
101 2016-06-01 00:00 ABC 105
101 2016-06-01 01:00 ABC 102
101 2016-06-01 02:00 ABC 103
...
101 2016-06-05 23:00 ABC 107
102 2016-06-01 00:00 XYZ 102
...
105 2016-06-05 23:00 XYZ 104
The second table is time series data of source measurements. Each source has three hourly measurements (meas_1, meas_2 and meas_3).
source_id date_time meas_1 meas_2 meas_3
ABC 2016-06-01 00:00 100 101 102
ABC 2016-06-01 01:00 99 100 105
ABC 2016-06-01 02:00 104 108 109
...
ABC 2016-06-05 23:00 102 109 102
XYZ 2016-06-01 00:00 105 106 103
...
XYZ 2016-06-05 23:00 103 105 101
I am looking for a query to get the data for a specified date range that grabs the device's measurements and its associated source's measurements. This example is the result for querying for device 101 from June 2-4.
device_id date_time d.meas s.meas_1 s.meas_2 s.meas_3
101 2016-06-02 00:00 105 100 101 102
101 2016-06-02 01:00 102 99 100 105
101 2016-06-02 02:00 103 104 108 109
...
101 2016-06-04 23:00 107 102 109 102
The actual data set could get large with lets say 100,000 devices and 90 days of hourly measurements. So any help on properly indexing the tables would be appreciated. I'm using MySQL.
UPDATE - Solved
Here's the query I used:
SELECT d.device_id, d.date_time, d.meas, s.meas_1, s.meas_2, s.meas_3
FROM devices AS d
JOIN sources AS s
ON d.source_id = s.source_id AND d.date_time = s.date_time AND d.device_id = '101' AND d.date_time >= '2016-06-02 00:00' AND d.date_time <= '2016-06-04 23:00'
ORDER BY d.date_time;
For what its worth, it also worked with the filters in a WHERE clause rather than in the JOIN but it was slower performing. Thanks for the help.

MySql Count the same value

I have this table. My task is this : Count the students having the same first name.
I want it to show me just the firstname and then how many times firstname is found:
like this: bob | 2
id firstN lastN group grade tax gender year university
9 Alyson Hanniga 112 1 0 female 1 UTC
5 Barney Stinson 111 4 0 male 1 UBB
8 Bob Saget 111 6 900 male 3 UBB
14 Bob Bob 112 10 250 male 1 UBB
11 Cobie Smulder 111 9 200 female 1 Dragos Voda
7 David Henrie 112 9 0 male 1 Dragos Voda
12 Jason Segel 112 10 150 male 1 UTC
13 Josh Radnor 112 7 0 male 2 Dragos Voda
4 Lily Aldrin 112 5 400 female 1 UBB
6 Lyndsy Fonseca 113 10 0 female 3 UTC
2 Marshal Eriksen 112 10 100 male 2 UTC
10 Neil Patrick 112 7 0 male 3 Dragos Voda
1 Robin Scherba 111 10 0 female 1 UBB
3 Ted Mosby 112 8 0 male 2 UBB
Here is query for
select FirstName,count(*) from tblStudent group by FirstName