I am trying to create an RDL file and I need a tablix to appear in the following format.
This is how I want the results to look
The values that are in bold are hard coded values. This is how the output from the SELECT statement in the datasets looks
SQL Output
I don't know how to make the values that output from the database match with the hard coded values in the RDL file. The 'Day' field represents a day in the month and the 'Num' field represents the number of sales that were on the day. The above example shows that on the first day of the month, there were 100 sales made. I need the tablix to output in that specific format.
If the day isn't in the SQL output (no sales made that day), I want it to output blank and/or 0.
Any idea how this could be accomplished?
Use a CTE to create rows for each day you need and then join your results on. A starting point for you CTE could be:
;WITH nums AS
(SELECT 1 AS value
UNION ALL
SELECT value + 1 AS value
FROM nums
WHERE nums.value <= 30)
SELECT *
FROM nums
You'll probably then want to modify the total days based on the month you are viewing.
You can do this using lookups, but you would need to hard code a lookup in each cell. e.g. for day 1
=lookup(cint(1),Fields!Day.Value,Fields!Num.Value,"Dataset1")
A faster way would be to create a tablix on the dataset filtered on the first ten days:
=Switch(
Fields!DAY.Value <= 10 and Fields!DAY.Value >=1,"Include",
True,"Exclude"
)
Create a row group on days, then create a column with day and num, and columns with Fields!DAY.Value+10 and Fields!DAY.Value+20 with the following lookups:
=lookup(Fields!DAY.Value+10,Fields!DAY.Value,Fields!NUM.Value,"DataSet1")
=lookup(Fields!DAY.Value+20,Fields!DAY.Value,Fields!NUM.Value,"DataSet1")
Related
I have a table named as energymeter which have 3 columns named as Date, Name, Value.
I want a result of highest value of day- minimum value of day of each day in specified time period of individual parameter (Name) into 3 separate columns.
Here is my table structure
And i am getting result for single parameter as i wish using query
select Date(Date) as Date,
Max(Value) - Min(Value) as Value ,
Name
from energy_meter.energy_meter
WHERE Name ='Energy_Meters\[1\].Total_active_energy_kWh'
And DATE(Date) between '2022-10-01' and '2022-10-13'
group by DATE(Date)
and getting result as below
and i want result should look like below for all 3 parameters
Community help is needed.
Thanks
I am having table name as "Table1" in mysql.I have to find Sum of Mean and Std dev on column "Open".I did it easily using python but I am unable to do it using sql.
Select * from BANKNIFTY_cal_spread;
Date Current Next difference
2021-09-03 00:00:00 36914.8 37043.95 129.14999999999418
2021-09-06 00:00:00 36734 36869.15 135.15000000000146
2021-09-07 00:00:00 36572.9 36710.65 137.75
2021-09-08 00:00:00 36945 37065 120
2021-09-09 00:00:00 36770 36895.1 125.09999999999854
Python Code-
nf_fut_mean = round(df['difference'].mean())
print(f"NF Future Mean: {nf_fut_mean}")
nf_fut_std = round(df['difference'].std())
print(f"NF Future Standard Deviation: {nf_fut_std}")
upper_range = round((nf_fut_mean + nf_fut_std))
lower_range = round((nf_fut_mean - nf_fut_std))
I search for Sql solution but I didn't get it. I tried building query but it's not showing correct results in query builder in grafana alerting.
Now I added Mean column ,std dev column , upper_range and lower_range column using python dataframe and pushed to mysql table.
#Booboo,
After removing Date from SQL Query, it's showing correct results in two columns- average + std_deviation and average - std_deviation.
select average + std_deviation, average - std_deviation from (
select avg(difference) as average, stddev_pop(difference) as std_deviation from BANKNIFTY_cal_spread
) sq
It looks as though the sample you're using for the aggregations for MEAN, STDDEV, etc is the entire table - in which case you have to drop the DATE field from the query's result set.
You could also establish the baseline query using a CTE (Common Table Expression) using a WITH statement instead of a subquery, and then apply the subsequent processing:
WITH BN_CTE AS
(
select avg(difference) as average, stddev_pop(difference) as std_deviation from BANKNIFTY_cal_spread
)
select average + std_deviation, average - std_deviation from BN_CTE;
With the data you posted having only a single Open column value for any given Date column value, you standard deviation should be 0 (and the average just that single value).
I am having difficulty in understanding your SQL since I cannot see how it relates to finding the sum (and presumably the difference, which you also seem to want) of the average and standard deviation of column Open in table Table1. If I just go by your English-language description of what you are trying to do and your definition of table Table1, then the following should work. Note that since we want both the sum and difference of two values, which are not trivial to calculate, we should calculate those two values only once:
select Date, average + std_deviation, average - std_deviation from (
select Date, avg(Open) as average, stddev_pop(Open) as std_deviation from Table1
group by Date
) sq
order by Date
Note that I am using column aliases in the subquery that do not conflict with built-in MySQL function names.
SQL does not allow both calculating something in the SELECT clause and using it. (Yes, #variables allow in limited cases; but that won't work for aggregates in the way hinted in the Question.)
Either repeat the expressions:
SELECT average(difference) AS mean,
average(difference) + stddev_pop(difference) AS "mean-sigma",
average(difference) - stddev_pop(difference) AS "mean+sigma"
FROM BANKNIFTY_cal_spread;
Or use a subquery to call the functions only once:
SELECT mean, mean-sigma, mean+sigma
FROM ( SELECT
average(difference) AS mean,
stddev_pop(difference) AS sigma
FROM BANKNIFTY_cal_spread
) AS x;
I expect the timings to be similar.
And, as already mentioned, avoid using aliases that are identical to function names, etc.
I have a temp table and I'm trying to sum data but can't seem to get the logic right for it. The table contains customer level data and now I'm trying to aggregate it by fiscal year, quarter, and product description. I'm trying to sum by going back 1 year and using the same quarter to sum the # of units sold.
I can do this in excel, but the table is too large for that. This is what the formula in Excel looks like:
=SUMIFS(Units,FiscalYearQuarter >= Concat(FiscalYear -1 & FiscalQuarter, FiscalYearQuarter <= Concat(FiscalYear, FiscalQuarter)
Here's an example of the table:
Here's what the results should looks like (This does not include productdescription, but I will want to add that in):
Every time I try to group by or do a Sum(Case When...) I keep getting the results only by the fiscal year/quarter instead of the sum of historical for 1 year.
A simple GROUP BY will work (although I don't quite understand your Excel logic with concatenation):
SELECT t1.FiscalYear, t1.FiscalQuater, sum(t2.UnitsPurchased)
FROM `table` t1
LEFT JOIN `table` t2
ON ( t1.FiscalYear = t2.FiscalYear + 1
AND t1.FiscalQuater < t2.FiscalQuater)
OR ( t1.FiscalYear = t2.FiscalYear
AND t1.FiscalQuater >= t2.FiscalQuater)
GROUP BY t1.FiscalYear, t1.FiscalQuater
EDIT 1
modified query based on author's feedback
I have attendance data for employees stored in the table attendance with the following column names:
emp_id (employee ID)
date
type (leave, absent, etc.)
(there are others but I'm omitting them for the sake of simplicity)
My objective is to retrieve all dates of the given month on which the employee was on leave (type = 'Leave') and the last leave taken in the last month, if any.
It's easy to do it using two queries (I'm using PHP to get process the data), but is there any way this can be done in a single query?
I'm answering my own question so as to close it. As #bpgergo pointed out in the comments, UNION will do the trick here.
SELECT * FROM table_name
WHERE type="Leave" AND
date <= (CURRENT_DATE() - 30)
Select the fields, etc you want then se a combined where clause using mysql's CURRENT_DATE() function. I subtracted 30 for 30 days in a month.
If date is a date column, this will return everyone who left 1 month or longer ago.
Edit:
If you want a specific date, change the 2nd month like this:
date <= (date_number - 30)
I have a table which contains date (Field Type: Date and Date Format: %Y-%m-%d) as a field. I need to select all the rows from the table for all the years whose date is not between Dec 3rd and Dec 24th.
The table contains month and day as a separate fields.
The result can be obtained by using the following query:
select * from mytable where date not in (select date from mytable where month=12 and day between 3 and 24);
But i m trying to get the result in a single query like the below one but it gave empty rows:
select * from mytable where date not between '%Y-12-03' and '%Y-12-24';
Can it be done in a single query like the above one?
SELECT *
FROM mytable
WHERE MONTH(`date`) <> 12
OR DAY(`date`) NOT BETWEEN 3 AND 24
;
This will give you every row that meets the requirements. I'm sure someone has a faster way of doing this, since this will ignore all indexes and will likely be slow on a large dataset, but it does work and return the data you require, so if no-one can suggest an improvement this will answer your question.