hive sql join statment - hadoop

I have a query
with ex as (
select z.num number, car.id as id, car.make as make, car.model, 'vehicles' as type from mytable mt LATERAL VIEW EXPLODE(mt.vehicles) vehiclestbl as honda
where car.working = true
union all
select z.num number, plane.id as id, plane.make as make, plane.model, 'flights' as type from mytable mt LATERAL VIEW EXPLODE(mt.flights) flightstbl as wings
where plane.active = true
union all
select z.num number, train.id as id, train.make as make, train.model, 'departures' as type from mytable mt LATERAL VIEW EXPLODE(mt.departures) departurestbl as wheels
where wheels.active = true
)
select ex0.*, 1499 as cost, model as location
from ex ex0
where mycolumn = 15
The end results looks something like this
number id make model type cost location
323 abc make1 model1 type1 1499 modelLoc1
329 xyz make2 model2 type2 1499 modelLoc2
984 lks make3 model3 type3 1499 modelLoc3
I am trying to join a column 'number' with another outside table. When ever i add a
INNER JOIN myothertable mot on mot.id = ex_id;
I get an 'cannot recognize input near' Error

In the CTE, the column is called number so I would expect the query to look like to look like:
select ex0.*, 1499 as cost, model as location
from ex ex0 inner join
myothertable mot
on mot.id = ex0.number;
where mycolumn = 15;

Related

How can I join, aggregate, and pivot data that includes a BLOB column?

I'm trying to aggregate and pivot data from one table while joining two others. The third table contains a BLOB (which isn't allowed and causes ORA-00932: inconsistent datatypes: expected - got BLOB)
What I would like to run (but cannot):
SELECT * FROM (
select p.blah, p.result, p.type, s.blah, b.blob
from table p
join table2 s on s.id = p.id
join tableBlob b on b.id = s.id)
PIVOT (max(result) FOR (type) IN ('type1' as type1, 'type2' as type2))
where blah = 'value'
ORDER BY blah;
How might I achieve the desired results?

Error: column reference is ambiguous

I have two tables from which i want to get the date without using joins.
id ProductVersion productName productDate
1 p1.1 product1 2017-3-11
2 p1.2 product1 2017-3-11
3 p2.1 product2 2017-5-12
4 p2.2 product2 2017-5-12
5 p2.3 product2 2017-5-12
6 p3.1 product3 2017-11-21
7 p3.1 product3 2017-11-21
Table2
tid productVersion comments status AvailableDate
101 p1.1 Good Sold 2017-3-11
102 p1.1 Good Available 2017-3-12
1009 p1.1 Good Available 2017-3-12
4008 p3.1 Average NA 2017-11-11
106 p3.2 Good Sold 2017-5-14
6 p3.1 Average Available 2017-11-12
I have two tables as shown above.
I want to get productVersion,productName,productDate,Comments,status column details from the above two tables.
SQL Query(without joins):
select productversion t1,productName t1,productDate t1,comments t2,status t2 from table1 t1,table2 t2
where t1.productVersion = t2.productversion
Error message:
Error: column reference "productDate" is ambiguous.
Any inputs?
[TL;DR] Your main issue is that you appear to be putting the table aliases after the column name where an alias for the column is expected when they should be prefixing the column name to identify which table the columns belong to.
Your query is equivalent to:
select productversion AS columnalias1,
productName AS columnalias2,
productDate AS columnalias3,
comments AS columnalias4,
status AS columnalias5
from table1 t1,
table2 t2
where t1.productVersion = t2.productversion
And all your column aliases are either t1 or t2 so you will get multiple columns with the same name. I do not think this is what you intended as both tables have a productVersion column so the query parser does not know which you intended to use. You probably want the table aliases before the column name to identify which table each column is from:
select t1.productversion,
t1.productName,
t1.productDate,
t2.comments,
t2.status
from table1 t1,
table2 t2
where t1.productVersion = t2.productversion
The second problem is that, while you say it is a query "without joins", you are using a legacy Oracle comma-join syntax and your query can be rewritten to have exactly the same functionality using ANSI/ISO SQL syntax and is equivalent to:
select t1.productversion,
t1.productName,
t1.productDate,
t2.comments,
t2.status
from table1 t1
INNER JOIN table2 t2
ON ( t1.productVersion = t2.productversion )
If you want something without joins then use UNION ALL:
SELECT productVersion,
productName,
productDate,
NULL AS Comments,
NULL AS status
FROM table1
UNION ALL
SELECT productVersion,
NULL AS productName,
NULL AS productDate,
Comments,
status
FROM table2
But it will not correlate the values in the two tables.
To refer to a specific table column, you use this syntax:
table_name.column_name
Your query should be:
select t1.productversion, t1.productName, t1.productDate,
t2.comments, t2.status
from table1 as t1
join table2 as t2 on t1.productVersion = t2.productversion

Restructure Inline view

SELECT MAX(column1)
FROM table1 B , table2 A, table3 H
WHERE B.unit=A.unit
AND B.value=A.value
AND B.unit=H.unit
AND B.value=H.value
AND A.number=1234
Can someone help me to restructure this query in inline view?
SAMPLE
Table1
------
Value Unit
001 A1
002 B1
003 C2
002 A1
Table2
--------
Value Unit Number
001 B4 11
002 B1 1234
004 B1 22
TABLE3
-------
VALUE UNIT NUMBER COLUMN1
001 B4 11 555
002 B1 1234 557
002 B1 1234 559
OUTPUT
------
MAX(C0LUMN1)
-----------
559
In your query there is no need for inlineview :-
if that is rewritten in inlineview it will be like
Select Max(Column1)
From (Select Value,Unit From Table1)B,
(Select Value,Unit,Number From Table2)A,
Table3 as H
Where B.Unit=A.Unit
And B.Value=A.Value
AND B.unit=H.unit
And B.Value=H.Value
AND A.number=1234;
Below is the example when to use inline view hope this help!!!
An inline view is a SELECT statement in the FROM clause. As mentioned in the View section, a view is a virtual table that has the characteristics of a table yet does not hold any actual data. In an inline view construct, instead of specifying table name(s) after the FROM keyword, the source of the data actually comes from a view that is created within the SQL statement. The syntax for an inline view is,
SELECT "column_name" FROM (Inline View);
When should we use inline view? Below is an example:
Assume we have two tables: The first table is User_Address, which maps each user to a ZIP code; the second table is User_Score, which records all the scores of each user. The question is, how to write a SQL query to find the number of users who scored higher than 200 for each ZIP code?
Without using an inline view, we can accomplish this in two steps:
Query 1
CREATE TABLE User_Higher_Than_200
SELECT User_ID, SUM(Score) FROM User_Score
GROUP BY User_ID
HAVING SUM(Score) > 200;
Query 2
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM User_Higher_Than_200 a1, User_Address a2
WHERE a1.User_ID = a2.ZIP_CODE
GROUP BY a2.ZIP_CODE;
In the above code, we introduced a temporary table, User_Higher_Than_200, to store the list of users who scored higher than 200. User_Higher_Than_200 is then used to join to the User_Address table to get the final result.
We can simplify the above SQL using the inline view construct as follows:
Query 3
SELECT a2.ZIP_CODE, COUNT(a1.User_ID)
FROM
(SELECT User_ID, SUM(Score) FROM
User_Score GROUP BY User_ID HAVING SUM(Score) > 200) a1,
User_Address a2
WHERE a1.User_ID = a2.ZIP_CODE
GROUP BY a2.ZIP_CODE;
There are two advantages on using inline view here:
We do not need to create the temporary table. This prevents the database from having too many objects, which is a good thing as each additional object in the database costs resources to manage.
We can use a single SQL query to accomplish what we want
Notice that we treat the inline view exactly the same as we treat a table. Comparing Query 2 and Query 3, we see that the only difference is we replace the temporary table name in Query 2 with the inline view statement in Query 3. Everything else stays the same.
Inline view is sometimes referred to as derived table. These two terms are used interchangeably.
I need to show column from other table that have the max column value
SELECT MAX( H.column1 ) AS max_column1,
MAX( A.number ) KEEP ( DENSE_RANK LAST ORDER BY H.column1 ) AS max_number
FROM table1 B
INNER JOIN table2 A
ON ( B.unit = A.unit AND B.value = A.value )
INNER JOIN table3 H
ON ( B.unit = H.unit AND B.value = H.value )
WHERE A.number=1234

Oracle Join Two Queries With Distinct Answer

I need help in linking two tables together to bring back data as I need.
I have two queries:
The first is as follows:
SELECT
POI.PO_ID as PO_ID, SUM(POI.INV_QTY) AS INV_QTY, POI.INV_NUMBER,
PO.SUP_SUP_ID AS SUPPLIER
FROM TABLE1 POI, PUR_ORDS PO
WHERE POI.PO_ID = '56886' AND POI.PO_ID = PO.PO_ID
GROUP BY POI.PO_ID, POI.INV_NUMBER, PO.SUP_SUP_ID
ORDER BY POI.INV_NUMBER ASC
The Results of this query are as follows:
PO_ID|INV_QTY|INV_NUMBER|SUPPLIER
---------------------------------
56886| 105|INV1 |SUP1
56886| 106|INV2 |SUP1
The second query I have is this:
SELECT
DIL.PO_PO_ID, sum(DIL.ACPTD_QTY) as ACPTD_QTY,
DIL.DLVD_DLVY_NUMB AS DEL_NUM
FROM TABLE2 DIL
where DIL.PO_PO_ID = '56886'
GROUP BY PO_PO_ID, DIL.DLVD_DLVY_NUMB
order by del_num
The Results of this query are as follows:
PO_PO_ID|ACPTD_QTY|DEL_NUM
--------------------------
56886| 105| 1
56886| 106| 2
Now I am attempting to join the two tables, but I get multiple values appearing, using the following:
SELECT DISTINCT(PO_ID), INV_NUMBER, INV_QTY, SUPPLIER, ACPTD_QTY, DEL_NUM
FROM
(SELECT
SELECT POI.PO_ID as PO_ID, SUM(POI.INV_QTY) AS INV_QTY,
POI.INV_NUMBER, PO.SUP_SUP_ID AS SUPPLIER
FROM TABLE1 POI, PUR_ORDS PO
WHERE POI.PO_ID = '56886'
AND POI.PO_ID = PO.PO_ID
GROUP BY POI.PO_ID, POI.INV_NUMBER, PO.SUP_SUP_ID
ORDER BY POI.INV_NUMBER ASC) POINET
INNER JOIN
(SELECT DIL.PO_PO_ID, sum(DIL.ACPTD_QTY) as ACPTD_QTY,
DIL.DLVD_DLVY_NUMB AS DEL_NUM
FROM TABLE 2 DIL
where DIL.PO_PO_ID = '56886'
GROUP BY PO_PO_ID, DIL.DLVD_DLVY_NUMB
order by DEL_NUM) DILV
ON DILV.PO_PO_ID = POINET.PO_ID
GROUP BY PO_ID, INV_NUMBER, INV_QTY, SUPPLIER, ACPTD_QTY, DEL_NUM
ORDER BY INV_NUMBER
However the dataset I get is this:
PO_ID|INV_NUMBER |INV_QTY|SUPPLIER|ACPTD_QTY|DEL_NUM
-----------------------------------------------------
56886|K-101/2014-15| 105|SUP1 | 105| 1
56886|K-101/2014-15| 105|SUP1 | 106| 2
56886|K-107/2014-15| 106|SUP1 | 105| 1
56886|K-107/2014-15| 106|SUP1 | 106| 2
However I need it show the following:
PO_ID|INV_NUMBER |INV_QTY|SUPPLIER|ACPTD_QTY|DEL_NUM
------------------------------------------------------
56886|K-101/2014-15| 105|SUP1 | 105| 1
56886|K-107/2014-15| 106|SUP1 | 106| 2
What do I need to do, to tweak my query?
Any help would be much appreciated.
I think you just need to modify your join statement to join on both columns
ON DILV.PO_PO_ID = POINET.PO_ID
AND DILV.INV_QTY = POINET.ACPTD_QTY
Actually, you can probably leave out the PO_ID, because I it's the same in both subqueries:
i.e. Just do this (on the third last line):
ON DILV.INV_QTY = POINET.ACPTD_QTY

need help on sql query

am a newbie to Oracle/PL SQL.I've 2 tables A and B.
A has a column CustId,Age,Location and Date. Table B has 2 columns CustId,CustName.
What would be the sql query to show show CustName and Location for a given age?
Thanks.
your question "What would be the sql query to show show CustName and Location for a given age?" helps define your query pretty well:
SELECT CustName, Location
FROM TableA a
INNER JOIN TableB b
ON b.CustId = a.CustId
WHERE a.Age = #
All we need to do on top of that select for your specific fields is make sure to join the two tables on their common column (CustID).
Another option would be to avoid the WHERE statement:
SELECT CustName, Location
FROM TableB b
INNER JOIN TableA a
ON a.CustID = b.CustID
AND a.Age = #
you need join. something like
SELECT custname, location FROM a JOIN b ON a.custid = b.custid WHERE age = [age];

Resources