How to add 'date_add()' in adonis js query builder - adonis.js

I am trying to convert a row sql query. Here is my original row query that I want to run
select * from sales WHERE updated_at BETWEEN
date_add('2018-10-24', INTERVAL -10 HOUR)
and
date_add('2018-10-25', INTERVAL -10 HOUR)
I want to add this date_add functionality into this query
Sale.query().whereBetween('updated_at',[data.date1,data.date2])
.getSum('totalAmount')
Many thanks.

What you need is a whereRaw, your query will be the next
Sale.query().whereRaw('updated_at BETWEEN date_add('2018-10-24', INTERVAL -10 HOUR) and date_add('2018-10-25', INTERVAL -10 HOUR'))

Related

SAS - Cumulative sum with date range and conditions

The following is an example of the data I have
startdate
enddate
amount
1/1/2010
2/2/2020
10
1/5/2011
2/3/2015
10
1/3/2012
2/2/2023
10
1/4/2013
2/2/2014
10
5/5/2015
2/2/2028
10
1/6/2016
2/2/2032
10
I want to calculate the sum of all existing amounts as of each start date so it should look like this:
startdate
amount
1/1/2010
10
1/5/2011
20
1/3/2012
30
1/4/2013
40
5/5/2015
30
1/6/2016
40
How do I do this in SAS?
Essentially what I want to do is for each of the start dates, calculate the cumulative sum of any amounts that haven't expired. So for the first four dates, it is just a running cumulative sum because none of the amounts have expired. But at 5/5/2015, two of the previous amounts have expired hence a cumulative sum of 30. Same for the last date, where the same two have previously expired and you have the additional amount as of 1/6/2016 therefore 40.
One way to accomplish this is with a self-join via Proc SQL:
proc sql;
create table out_dset as
select a.startdate, sum(a.amount) as amount
from in_dset as a left join in_dset as b
on a.startdate >= b.startdate and a.startdate < b.enddate
group by a.startdate
order by a.startdate;
quit;
For each observation in the original dataset, this code will find observations in the same dataset that meet the date range criteria and will sum up the amount column.
You can change the second comparison operator from < to <= if you want to include situations when a previous amount expired on the same date as a given startdate.

Timestamp of yesterday at specific hour bigquery

I need to schedule a query on BigQuery, that will retrieve some data from 24h ago. The problem is that for example if its scheduled for 5am, the query needs time to be executed (let's say 2.248 seconds), and so the yesterday's data from 5:00:00.000 to 5:00:02.248 will not be retrieved. I'm using a timestamp field, and i do something like this :
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP_SUB(current_timestamp(), INTERVAL 24 hour)
I would like to know if there is a way to get the yesterday's date, at a specific hour, so even if there is a little gap due to the execution, it will still retrieve data from a specific hour.
edit:
I found something :
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP(DATE_SUB(current_date(), INTERVAL 1 DAY) )
AND my_timestamp < TIMESTAMP(current_date)
But this retrieves yesterday between 00:00:00.000 to 23:59:59.999
It is okay but is there a way to choose the hour ?
Consider below least verbose approach
select *
from my_table
where my_timestamp >= timestamp(current_date - 1 + interval 5 hour)
and my_timestamp < timestamp(current_date + interval 5 hour)
Okay I found on my own lol, maybe it will help someone else looking for this.
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP_ADD(TIMESTAMP(DATE_SUB(current_date, INTERVAL 1 DAY)), INTERVAL 5 HOUR)
AND my_timestamp < TIMESTAMP_ADD(TIMESTAMP(current_date), INTERVAL 5 HOUR)

MDX query for percent of current row within table

I have an SSAS cube with a FactTicket, that has a TicketsSold measure.
The cube has a good number of dimensions. Lets call them Date, Agent and Geo.
I can create a table (we're using Power BI service, if it makes a difference) and put in it all and any dimensions, and the TicketsSold measure.
What I'd like to do, is have another measure that'll calculate the percent of each row in the table out of the total of the table at that moment.
so suppose I have the following data:
Date Agent Geo TicketsSold
2020-04-01 Moulder US 12
2020-04-01 Moulder UK 4
2020-04-02 Moulder US 10
2020-04-03 Moulder UK 5
2020-04-01 Skully US 16
2020-04-01 Skully UK 12
I would like to be able to filter my report on any measure, display any other measure(s) with the TicketsSold and the percent.
Such as, filtered on 2020-04-01 and 2020-04-02:
Agent Geo TicketsSold %ofTicketsSold
Moulder US 22 40.74 %
Moulder UK 4 7.40 %
Skully US 16 29.63 %
Skully UK 12 22.22 %
TOTAL 54 100 %
Or filtered only on Agent Moulder:
Date TicketsSold %ofTicketsSold
2020-04-01 16 51.61 %
2020-04-02 10 32.26 %
2020-04-03 5 16.13 %
TOTAL 31 100 %
It's clear to me that what I probably need is some way of getting -- for each row in the table -- the total of the table itself, and then dividing the row's own TicketsSold by this total.
But I can't seem to figure out a trivial way of creating such a calculation. Is this doable, without having to define a tuple of all possible dimensions?
Thanks!
You can achive that using the axis function . Below is an example based on Adventureworks
where we will create a PercentageOFTotal Column based on InternetSales
select
{[Measures].[Internet Sales Amount]}
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result
Now lets add our new column
with member measures.PercentageOFTotal
as round(([Measures].[Internet Sales Amount]/sum(Axis(1),[Measures].[Internet Sales Amount]))*100,2)
select
{[Measures].[Internet Sales Amount],measures.PercentageOFTotal }
on columns,
non empty {([Product].[Category].[Category],[Product].[Subcategory].[Subcategory])}
on rows
from
[Adventure Works]
where [Date].[Calendar Year].&[2013]
Result
If you are already using PBI: why don't you make use of DAX? A DAX query is able to solve your problem: MEASURE Fact[Percent] = CALCULATE(SUM(Fact[Amount]), ALLSELECTED(Fact)). "ALLSELECTED" is similar to VISUALTOTALS() in MDX, but more dynamic.

How to calculate accumulated time for a defined frequency?

I have rows containing descriptions of services that have been ordered by our customers.
Table:
OrderedServices
Columns:
Id (key)
CustomerId
ServiceId
StartDate
EndDate
AmountOfTimeOrdered (hours)
IntervalType (month, week or day)
Interval (integer)
An example:
1;24343;98;2020-01-20;2020-06-05;1.5;day;3
The above is read as ”Customer w/ id 24343 has ordered service #98 to be executed 1.5hrs every 3rd day during the period 2020-01-20 up until 2020-06-05”
The first day of execution is always StartDate, so, in the given example, the services is first executed 2020-01-20, followed by 2020-01-23 (20+3), 2020-01-26, 2020-01-29 aso.
Now I want to calculate the total amount of time executed for a given ServiceType for a given time period.
E.g. 2020-01-01 - 2020-01-31 = 4 x 1.5 = 6hrs in total executed time for the above.
What I can’t figure out is how to create a measure, or a calculated table to achieve this.
Does anyone have an idea?
Kind regards,
Peter
Go to the query editor and use the following stepts:
If your column looks like in your example use as first step Split Column by Delimiter.
After this just add the following custom column:

Using TABLESAMPLE with PERCENT returns all the records from table

I have a small test table with two fields - id and name, 19 records total. When I try to get 10 percent of record from this table using the following query, I get ALL the records. I tried to do this on large table, but result is the same - all records are returned. The query:
select * from test tablesample (10 percent) s;
If I use ROWS instead of TABLESAMPLE (i.e.: select * from test tablesample (10 rows) s;, it works fine, only 10 records are returned. How can I get just the neccessary percentage of records?
You can refer to the link below:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Sampling
You must be using CombinedHiveOutputFormat, which does not go well with ORC format. Hence you will never be able to save the output from Percent query to a table.
In my knowledge the best way to do this is using rand() function. But again you should not use this with order by() clause as it will impact performance. Here is my sample query which is time efficient :
SELECT * FROM table_name
WHERE rand() <= 0.0001
DISTRIBUTE BY rand()
SORT BY rand()
LIMIT 5000;
I tested this on 900M row table and query executed in 2 mins.
Hope this helps.
You can use PERCENT with TABLESAMPLE. For example:
SELECT * FR0M TABLE_NAME
TABLESAMPLE(1 PERCENT) T;
This will select 1% of the data size of inputs and not necessarily the number of rows. More details can be found here.
But if you are really looking for a method to select a percentage of the number of rows, then you may have to use LIMIT clause with the number of records you need to retrieve.
For example, if your table has 1000 records, then you can select random 10% records as:
select * from table_name order by rand() limit 100;