How to use ATTR with Fixed LOD Tableau or any workaround? - data-visualization

How to make the calculation below work.
{ FIXED [Call_Count]:IF ATTR([Date]) >= MIN([Date]) AND ATTR([Date]) <= MAX([Date])THEN COUNTD([Date]) END }
I just need to get the number of days starting the first occurrence date to the Latest date available or now().

I think we need clarification on what Call_Count is, and the table layout, but here's a stab at it:
COUNTD(
IF (
[Date] >= { FIXED [Call_Count] : MIN([Date]) }
AND [Date] <= { FIXED [Call_Count] : MAX([Date]) }
) THEN [Date]
END
)
This will check if the current row's date is >= the min date of the call_count and the current row's date is <= the max date of the call count.
However, I think that this would always be true? The date would have to be between the min and max?
Maybe you're looking for something like this?
DATEDIFF("day", { FIXED [Call_Count] : MIN([Date]) }, { FIXED [Call_Count] : MAX([Date]) })
This would get you the days between the minimum and maximum for each call_count.

Related

Dynamic date in BigQuery

Without manually having to change the date to current date, I'd like to have code which helps to change the date automatically or auto increment date by one day post 0000hrs in big query
AND ((call_date >= "2022-10-01") AND (call_date <= "2022-10-12"))
Below is the complete code.
WITH_0 AS ( SELECT *, FROM employee_calldata),
_1 AS (
SELECT
call_date AS __call_date__1,
sub_queue AS __sub_queue__1,
sum(call_count) as callstaken,
mode AS __mode__1, `FROM _0 AS _t
WHERE
(NOT ((call_type) IS NULL)))
AND ((call_date >= "2022-10-01") AND (call_date <= "2022-10-12"))
AND (sub_queue = "Customer_Complaints")
GROUP BY __call_date__1, __sub_queue__1, __mode__1)
SELECT * FROM _1
`
DATE((DATETIME_ADD(('2022-10-03 00:00:00'), INTERVAL 100 HOUR)))
=> 2022-10-07
I think what you are asking is how you can create rolling windows that increment as the days go forward.
The equivalent to:
AND ((DATE(call_date) >= "2022-10-01") AND (call_date <= "2022-10-12"))
Is:
AND ((DATE(call_date) >= DATETIME_SUB(CURRENT_DATE(), INTERVAL 2 DAY) AND (DATE(call_date) <= DATETIME_ADD(CURRENT_DATE(), INTERVAL 9 DAY)
These values will change based on the current date, change the intervals in DATETIME_SUB and DATETIME_ADD to change the difference from the current date.
Also some other general comments on your code.
You do not need brackets on the WHERE conditions.
(NOT ((call_type) IS NULL))) can be written as call_type IS NOT NULL.
You do not need your first SELECT *, FROM employee_calldata or your SELECT * FROM _1 as they do nothing extra.
This means your final query can be written as:
SELECT
call_date AS __call_date__1,
sub_queue AS __sub_queue__1,
sum(call_count) as callstaken,
mode AS __mode__1
FROM
employee_calldata AS _t
WHERE
call_type IS NOT NULL
AND DATE(call_date) >= DATETIME_SUB(CURRENT_DATE(), INTERVAL 2 DAY)
AND DATE(call_date) <= DATETIME_ADD(CURRENT_DATE(), INTERVAL 9 DAY)
AND sub_queue = "Customer_Complaints"
GROUP BY
__call_date__1,
__sub_queue__1,
__mode__1

YoY & YTD - Taking into account the timestamp, as well as date, on records in the previous year

My question is, what do I need to change in the WHERE clause for the SP to take into account the timestamp, as well as the date itself, on the CreatedDate field. For example...If the report was run at 1pm today, it would only bring back records made up to 1pm today this time last year [2015] and obviously all records created in 2016 so far. At present, 2015 is bringing back all records up to the end of the day. Any help would be much appreciated. Claire
#CreatedFrom15 DateTime = NULL,
#CreatedTo15 DateTime = NULL,
#CreatedFrom16 DateTime = NULL,
#CreatedTo16 DateTime = NULL,
SELECT
BookingID,
CreatedYear,
CreatedDate,
NightDuration,
HolidayCost,
TRAVPropertyTypeCategory
FROM Vw_TRAVLodgeResortGlampingFinancialByNights
WHERE
((CreatedYear = DATEPART(YEAR,GETDATE())-1 AND CreatedDate BETWEEN #CreatedFrom15 AND #CreatedTo15) AND (CreatedDate >= #CreatedFrom15 OR #CreatedFrom15 IS NULL) AND (CreatedDate <= #CreatedTo15 OR #CreatedTo15 IS NULL)
OR
(CreatedYear = DATEPART(YEAR,GETDATE()) AND CreatedDate BETWEEN #CreatedFrom16 AND #CreatedTo16) AND (CreatedDate >= #CreatedFrom16 OR #CreatedFrom16 IS NULL)AND(CreatedDate <= #CreatedTo16 OR #CreatedTo16 IS NULL))
can you specify the dates of the parameters?
anyhow, the reference to the year seems unnecessary and also you can include the null case inside the between like this-
where CreatedDate between isnull(#createdFrom16,'20160101') and isnull(#CreatedTo16,'20161231')

Choose between two different date ranges

I have a check box, when checked, my date range flips causing me now to choose which date range to look at.
So, 4/15/14 thru 4/20/14 … when my check box is checked, this date range is now 4/10/14 thru 4/15/14.
In my SQL Select I need to chose, based on this check box, which date range.
This didn't work ??
Where ( ? Between Date_1 and Date_2 ) or ( ? Between Date_2 and Date_1 )
Nor did this work ??
Where ( ?
Case
When Ck_Bx Is Null
Then Date_2 and Date_1
Else Date_1 and Date_2
End
)
Here is the SQL THAT IS WORKING AND I AM TRYING TO MODIFY THE "WHERE CLAUSE"
ExecuteSQL ( "
Select ToDo_Name_Calc, ToDo_Name
From ToDo
WHERE ( ( ? Between ToDo_Alert_Date and ToDo_Date ) or ( ? Between ToDo_Date and ToDo_Alert_Date ) ) and ToDo_Ck_Bx Is Null
Order By ToDo_Alert_Date Asc " ; " - " ; "" ;
cDateOfFirstPortal +11 )
Any assistance I would be grateful.
Tom
between is really just a syntax shortcut and it is the exact equivalent of:
( field >= small-value and field <= larger-value )
Let's say we use 2014-04-20 as the larger-value, but the field contains time as well as date. So evaluating <= 2014-04-20 against a stored value of 2014-04-20 11:12:13 means that value is ignored. In truth we really do want that value included (it occurs DURING the day of 2014-04-20) and the most reliable way of protecting we don't make that mistake is to get all data that is less than 2014-04-21.
So, the greatest problem faced when using date ranges using between is that you could miss almost 24 hours of data if you get it wrong. A safer approach avoids this problem by using less than for the upper date - but you add one day to it, and because we need to add one day we may have to use database specific code (e.g. date_add() for MySQL, dateadd() for SQL Server/Sybase).
Not using between, which is the safer option, requires some date arithmetic, represent here simply by +1
SELECT
ToDo_Name_Calc
, ToDo_Name
FROM ToDo
WHERE (
? >= ToDo_Alert_Date AND ? < (ToDo_Date+1) --*
AND ToDo_Ck_Bx IS NULL)
OR (
? >= ToDo_Date AND ? < (ToDo_Alert_Date+1) --*
AND ToDo_Ck_Bx IS NOT NULL
)
ORDER BY
ToDo_Alert_Date ASC
--* use the relevant date addition method for your dbms.
Another reason for not using between here is that the dates MUST be in a specific order or nothing is returned', the older date must be first, the younger date must be last. You cannot just reverse them inside the between syntax.

Insert value into column using function and mathematical calculation

table IssuedBooks
{
column RollNo,BookNo,BookName,AuthorName,IssueDate,Fine
}
I want to create a function which fills or gives the calculated Fine when called. Suppose after 15 days of IssueDate there is a fine of 5 each day. May be I can send the IssueDate as parameter to the function and then calculate the difference between the IssueDate and Current date using DATEDIFF(DAY,IssueDate,CAST(GETDATE() as date)) and then use the number of days returned to check whether they are greater than 15 and calculate the fine accordingly and return it through function.I am very confused about how to implement it, what will be the queries etc. Please help.
Thank you very much in advance..
SELECT
RollNo, BookNo, BookName, AuthorName, IssueDate,
CASE
WHEN DATEDIFF(Day,IssueDate,cast(getdate() as date)) > 15
THEN (DATEDIFF(Day,IssueDate,cast(getdate() as date)) - 15) * 5
ELSE '0'
END as Fine
From IssuedBooks
Or just:
UPDATE IssuedBooks
SET Fine = (DATEDIFF(Day,IssueDate,cast(getdate() as date)) - 15) * 5
WHERE DATEDIFF(Day,IssueDate,cast(getdate() as date)) > 15

Count of records that are created the same day

I am trying to create a array that should be something like this:
[ [created_at date * 1000, count record for that date],
[created_at date * 1000, count record for that date],
[created_at date * 1000, count record for that date] ]
The created_at date is not exactly the same, because of minutes, hours and seconds.
I was thinking is it possible to change created_at time on create to 00:00:00
I have tried with this,
#kliks = Klik.all.map{|klik| [(klik.created_at.to_i * 1000), 1]}
But I have not figure out to sum those records that are created the same day. Also this loops create a array for every single record, I don't want duplicates of the sum.
Rails has ActiveRecord::Calculations which is designed to do exactly this sort of thing at the database level. You should use it. In this case, count is the method you want:
#kliks = Klik.count( :group => "DATE( created_at )" )
This is equivalent to the following SQL:
SELECT *, COUNT(*) FROM kliks
GROUP BY DATE( created_at )
The DATE() function is MySQL changes a datetime (like created_at, e.g. 2012-02-27 10:08:59) to a plain date (e.g. 2012-02-27). No need to go converting things to integers or multiplying minutes and seconds, and no need to use map or any other method in Ruby.
According to the query guide, you should try with
items = Klik.select("date(created_at) as creation_date, count(*) as count").group("date(creation_date)")
result = items.map { |k| [ k['creation_date'], k['count'] ] }
The following will produce the result you have asked for:
Klik.all.group_by do |k|
k.created_at.beginning_of_day
end.map do |date, records|
[date, records.length]
end