Rails Daily/Monthly Report - mysql

Report
I need to create a daily report + monthly report with this records.. All records is from one table and under one field('nature' is the name of my field)..
Calls
Texts
Emails
Is there any possible way to create a single query that will
generate this daily report + monthly report.?
Ex.
Day1 Day2 Day3 etc.. Total
Calls 20 23 23 ... 66
Texts 120 125 130 ... 375
Emails 50 60 55 ... 165
Total 190 208 208 ... 606
If not, please tell how to do it the simplest way, and how to this on rails.?
Thanks in advance...

for One month
$todayDate = date("Y-m-d");
$thismonth = strtotime($todayDate);
$next_month = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
query
select colum_name from table_name where month(table_field_name) BETWEEN month($thismonth) AND month($next_month) AND year(table_field_name) BETWEEN year($thismonth) AND year($next_month)

Related

How to add a loop in SSRS

I am using SSRS 2012 R2.
I need to create a loop in ssrs to calculate the compliance of ONB to LB
but with a condition, for the total bags count <= 100 it will be 27 mins. but for each additional 50 bags add 5 mins.
I tried the code below but it takes only 27 and 34 min. I want to replace 34 with a loop.
count(
iif(Fields!Duration_ONB_LB_.Value <
iif(Fields!Total_Bag_Count.Value <= "100", "27","34") ,1, Nothing))
/ count(Fields!TotalRows.Value) * 100 )
Can you please help me out in this?
I'm not sure if 'loop' is the correct term, I assume you want to just create an expression that adds 5 mins for every 50 bags (or part of).
I created a sample dataset so you can see the results, it assumes that up to 100 bags is 27 mins, 101-150 bags would be 32 mins, 151-200 bags would be 37 mins and so on...
here is the expression I used, you will need to replace the iif(Fields!Total_Bag_Count.Value <= "100", "27","34") part of your expression with this.
IIF
(
Fields!Total_Bag_Count.Value <= 100,
27,
(CEILING((Fields!Total_Bag_Count.Value -100) / 50) * 5) + 27
)
Here are the results

5 point average in SSRS

I try to put a 5 point avg in my chart. I add a trendline, but it looks like this:
And then I created a new series to calculate there the avg. and this looks like this:
but I would like to show this in a 5 point average. How can I do this?
This answer is based on my experience with Excel, not reporting services, but it is probably the same problem.
Your chart is probably a scatter plot rather than a line chart (note: this is Excel terminology). A scatter plot does not have an intrinsic ordering in the data. A line chart does.
The solution (for a scatter plot) is simply to sort the data by the x-values. The same will probably work for you. If you are pulling the data from a database, then order by can accomplish this. Otherwise, you can sort the data in the application.
Using this post as a starting point you can see that it is possible to calculate a moving average for a chart using the SQL query that pulls the data from the database.
For example, using this table in my database called mySalesTable
myDate sales myDate sales myDate sales
---------- ------ ---------- ------ ---------- ------
01/01/2015 456 16/01/2015 546 31/01/2015 658
02/01/2015 487 17/01/2015 12 01/02/2015 121
03/01/2015 245 18/01/2015 62 02/02/2015 654
04/01/2015 812 19/01/2015 516 03/02/2015 261
05/01/2015 333 20/01/2015 1 04/02/2015 892
06/01/2015 449 21/01/2015 65 05/02/2015 982
07/01/2015 827 22/01/2015 15 06/02/2015 218
08/01/2015 569 23/01/2015 656 07/02/2015 212
09/01/2015 538 24/01/2015 25 08/02/2015 312
10/01/2015 455 25/01/2015 549 09/02/2015 21
11/01/2015 458 26/01/2015 261
12/01/2015 542 27/01/2015 21
13/01/2015 549 28/01/2015 21
14/01/2015 432 29/01/2015 61
15/01/2015 685 30/01/2015 321
You can pull out this data, and create a Moving average based on the last 5 dates by using the following query for your dataset
SELECT mst.myDate, mst.sales, avg(mst_past.sales) AS moving_average
FROM mySalesTable mst
JOIN mySalesTable as mst_past
ON mst_past.myDate
BETWEEN DATEADD(D, -4, mst.myDate) AND mst.myDate
GROUP BY mst.myDate, mst.sales
ORDER BY mst.myDate ASC
This is effectively joining a sub-table for each row consisting of the previous 4 dates and the current date, and finds the average for these dates, outputting that as the column moving_average
You can then chart both these fields as normal, to give the following output (with the data table so you and see the actual calculated moving average)
Hopefully this will help you. Please let me know if you require further assistance

How to Union every 2 consecutive columns from the same table

date1 tran_1 date2 tran_2 date3 tran_3 ..... date80 tran_80
may01 24 jun02 32 aug18 56 ..... sep10 44
Sep01 24 Nov08 32 Jan18 56 ..... Jun18 44
Now the output should be and How to write a dynamic query.. I have written a procedure by passing parameters , for the above 80 variables i have to call the procedure about 40 times, plz help me
date tran type
may01 24 tran_1
Sep01 24 tran_1
jun02 32 tran_2
Nov08 32 tran_2
aug18 56 tran_3
Jan18 56 tran_3
........................
........................
sep10 44 tran_80
Jun18 44 tran_80
One method is to just use union all:
select date, tran_1 as tran, 'tran_1' as type from t union all
select date, tran_2 as tran, 'tran_2' as type from t union all
select date, tran_3 as tran, 'tran_3' as type from t union all
. . .
My recommendation would be to generate the code in a spreadsheet. Just generate the numbers 1 to 80 and use spreadsheet functions. Alternatively, you could generate dynamic SQL, if you don't want to type all the column names in.
If performance is an issue and you have lots and lots data, there are other methods. However, this type of query is often run only once and a more efficient query is more difficult to construct.

Want to Calculate Hightest Pay Value For a table?

I want a list of employees who
have worked on the activity that has the highest Total Pay value.
don't use code such as …where actid = 151…ect
• Note: Total Pay worked for an activity is the sum of the (Total Hours Worked * matching
Hourly Rate)
(e.g. Total Pay for Activity 151 is 10.5 hrs # $50.75 + 11.5 hrs # $25 + 3hrs # $33,)
You must use a subquery in your
solution.
ACTID HRSWORKED HOURLYRATE Total Pay
163 10 45.5 455
163 8 45.5 364
163 6 45.5 273
151 5 50.75 253.75
151 5.5 50.75 279.125
155 10 30 300
155 10 30 300
165 20 25 500
155 10 30 300
155 8 27 216
151 11.5 25 287.5
151 1 33 33
151 1 33 33
151 1 33 33
You time and effort much appreciated. Thanks !!
Without knowledge of the schema, I can only provide a possible sketch (you'll have to compute total pay and provide all necessary JOINs and predicates):
SELECT DISTINCT(employee id) -- reconfigure if more then just employee id
FROM <table(s)>
[WHERE...]
{ WHERE | AND } total pay = (SELECT MAX(total pay)
FROM <table(s)>
[WHERE...]);
I used DISTINCT because it's possible to have more than one activity with the same MAX value and overlapping employees. If you're including ACTID in the output, then you won't need DISTINCT because the same employee shouldn't be on a project twice (unless they are tracked by roles on a project in which case a single employee might have multiple roles - it all depends on the data set).

Querying a table to get values based on no of digits of a parameter?

Considering the following table
I have a large table from which I can query to get the following table
type no of times type occurs
101 450
102 562
103 245
111 25
112 28
113 21
Now suppose I wanted to get a table which shows me the sum of no of times type occurs
for type starting with 1 then starting with 10,11,12,13.......19 then starting with 2, 20,21, 22, 23...29 and so on.
Something like this
1 1331 10 1257
11 74
12 ..
13 ..
.. ..
2 ... 20 ..
21 ..
Hope I am clear
Thanks
You really have two different queries:
SELECT [type]\100 AS TypePart, Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100;
And:
SELECT [type]\100 AS TypePart, [type] Mod 100 AS TypeEnd,
Count(t.type) AS CountOftype
FROM t
GROUP BY [type]\100, [type] Mod 100;
Where t is the name of the table.
Here on the first query i am getting something like this
utypPart CountOftype
1 29
2 42
3 46
4 50
5 26
6 45
7 33
9 1
it is giving me how many utyp are starting with 1 2 and so on
but whai i want is the sum of no of times those types occur for the utyp .