SQL: Filter self-loops in logistic movements - max

I'm working on a dataset containing logistic movements of stuff. In visualising the stuff we would like to filter out the movements from e.g. A to A (this madness occurs in the dataset).
Say I have a dataset looking like this: Equipment contains the id of the stuff that is moving, FROM and TO the stockrooms, the TIME_FROM when the stuff has been moved to the FROM stockroom, and TIME_TO when the stuff has been moved to the TO stockroom.
EQUIPMENT FROM_MAG TO_MAG TIME_FROM TIME_TO
1 A B 1 2
1 B C 2 3
1 C D 3 4
1 D D 4 5
1 D E 5 6
1 E F 6 7
1 F F 7 8
1 F F 8 9
1 F G 9 10
Than I would like an output from my query without the D-->D and two F-->F movements, but with a logical continuation of the time columns:
EQUIPMENT FROM_MAG TO_MAG TIME_FROM TIME_TO
1 A B 1 2
1 B C 2 3
1 C D 3 5
1 D E 5 6
1 E F 6 9
1 F G 9 10
I tried using queries like, but that does not give me the desired result. I'm working on SAP HANA by the way.
SELECT
EQUIPMENT,
FROM_MAG,
TO_MAG,
min(TIME_FROM),
max(TIME_TO)
FROM MOVEMENTS
GROUP BY EQUIPMENT,
FROM_MAG,
TO_MAG;
Create statement for SQL:
CREATE TABLE IF NOT EXISTS MOVEMENTS(
EQUIPMENT NVARCHAR(1) NOT NULL PRIMARY KEY
,FROM_MAG NVARCHAR(1) NOT NULL
,TO_MAG NVARCHAR(1) NOT NULL
,TIME_FROM NVARCHAR(1) NOT NULL
,TIME_TO NVARCHAR(2) NOT NULL
);
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'A',N'B',N'1',N'2');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'B',N'C',N'2',N'3');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'C',N'D',N'3',N'4');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'D',N'D',N'4',N'5');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'D',N'E',N'5',N'6');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'E',N'F',N'6',N'7');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'F',N'7',N'8');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'F',N'8',N'9');
INSERT INTO MOVEMENTS(EQUIPMENT,FROM_MAG,TO_MAG,TIME_FROM,TIME_TO) VALUES
(N'1',N'F',N'G',N'9',N'10');

Checked your query, I don't think there is need for FROM_MAG in GROUP BY. I checked this in MySql, giving desired result.
SELECT
EQUIPMENT,
FROM_MAG,
TO_MAG,
min(TIME_FROM),
max(TIME_TO)
FROM MOVEMENTS
GROUP BY EQUIPMENT,
TO_MAG

I used this, and now it works:
WITH A AS(
SELECT EQUIPMENT,
FROM_MAG,
TO_MAG,
TIME_FROM,
TIME_FROM,
TIME_TO
FROM MOVEMENTS
WHERE FROM_MAG<>TO_MAG
ORDER BY TO_NUMBER(TIME_TO))
SELECT EQUIPMENT,
FROM_MAG,
TO_MAG,
TIME_FROM,
IFNULL(LEAD(TIME_FROM) OVER(PARTITION BY EQUIPMENT ORDER BY TO_NUMBER(TIME_TO)),
TIME_TO) TIME_TO
FROM A;

Related

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;

Oracle PL/SQL- query a table, selecting data from the same table but utilizing an ID from another

I know the title will not do justice, and I have tried searching around between Joins and Merges, however I am a bit stumped and could use some guidance writing this.
I have two tables:
Table 1
A B C
20348 12306 191
31502 12306
20342 12297 191
31492 12297
20341 12296 191
31504 12296
20344 12299 191
31499 12299
Table 2
A(ident)B (F_Key of T1_A) C D E
25003 20348 1 2 3
35915 20342 1 2 3
41883 20341 1 2 3
31303 20344 1 2 3
I want to take table 2, select the contents B, C, D, E, However I want to select Table 1, column A as B where Table 1 C is null Table 1 B = X.
Table 2 Column B is the foreign Key of Table 1 A
As a result, I would like to see this:
A B C D E
5555 31502 1 2 3
5556 31492 1 2 3
5557 31504 1 2 3
5558 31499 1 2 3
Any help is much appreciated.
So if I understood your question correctly, you need case statement in your select:
select (case when t1.C is null and t1.b=X then t1.A else t2.B end), t2.C, t2.D, t2.E
from Table1 t1 join table2 t2 on t1.A = t2.B
Also not sure what Table 1 B = X means so I just kept the syntax same as in your explanation. Feel free to modify that part. Give it a try to see whether it works for you
I was able to figure this out for what I needed.
I end up going up a further level in my database where I introduced Table 3, where its Primary key was the Foreign Key of Table 1 Column B that referenced a primary key and created:
select t1b.A, t2.C, t2.D, t2.E
from Table1 t1a, table1 t1b, table2 t2, table3 t3
WHERE t3.A = t1a.b
AND t1a.C is null
AND t2.B = t1a.A
AND t1b.B = t3.A
AND t1b.B = 'xxx'

Oracle: How to get max value for each item in list

I have a very large table that I need to retrieve data from. The table looks like this:
A B C D
1 foo 4 q
1 fool 2 p
1 fools 13 a
2 pho 5 d
3 phone 14 g
3 phones 6 f
I'm trying to run something like:
select max(B) from table where A = 1 union
select max(B) from table where A = 2 union
.
.
.
select max(B) from table where A = 50000;
What I want is to get:
1 -> fools
2 -> pho
3 -> phones
I have about 50,000 records to run this query for.
This above approach theoretically works (I tried on a small subset), but I think it's inefficient to have one select query for each of the 50000 values.
This also resulted in a process out of memory error.
Is there a way I can run this in a single query?
I tried:
select max(B) from table where A in (first group of 1000) union
select max(B) from table where A in (1000...2000) union
.
.
.
select max(B) from table where A in (40000...50000)
But this gives me only one max value per select query (and I understand why)
What I really want is 50000 max values.
Is there a way for me to get the value of max(B) for each item in the list if I use
select max(B) from table where A in (...)
Thank you!
It looks like you just need to use GROUP BY, like this:
select A, max(B)
from table
group by A
order by A
Let me know if I'm missing something.

What if the value of order field is the same for all the records [duplicate]

This question already has answers here:
Why does Oracle return specific sequence if 'orderby' values are identical?
(4 answers)
Closed 7 years ago.
All, Let's say the SQL looks like below.
Select a, b ,c from table1 order by c
If all the rows in table1 have the same field value in the field c. I want to know if the result has the same order for each time I executed the SQL.
Let's say data in the table1 looks like below.
a b c
-------------------------------------------
1 x1 2014-4-1
....
100 x100 2014-4-1
....
1000 x1000 2014-4-1
....
How Oracle determine the rows sequence for the same order by value?
Added
Will they be random sequence for each time?
One simple answer is NO. There is no guarantee that the ORDER BY on equal values will return the same sorted result every time. It might seem to you it is always stable, however, there are many reasons when it could change.
For example, the sorting on equal values might defer after:
Gathering statistics
Adding an index on the column
For example,
Let's say I have a table t:
SQL> SELECT * FROM t ORDER BY b;
A B
---------- ----------
1 1
2 1
3 2
4 2
5 3
6 3
6 rows selected.
The sorting on the column having similar values is just like:
SQL> CREATE TABLE t1 AS SELECT * FROM t ORDER BY b, DBMS_RANDOM.VALUE;
Table created.
SQL> SELECT * FROM t1 ORDER BY b;
A B
---------- ----------
1 1
2 1
4 2
3 2
5 3
6 3
6 rows selected.
So, similar data in bot the tables, however, ORDER BY on the column having equal values, dos not guarantee the same sorting.
They must not be random (change each time), but the order is not guaranteed (change sometimes).

How to select two max value from different records that has same ID for every records in table

i have problem with this case, i have log table that has many same ID with diferent condition. i want to select two max condition from this. i've tried but it just show one record only, not every record in table.
Here's my records table:
order_id seq status____________________
1256 2 4
1256 1 2
1257 0 2
1257 3 1
Here my code:
WITH t AS(
SELECT x.order_id
,MAX(y.seq) AS seq2
,MAX(y.extern_order_status) AS status
FROM t_order_demand x
JOIN t_order_log y
ON x.order_id = y.order_id
where x.order_id like '%12%'
GROUP BY x.order_id)
SELECT *
FROM t
WHERE (t.seq2 || t.status) IN (SELECT MAX(tt.seq2 || tt.status) FROM t tt);
this query works, but sometime it gave wrong value or just show some records, not every records.
i want the result is like this:
order_id seq2 status____________________
1256 2 4
1257 3 2
I think you just want an aggregation:
select d.order_id, max(l.seq2) as seq2, max(l.status) as status
from t_order_demand d join
t_order_log l
on d.order_id = l.order_id
where d.order_id like '%12%'
group by d.order_id;
I'm not sure what your final where clause is supposed to do, but it appears to do unnecessary filtering, compared to what you want.

Resources