Elasticsearch Join columns from different index with condition - sql

I have 2 different indexes in elasticsearch, indx1 and indx2 which i have indexed from an SQL Database using river plugin.
indx1
----------
id | Amt
1 2
2 3
3 2
1 9
2 4
----------
indx 2
----------
id | Name
1 Alex
2 Joe
3 MARY
----------
I want to create a new index now which calculate the average amount from indx1 and join everything in a single index.
So the final structure of the index should look like
indx_final
----------
id | Name | Avg Amt | Status
1 Alex 5.5 High
2 Joe 3.5 Med
3 Mary 2.0 Low
----------
The status is set according to average amount , if Avg amt > 4 , status = high, if avg amt >3, status = Med, If avg amt <2.5 ,status = Low.
Is this possible to do in elasticsearch only? If not possible i would have to do the calculation in SQL and then index the data again.
Any help would be appreciated. Thanks!

Related

Count values separately until certain amount of duplicates SQL

I need a Statement that selects all patients and the amount of their appointments and when there are 3 or more appointments that are taking place on the same date they should be counted as one appointment
That is what my Statement looks so far
SELECT PATSuchname, Count(DISTINCT AKTDATUM) AS AKTAnz
FROM tblAktivitaeten
LEFT OUTER JOIN tblPatienten ON (tblPatienten.PATID=tblAktivitaeten.PATID)
WHERE (AKTDeleted<>'J' OR AKTDeleted IS Null)
GROUP BY PATSuchname
ORDER BY AKTAnz DESC
The result should look like this
PATSuchname Appointments
----------------------------------------
Joey Patner 13
Billy Jean 15
Example Name 13
As you can see Joey Patner has 13 Appointments, in the real table though he has 15 appointments but three of them have the same Date and because of that they are only counted as 1
So how can i write a Statement that does exactly that?
(I am new to Stack Overflow, sorry if the format I use is wrong and tell me if it is.
In the table it looks like this.
tblPatienten
----------
PATSuchname PATID
------------------------
Joey Patner 1
Billy Jean 2
Example Name 3
tblAktivitaeten
----------
AKTDatum PATID AKTID
-----------------------------------------
08.02.2021 1 1000 ----
08.02.2021 1 1001 ---- So these 3 should counted as 1
08.02.2021 1 1002 ----
09.05.2021 1 1003
09.07.2021 2 1004 -- these 2 shouldn't be counted as 1
09.07.2021 2 1005 --
Two GROUP BY should do it:
SELECT
x.PATID, PATSuchname, SUM(ApptCount)
FROM (
SELECT
PATID, AKTDatum, CASE WHEN COUNT(*) < 3 THEN COUNT(*) ELSE 1 END AS ApptCount
FROM tblAktivitaeten
GROUP BY
PATID, AKTDatum
) AS x
LEFT JOIN tblPatienten ON tblPatienten.PATID = x.PATID
GROUP BY
x.PATID, PATSuchname

delete multiple rows from different tables on oracle

I have a two table.One of them is student, the other one is salary.
Student table
id | name | code | status
1 | steven | 123 | 100
2 | joe | 678 | 200
3 | paul | 758 | 100
Salary table
id | code | status | currency
1 | 123 | 100 | euro
2 | 678 | 200 | dolar
3 | 758 | 520 | yuan
I want to delete row1 from Student table and row 1 and 2 from Salary table because code and status fields
are same.
I write that query
delete a,b Student as a , join Salary as b
on a.code= b.code and a.status = b.status
but it is not working.I want to delete rows with one query.Do you have any idea?
Would something like this do? PL/SQL, though, not SQL.
Initial data sets:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
1 steven 123 100
2 joe 678 200
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
1 123 100 euro
2 678 200 dollar
3 758 520 yuan
Remove common (CODE, STATUS) combinations:
SQL> begin
2 for cur_r in (select code, status from student
3 intersect
4 select code, status from salary
5 )
6 loop
7 delete from student where code = cur_r.code and status = cur_r.status;
8 delete from salary where code = cur_r.code and status = cur_r.status;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
Result:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
3 758 520 yuan
SQL>
You can use two statements to do so easily:
delete student s where exists(
select * from student stu inner join salary sal on stu.code=sal.code and stu.status=sal.status and stu.id=s.id);
delete salary sal where not exists (select code from student stu where stu.code=sal.code);
First one to delete all the students having same code and status in both tables and second is to delete all the rows from salary table where code doesn't exist in student table.

sql how to select the lack of a condition?

I have this table structure and data for keeping track of horse race results:
T_RACE_HISTORY
==============
HORSE_ID RACE_DT PLACE
-------- ---------- -----
1 2014-05-03 1
1 2014-07-22 1
1 2016-06-10 3
2 2016-06-10 2
3 2016-06-10 1
I want a query that returns each unique horse id and either the date of the latest race won by that horse, or null if the horse has never won.
In other words, I want a query with this output:
HORSE_ID RACE_DT
-------- ----------
1 2014-07-22
2 (null)
3 2016-06-10
I can get the winning horses with a query like this:
SELECT HORSE_ID,
MAX(RACE_DT)
FROM T_RACE_HISTORY
WHERE PLACE = 1
GROUP BY HORSE_ID
But I have no idea how to look for the lack of any won races.
You can use conditional aggregation:
select horse_id,
max(case when place = 1 then race_dt end)
from t_race_history
group by horse_id
SQL Fiddle Demo

Execute an SQL UPDATE using GROUP BY and COUNT

I am working with SQL in an SQLite database. I have a table that looks something like this:
STORAGE
------------------------------
REC_ID SEQ_NO NAME
------------------------------
100 1 plastic jar
100 2 glass cup
100 fiber rug
101 1 steel fork
101 wool scarf
102 1 leather boots
102 2 paintbox
102 3 cast iron pan
102 toolbox
Keep in mind that that this is a very small number of records compared to what I actually have in the table. What I need to do is update the table so that all the records that have a null value for SEQ_NO are set with the actual number they are supposed to be in sequence to the group of records with the same REC_ID.
Here is what I want the table to look like after the update:
STORAGE
------------------------------
REC_ID SEQ_NO NAME
------------------------------
100 1 plastic jar
100 2 glass cup
100 3 fiber rug
101 1 steel fork
101 2 wool scarf
102 1 leather boots
102 2 paintbox
102 3 cast iron pan
102 4 toolbox
so for example, the record with REC_ID 102 should have have SEQ_NO of 4, because it is the fourth record with the REC_ID 102.
If I do:
SELECT REC_ID, COUNT(*) FROM STORAGE GROUP BY REC_ID;
this returns all of the records by REC_ID and the number (count) of records matching each ID, which would also be the number I would want to assign to each of the records with a null SEQ_NO.
Now how would I go about actually updating all of these records with their count values?
this should work:
update storage set
seq_no=(select count(*) from storage s2 where storage.rec_id=s2.rec_id)
where seq_no is null

SQL sort that distributes results

Given a table of products like this:
ID Name Seller ID Updated at
-- ---- --------- ----------
1 First 3 2012-01-01 12:00:10
2 Second 3 2012-01-01 12:00:09
3 Third 4 2012-01-01 12:00:08
4 Fourth 4 2012-01-01 12:00:07
5 Fifth 5 2012-01-01 12:00:06
I want to construct a query to sort the products like this:
ID
---
1
3
5
2
4
In other words, the query should show most recently updated products, distributed by seller to minimize the likelihood of continuous sequences of products from the same seller.
Any ideas on how to best accomplish this? (Note that the code for this application is Ruby, but I'd like to do this in pure SQL if possible).
EDIT:
Note that the query should handle this case, too:
ID Name Seller ID Updated at
-- ---- --------- ----------
1 First 3 2012-01-01 12:00:06
2 Second 3 2012-01-01 12:00:07
3 Third 4 2012-01-01 12:00:08
4 Fourth 4 2012-01-01 12:00:09
5 Fifth 5 2012-01-01 12:00:10
to produce the following results:
ID
---
5
4
2
3
1
One option demonstrated in this sqlfiddle is
select subq.*
from (
select rank() over (partition by seller_id order by updated_at desc) rnk,
p.*
from products p) subq
order by rnk, updated_at desc;