AWS Quicksight average on unique data - amazon-quicksight

I have a table like this
Order ID
Department
Item
Order Date
EndOrderDate
DiffTime
1
dep1
it_1_1
01-01-2022
02-01-2022
1
1
dep1
it_1_2
01-01-2022
02-01-2022
1
2
dep1
it_2_1
02-01-2022
04-01-2022
2
2
dep1
it_2_2
02-01-2022
04-01-2022
2
3
dep2
it_3
02-01-2022
05-01-2022
3
In my view a need a table with average response time for order
Department
average time
Dep1
1.5
Dep1
3
How can i do it?
If i use average i get all the value from all item, but i need to include value only once for order

Related

Logstash bulk update documens in ElasticSearch with WHERE conditon

I have 2 tables in MySQL like this
Table DEPARTMENT
Id
Name
1
Department 1
2
Department 2
Table STAFF
Id
Department_Id
Name
1
1
Staff 1
2
1
Staff 2
3
2
Staff 3
4
1
Staff 4
STAFF table has about 10 million records.
All STAFF's informations has been pushed by Logstash to ElasticSearch. Each document in ElasticSearch now only have 3 fields are Staff_Id, Staff_Name and Department_Name. Something like this:
{
"Staff_Id": 1,
"Staff_Name": "Staff 1",
"Department_Name": "Department 1"
}
Because of practical needs, I need to add one more field called Department_Id to each document. Note that this field (Department_Id) does not exist on existing documents.
I am a newbie to both Logstash and ElasticSearch. How can I do this with Logstash? Interpreted in the SQL way would be:
SELECT * FROM DEPARTMENT;
UPDATE STAFF SET Department_Id = XXX WHERE Department_Name = YYY
Note that DEPARTMENT table has about 100.000 records and ElasticSearch has about 10 million documents.
Can you take a look?

Access other values ​in a trigger before save Oracle

Is it possible to access the previous values ​​that have not yet been stored in the database?
I have a table related to a particular module (MOD) which I will call table XA.
I can insert multiple records into XA simultaneously they are going to be inserted, I cannot change this fact.
For example, the following data is inserted in XA
ID | ParentId | Type | Name | Value
1 | 1 | 5 | Cost | 20000
2 | 1 | 9 | Risk | 10000
And I need in this case to insert / update a record in this same table. A calculated value
At the moment of executing the trigger, the value with the name of Cost for example is inserted first, and then the value of Risk.
When evaluating the Risk, I must have the ability to know what the Cost value is to make the calculation and insert the calculated record.
I tried to create a Package to which I would feed the data, but I still have the same problem.
create or replace PACKAGE GLOBAL
IS
PRAGMA SERIALLY_REUSABLE;
TYPE arr IS TABLE OF VARCHAR2 (32)
INDEX BY VARCHAR2 (50);
NUMB arr;
END GLOBAL;
//Using in trigger
GLOBAL.NUMB (:NEW.ID || '-' || :NEW.ParentId) := :NEW.Value;
BEGIN
IF :NEW.Type == 9 AND GLOBAL.NUMB (5 || '-' || :NEW.ParentId) IS NOT NULL
THEN
// calculate and insert record
ELSE IF :NEW.Type == 5 AND GLOBAL.NUMB (9 || '-' || :NEW.ParentId) IS NOT NULL
// calculate and insert record
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
// NOT HAVE TWO INSERT TO SAME REGISTER
END;
Values ​​5 and 9 are for reference.
Both records are not always inserted, one or more can be inserted, even the calculated value can be imputed but must be replaced by the calculation.
And I can't create a view since there is an internal process that depends on this particular table.
Do you really really must store calculated value into a table? That's usually not the best idea as you have to maintain it in any possible case (inserts, updates, deletes).
Therefore, another suggestion: a view. Here's an example; my "calculation" is simple, I'm just subtracting cost - risk as I don't know what you really do. If calculation is very complex and should be run every time on a very large data set, yes - performance might suffer.
Anyway, here you go; see if it helps.
Sample data:
SQL> select * From xa order by parentid, name;
ID PARENTID TYPE NAME VALUE
---------- ---------- ---------- ---- ----------
1 1 5 Cost 20000
2 1 9 Risk 10000
5 4 5 Cost 4000
7 4 9 Risk 800
A view:
SQL> create or replace view v_xa as
2 select id,
3 parentid,
4 type,
5 name,
6 value
7 from xa
8 union all
9 select 0 id,
10 parentid,
11 99 type,
12 'Calc' name,
13 sum(case when type = 5 then value
14 when type = 9 then -value
15 end) value
16 from xa
17 group by parentid;
View created.
What does it contain?
SQL> select * from v_xa
2 order by parentid, type;
ID PARENTID TYPE NAME VALUE
---------- ---------- ---------- ---- ----------
1 1 5 Cost 20000
2 1 9 Risk 10000
0 1 99 Calc 10000
5 4 5 Cost 4000
7 4 9 Risk 800
0 4 99 Calc 3200
6 rows selected.
SQL>

Complex Networks in Hive - Optimization Code

I have a problem with how to get my Hive code optimized.
I have a huge table as follows:
Customer_id Product_id Date Value
1 1 02/28 100.0
1 2 02/02 120.0
1 3 02/10 144.0
2 2 02/15 120.0
2 3 02/28 144.0
... ... ... ...
I want to create a complex network where I link the products through the buyers. The graph does not have to be directed and I have to count the number of links between them.
In the end I need this:
Product_x Product_y amount
1 2 1
1 3 1
2 3 2
Can anyone help me with this?
I need an optimized way to do this. The join of the table with itself is not the solution. I really need an optimum way on this =/
CREATE TABLE X AS
SELECT
a.product_id as product_x,
b.product_id as product_y,
count(*) as amout
FROM table as a
JOIN table as b
ON a.customer_id = b.customer_id
WHERE a.product_id < b.product_id
GROUP BY product_x, product_y;

Using loop to insert values from one table to another

I have the following table named screening_plan:
plan_id movie_id plan_start_day plan_end_day plan_min_start_hh24 plan_max_start_hh24 screenings
1 1 1/06/2015 28/06/2015 9 17 5
2 2 1/06/2015 28/06/2015 9 22 4
3 3 1/06/2015 28/06/2015 9 22 5
4 4 1/06/2015 28/06/2015 9 17 4
And another tables theatre:
THEATRE_ID THEATRE_DESCRIPTION THEATRE_TOTAL_ROWS
1 2
2 2
3 3
4 2
There is a total of 18 screenings per day. I have to insert the details in the screening table as follows:
screening_id plan_id theatre_id screening_date screening_start_hh24 screening_start_mm60
1 1 3 1/06/2015 9 0
2 1 3 1/06/2015 11 30
3 1 3 1/06/2015 14 0
4 1/06/2015
plan_id is a foreign key referring table screening and theatre_id is a foreign key referring table theatre.
Each movie should be screened as per the screening number is defined
in the table screening_plan.
There is a break of 30 minutes between 2 consecutive screenings in
the same theatre.
The screening_start_hh24 should be less than plan_max_start_hh24.
Please note that the number of screenings for the first movie won't
fit into the provided time interval,so the second screening should be done
in an alternate theatre(preferably in theatre_id=2 starting from 11:30).
Each movie has a lenght of 2 hours.
I am stuck with this since yesterday. Tried doing it using the If-Else block, but that requires defining every condition. How can I do this using a loop?Please help.
My code(I have skipped the declaration part here):
BEGIN
SELECT plan_id INTO s_plan_id FROM screening_plan WHERE plan_id=1;
SELECT theatre_id INTO s_theatre_id FROM theatre WHERE theatre_id=1;
SELECT PLAN_START_DATE INTO s_screening_date FROM screening_plan WHERE plan_id=1;
SELECT Count(*) INTO s_count_theatre_id FROM screening;
IF s_count_theatre_id = 0
THEN
s_screening_start_hh24:=9;
s_screening_start_mm60:=0;
ELSIF s_count_theatre_id >0 AND s_count_theatre_id <=4
THEN
s_screening_start_hh24:=11 ;
s_screening_start_mm60:=30 ;
ELSE
Dbms_Output.put_line('---');
END IF;
INSERT INTO screening (plan_id, theatre_id, screening_date, screening_start_hh24, screening_start_mm60)
VALUES( s_plan_id,
s_theatre_id,
s_screening_date,
s_screening_start_hh24,
s_screening_start_mm60);
END;
There should be a total of 18 record in the table screening.5 for movie_id=1, 4 for movie_id=2, 5 for movie_id=3 and 4 for movie_id=3.

SAS Sorting within group

I would like to try and sort this data by descending number of events and from latest date, grouped by ID
I have tried proc sql;
proc sql;
create table new as
select *
from old
group by ID
order by events desc, date desc;
quit;
The result I currently get is
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
2 09/11/2015 2
3 01/01/2015 2
2 16/10/2014 2
3 08/12/2013 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Although the dates and events are sorted descending. Those IDs with multiple events are no longer grouped.
Would it be possible to achieve the below in fewer steps?
ID Date Events
1 09/10/2015 3
1 27/06/2014 3
1 03/01/2014 3
3 01/01/2015 2
3 08/12/2013 2
2 09/11/2015 2
2 16/10/2014 2
4 08/10/2015 1
5 09/11/2014 1
6 02/02/2013 1
Thanks
It looks to me like you're trying to sort by descending event, then by either the earliest or latest date (I can't tell which one from your explanation), also descending, and then by id. In your proc sql query, you could try calculating the min or max of the Date variable, grouped by event and id, and then sort the result by descending event, the descending min/max of the date, and id.

Resources