Hive like Operator - hive

[enter link description here][1]
Table1:
BRAND
Sony
Apple
Google
IBM
etc.
Table2:
hive> select * from PRODUCT;
SonyABC,Applepqr 101
Sonyadvv,IBMabc 102
Sonyaaaa 103
Apple123,Sonyxyz 104
Apple345,IBMabc 105
IBM13123SonyABC 106
My data is like above.and i need output like bellow.Please suggest a query
SonyABC,Applepqr 101 Sony,Apple
Sonyadvv,IBMabc 102 Sony,IBM
Sonyaaaa 103 Sony
Apple123,Sonyxyz 104 Apple,Sony
Apple345,IBMabc 105 Apple,IBM
IBM13123,SonyABC 106 IBM,Sony

May be some issue in your data, but this works as following
hive> select * from BRAND;
Sony
Apple
Google
IBM
hive> select * from PRODUCT;
SonyABC 101
Sonyadvv 102
Sonyaaaa 103
Apple123 104
Apple345 105
IBM13123 106
select PRODUCT.*, BRAND.* from BRAND, PRODUCT where trim(PRODUCT_ID) like concat(trim(BRAND_ID),'%');
SonyABC 101 Sony
Sonyadvv 102 Sony
Sonyaaaa 103 Sony
Apple123 104 Apple
Apple345 105 Apple
IBM13123 106 IBM
Time taken: 11.381 seconds, Fetched: 6 row(s)

Related

How to Capture Original "Start With" ID as a Column in Oracle SQL LEVEL Query

Working with a large database of material being consumed as "inputs" to create various "outputs" over multiple generations. The final output (the product for market) can have potentially a dozen+ generations of inputs in its history. Each of these inputs has its own record.
Currently I'm creating a top-down view of this genealogy (meaning starting with final product and working back through all inputs) using LEVEL.
Simplified/Conceptual example of the code as follows:
SELECT
OL.LOT_NAME AS output_id,
IL.LOT_NAME AS input_id,
LEVEL
FROM GENEALOGY_TABLE G
INNER JOIN LOT_TABLE OL
on G.OUTPUT_LOT_KEY = OL.LOT_KEY
INNER JOIN LOT_TABLE IL
on G.INPUT_LOT_KEY = IL.LOT_KEY
START WITH OL.LOT_NAME IN ('X', 'Y', etc...)
CONNECT BY NOCYCLE PRIOR IL.LOT_NAME = OL.LOT_NAME
ORDER BY LEVEL
I am looking to add another column to this output table that holds the original "START WITH" value that is the origin of any the given record. Meaning that even if the record has a level of 10, I won't just see the level 9 output that that material created, but which of the multiple final products ('X', 'Y', etc... in the above example) that was ultimately created downstream.
Does Oracle have a function that can handle this? Is there a simple trick for this I'm missing? Any suggestions would be wonderful.
You can use the connect_by_root operator:
SELECT
OL.LOT_NAME AS output_id,
IL.LOT_NAME AS input_id,
LEVEL,
CONNECT_BY_ROOT(OL.LOT_NAME) AS STARTED_WITH
FROM GENEALOGY_TABLE G
...
Quick demo using HR-schema tables:
SELECT employee_id, last_name, manager_id, connect_by_root(manager_id)
FROM employees
START WITH manager_id in (101, 102)
CONNECT BY PRIOR employee_id = manager_id;
EMPLOYEE_ID LAST_NAME MANAGER_ID CONNECT_BY_ROOT(MANAGER_ID)
----------- ------------------------- ---------- ---------------------------
108 Greenberg 101 101
109 Faviet 108 101
110 Chen 108 101
111 Sciarra 108 101
112 Urman 108 101
113 Popp 108 101
200 Whalen 101 101
203 Mavris 101 101
204 Baer 101 101
205 Higgins 101 101
206 Gietz 205 101
103 Hunold 102 102
104 Ernst 103 102
105 Austin 103 102
106 Pataballa 103 102
107 Lorentz 103 102
16 rows selected.
With Oracle there is always a way. Use CONNECT_BY_ROOT.

result is wrong when retrieving the date

I'm working with PostgreSQL. I have two database tables,i want to get the min and max date stored in table1 daterange column which is of type character varying. table1 and table2 is mapped using sid. i want to get the max and min date range of table1 when compared with sid of table2. Please find the demo here. The result is wrong.
table1:
sid daterange
100 5/25/2017
101 1/24/2017
102 4/4/2014
103 11/12/2007
104 4/24/2012
105 01/15/2017
106 1/1/2017
107 3/11/2016
108 10/10/2001
109 1/10/2016
110 12/12/2016
111 4/24/2017
112 06/28/2015
113 5/24/2017
114 5/22/2017
table2:
sid description
100 success
101 pending
104 pending
105 success
106 success
107 success
110 success
111 pending
112 failed
113 failed
114 pending
Below is my query:
select min(daterange) as minDate,max(daterange) as maxDate from (SELECT to_date(table1.daterange, 'DD/MM/YYYY') as daterange FROM table1,table2 where
table1.sid = table2.sid) tt;
The result is as below which is wrong(mindate and maxdate displayed are wrong dates).
mindate maxdate
2013-12-07 2019-01-07
Please advice. daterange column in table1 is of type character varying.I cannot use ::date to convert to date type, because i need to use this query in my java hibernate code and the java code is not recognizing ::
You have day and month mixed up in the date format string.
Should be
to_date(table1.daterange, 'MM/DD/YYYY')

SQL Query to order data based on other column value

I have the below set of data(current data), where system_id is the ID of the particular system. And pre_system_id's are ID of system where it is dependent. Now I need the order in such a way that rows with no dependent system should come first , then rows with one dependent system come second and so on.
The current result:
System_ID PRE_SYSTEM_ID1 PRE_SYSTEM_ID2 PRE_SYSTEM_ID3 PRE_SYSTEM_ID4
106 100
105
112 105 100 109
100
109 100 105
119 100 109 105 112
102 112 109
104 109 106
The actual result should be like below:
Order System_ID PRE_SYSTEM_ID1 PRE_SYSTEM_ID2 PRE_SYSTEM_ID3 PRE_SYSTEM_ID4
1 100
2 105
3 106 100
4 109 100 105
5 112 105 100 109
6 119 100 109 105 112
7 104 109 106
8 102 112 109 104
The query for the current result is simply
Select * from ImpactedSystem;
Sorting by the various PRE_SYSTEM_IDn columns using the nulls first clause should produce the order you want:
select *
from ImpactedSystem
order by PRE_SYSTEM_ID1 nulls first,
PRE_SYSTEM_ID2 nulls first,
PRE_SYSTEM_ID3 nulls first,
PRE_SYSTEM_ID4 nulls first,
SYSTEM_ID
Finally sort by SYSTEM_ID, to order the values with no dependent IDs.
you can use the below query to obtain the result as well.
select *
from Current_data
order by DECODE(pre_system_td1,null,1),
DECODE(pre_system_td2,null,1),
DECODE(pre_system_td3,null,1),
DECODE(pre_system_td4,null,1);

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo

How I select record that not appear in another table

Table: Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
104 E.T. 1982 Steven Spielberg
105 Titanic 1997 James Cameron
106 Snow White 1937 <null>
107 Avatar 2009 James Cameron
108 Raiders of the Lost Ark 1981 Steven Spielberg
Table: Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4 <null>
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4 <null>
206 107 3 2011-01-15
206 106 5 2011-01-19
207 107 5 2011-01-20
208 104 3 2011-01-02
I need to fetch movies which are not rate yet. In this case Titanic (mID 105) and Star Wars (mID 102) never get rate in rating table.
I figured out it with
select distinct movie.title from movie,rating where
rating.mid!=movie.mid except select distinct movie.title from
movie,rating where rating.mid=movie.mid
however I think it might have better (easier/cleaner) way to do.
Simple:
SELECT Movies.* FROM Movies LEFT JOIN Rating ON Movies.mID = Rating.mID WHERE Rating.mID IS NULL
If I understood your question properly, that looks like textbook application of outer joins.
You could do it like this:
SELECT * FROM Movie WHERE mid NOT IN (SELECT DISTINCT(mid) FROM Rating)
Basically it will select all records from the movie table that are not in the rating table, linking them on the 'mid' column, which I am assuming is a unique identifier.
I will add another possibility.
Select [list columns here]
from Movie m
where NOT exists (SELECT * FROM RATING r where m.mid = r.mid)