How to get data according to a user defined date in Tableau? - tableau-api

I am creating a tableau dashboard and it should load data according to the date the user enters. This is the logic that I am trying.
select distinct XX.ACCOUNT_NUM, (A.TOTAL_BILLED_TOT-A.TOTAL_PAID_TOT)/1000 arrears, ((A.TOTAL_BILLED_TOT-A.TOTAL_PAID_TOT)/1000 - XX.SUM_BILL ) TILL_REF_JUL20
FROM
(
select /*+ parallel (bill ,40 ) */ distinct account_num, sum(INVOICE_NET_mny + INVOICE_TAX_mny)/1000 SUM_BILL
from bill
where account_num in (Select account_num from MyTable)
and trunc(bill_dtm) > ('07-Aug-2021') –-Refmonth+1 month and 07 is a constant. Only month and year is changing
and cancellation_dtm is null
group by account_num
)xx , account a
where xx.ACCOUNT_NUM = A.account_num
Here is what I tried. First created a parameter called ref_Month. Then created a calculated field with this.
[Ref_Month]=MAKEDATE(DATETRUNC('year',(DATEADD('month', 1, [Ref_Month])), 07))
But I am getting an error. I am using a live connection. Is there any method to achive this?

I don't understand what this statement is trying to do:
[Ref_Month]=MAKEDATE(DATETRUNC('year',(DATEADD('month', 1, [Ref_Month])), 07))
If Ref_Month is a date, which it has to be for DATEADD to be a valid operation, why not make it the 7th of whatever starting month and year you are starting with? Then DATEADD('month',1, [Ref_Month]) will be a month later, still on the 7th. So you don't need DATETRUNC or MAKEDATE at all.
That said, how, when, and where are you trying to COMPUTE a PARAMETER,
let alone based on itself?

Related

SQL - Getting day for the whole week

I want to get the whole day of the week depend on the date, my query is working and getting the result that I want but when the date value is Sunday, result changes.
I'm starting the result from Mon to Sunday.
Examples below:
My Code:
SELECT UserID,Scdl_TkIN as TimeIn, Scdl_TkOut as [TimeOut]
FROM EmployeeTimekeeping
WHERE CONVERT(DATE,Scdl_TkIN) >= dateadd(day, 2-datepart(dw, '2022-04-23'),CONVERT(date,'2022-04-23'))
AND CONVERT(DATE,Scdl_TkIN) < dateadd(day, 9-datepart(dw, '2022-04-23'), CONVERT(date,'2022-04-23'))AND UserID ='15020009'
ORDER BY CONVERT(DATE,Scdl_TkIN)
1st display is correct, but when I change the value into '2022-04-24' , the result is now the second pic but I want the result still 1st pic.
If I got it right you want the whole week of data given a single date.
I'm not 100% sure about your date logic and I'd rather use the WEEK as a filter as it seems clearer, that said the issue you have is the value of SELECT ##DATEFIRST.
By default its value is 7, meaning that Sunday is considered the first day of the week, that's why you get that "unexpected" result.
here is my solution, but just setting SET DATEFIRST 1; should give you the expected result.
SET DATEFIRST 1;
SELECT
UserID
,Scdl_TkIN as TimeIn
,Scdl_TkOut as TimeOut
FROM EmployeeTimekeeping
WHERE
DATEPART(WEEK,Scdl_TkIN) = DATEPART(WEEK,'2022-04-23')
AND YEAR(Scdl_TkIN) = YEAR('2022-04-23')
AND UserID ='15020009'
ORDER BY
Scdl_TkIN
Note: if you decide to use WEEK for filtering you will have to choose between WEEK and ISO_WEEK
Edit: when using week you must also consider the year in the filter

Optimizing a WHERE clause with a dateadd function

For the business that I am working in I would like to get information on our customers. The base information I have on these customers is as follows:
Activation_Date stored in a Loans table, datatype is datetime.
ActivityDate stored in a CustomerDailyLoanActivity_Information table (a daily loans table to those interested, it is part of a datamart and stores for each day that a customer has been active with our company how much they have paid into their loan, so if a customer has an Activation_Date of 15-03-2017, it has ActivityDates in the CustomerDailyLoanActivity_Information table from 15-03-2017 up until now whereby each ActivityDate has a record in another column Sum_Paid_To_Date how much has been paid up until that ActivityDate). Datatype of ActivityDate is date.
What I would like to know is the following, I would like to know how much each customer has paid on 1, or 2, or 3, etc. months after his Activation_Date. So the query would look something like the following (slightly pseudo-code, the more important part is the WHERE clause).
SELECT
cldai.Sum_Paid_To_Date,
cldai.ActivityDate,
cldai.Customer_Account_Number
FROM
CustomerLoanDailyActivity_Information cldai
INNER JOIN
Loans l ON l.Customer_Account_Number = cldai.Customer_Account_Number
WHERE
(cldai.ActivityDate = CAST(l.Activation_Date AS date)
OR
cldai.ActivityDate = DATEADD(month, 1, CAST(l.Activation_Date AS date))
OR
cldai.ActivityDate = DATEADD(month, 2, CAST(l.Activation_Date AS date))
OR
cldai.ActivityDate = DATEADD(month, 3, CAST(l.Activation_Date AS date))
)
ORDER BY
l.Customer_Account_Number, cldai.ActivityDate ASC
So the problem is that this query is really really slow (because of the WHERE clause and because the cldai table is big (~6 GB)) and exits before any data is retrieved. A couple of problems that I have heard, and possible solutions, but haven't worked so far.
The CAST function makes the query really slow because it does a comparison with the ActivityDate column, which is indexed. I used CONVERT before but that was also really slow. I feel like I need to do the convert/cast though, because the ActivityDate is of date type and the Activation_Date is of datetime type, so there is a possibility that the time part of the datetime in Activation_Date will cause there to be no matches with the ActivityDate (e.g. Activation_Date for a given customer is 15-03-2017 09:00:00 so it will never match with ActivityDate 15-03-2017 because this might be converted to datetime 15-03-2017 00:00:00, which will never be equal because of the time part).
I have to use "DateTime" evaluations, which has been suggested as a solution, but I have no clue on how to apply this correctly.
I can't look at the execution plan because the DBA has blocked me from seeing that.
Any ideas on how to make this query perform more quickly? Any help would be greatly appreciated.
So a massive speedup was obtained by using a LEFT JOIN instead of an INNER JOIN and by not ordering the data on the server but on the client side. This reduced the query time from about an hour and 10 minutes to about 1 minute. It seems unbelievable but it's what happened.
Regards,
Tim.
If you are guaranteed to have a record for each day, you could apply use the row_number() function to apply row numbers for each group of customer loan repayment records, and then retrieve rows 1,31,61 and 91? This would avoid any date manipulation.
How about splitting this up into two steps? Step one - build a table with the four dates for each customer. Then step two, join this to your main CustomerLoanDailyActivity_Information table on date and customer account number. The second step would have a much simpler join, just an = between the ActivityDate and date entry in the table you have built.

Newbie to HQL - Date conversions in Hive - extract Year from free flow text varchar100

I have a simple HQL statement which works. I want to be able to count the occurrence's by Year or by Month. The data Quality is not good,and the incorporationdate column is held as a varchar100 and contains free flowing text and nulls
So I cannot use Substring or YEAR as I need to only perform a extract on the format mm/dd/yyyy to pull out the Year or month. Ideally I would like to create a View and create 2 new Columns , one to show the year and one to show the month this would be the perfect scenario.
select
incorporationdate, count(incorporationdate) from default.chjp2
group by companynumber,incorporationdate
===================================================
Regards
JP
you could use if and test any condition you want before using substring:
select if(date is not null and date rlike '^\d{2}\/\d{2}\/\d{4}$', substr(7),null)

Change all dates in postgresql table to previous day

I need to go through every row in a table and set every date in a particular column to the date before its current value (minus 14 hours, previous day, etc).
I could write a script to do this but I was wondering if there was a better SQL method?
Thanks!
UPDATE yourtable SET thefield = thefield - interval '14 hour';
relevant docs here, which should have been your first place to check.

How do I do a running year for a calendar report?

Here is the Query I am using:
SELECT * FROM ra_ProjectCalendar
WHERE MonthNumber between #Month and #Month + 12 and FullYear = #Year
It works great for this year, but, stops at December of this year. How do I get it to show a running year?
how is your date stored?
from your sql, i guess that you have one field for month, one for year and one for day. i'd recommend having one single datetime field instead, and then you can use the DateAdd() method to add 12 months (or any other interval).
EDIT: it was pointed out to me in a comment that one thing you gain on this is performance - which is more or less important depending on the scale of your applicaiton, but always nice to be aware of. if you run this query in a stored procedure, here's what you'll do:
DECLARE #oneYearAhead datetime;
SET #oneYearAhead = DateAdd(m, 12, #PassedDate)
SELECT * FROM ra_ProjectCalendar
WHERE #PassedDate <= [Date] AND [Date] <= #oneYearAhead;
with the above code, you will get all entries between the date you pass, and one year ahead of it. (i am not 100% sure on the syntax to declare and set the #oneYearAhead variable, but you get the idea...). Note that the [] that i use around the column name Date allows me to use reserved words in column names - i have made it a habit, instead of memorizing which words are reserved...
Try this:
SELECT * FROM ra_ProjectCalendar
WHERE
(MonthNumber > #Month AND FullYear = #Year)
OR (MonthNumber < #Month AND FullYear = #Year + 1)
Something like this might work better - but its not clear what your intent is:
select *
from ra_ProjectCalendar
where DATEDIFF ( month , #startdate , #enddate ) <=12
Edit: This is SQl server syntax
Actually pass in and store the date rather than month and year, and compare with the current date using DateDiff:
WHERE DATEDIFF(m, #PassedDate, StoredDateColumn) < 12
Note that I used 12 months, rather than 1 year, because DATEDIFF counts the number of boundries crossed. So in december, using datediff with the year datepart would return one after only one month. You really want a 12 month span.
Depending on how your information is stored (it's a little difficult to tell from your query), either DATEADD or DATEDIFF will probably do you right. There's a list of the availabe date functions on MSDN.