Query create table as with inner join select and group by (ORACLE) - oracle

This is my query, if i run the error is : Error in query: ORA-00907: missing right parenthesis, anybody can solve my problem?
Create table r_tcash_loci_act_tmp AS (
SELECT DISTINCT
R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI,
R_TCASH_ACT_MSISDN.MSISDN AS MSISDN_ACT,
R_TCASH_LOCI_ACT.AREA,
R_TCASH_LOCI_ACT.REGIONAL,
R_TCASH_LOCI_ACT.BRANCH,
R_TCASH_LOCI_ACT.SUB_BRANCH,
R_TCASH_LOCI_ACT.CLUSTERX,
R_TCASH_LOCI_ACT.UPDATED,
R_TCASH_ACT_MSISDN.DAILY,
R_TCASH_ACT_MSISDN.TOTAL_TRX,
R_TCASH_ACT_MSISDN.TOTAL_VOL
FROM R_TCASH_ACT_MSISDN
INNER JOIN R_TCASH_LOCI_ACT
ON R_TCASH_LOCI_ACT.MSISDN = R_TCASH_ACT_MSISDN.MSISDN
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
HAVING COUNT(R_TCASH_LOCI_ACT.MSISDN ) > 10);

It's a simple syntax error. You included the column alias in the GROUP BYclause (probably a cut'n'paste error).
GROUP BY R_TCASH_LOCI_ACT.MSISDN AS MSISDN_LOCI
So just remove the AS MSISDN_LOCI.
Although given that you have a DISTINCT clause and no aggregated columns it's a mystery why you have the GROUP BY at all. You should remove the whole line.

Related

INNER JOIN two table get error SQL Statment not ended properly

I have two table which I want to select Name from t2.
The situation is following.
I have t1 Policy which containt EmployeeID
And table t2 which containt Name
Now I want to select which Employee release policy.
So in t1(Policy- AUTO.POL) I have column: SIFRA_RAD
and t2(Employee-AUTO.SIFRAD) I have colum: SIFRA_R, Ime
I try something like this:
select auto.pol.sifra_rad, auto.sifrad.ime
from auto.sifrad
inner join auto.pol on auto.sifrad.ime = auto.pol.sifra_rad;
After that I get error
ORA-00933: SQL command not properly ended
I have no idea what is wrong here. Any suggestion?
The problem comes from query, and we fix it
select p.sifra_rad, s.ime from auto.sifrad s, auto.pol p where s.ime = p.sifra_rad

Update table with values of another table

I'm trying to update a table via subquery but i get following error:
ORA-01427: single-row subquery returns more than one row
01427. 00000 - "single-row subquery returns more than one row"
I would like to insert the content of a third table in the column.
My query:
UPDATE GH212_TABLE1
SET GH212_LINK = (SELECT GH201_TABLE1.GH201_ATTRIBUTEX
FROM GH210_TABLE3
LEFT JOIN GH201_TABLE1
ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
INNER JOIN GH212_TABLE1
ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID
GROUP BY GH201_ATTRIBUTEX)
WHERE GH212_TABLE1.GH212_ATTRIBUTEY='11';
I'm not sure how to link the tables right, so that i get the attribute from one specific object.
Any help would be much appreciated.
Cheers,
Fabi
EDIT: Thank you for your reply! I was well of the Problem with the multiple rows, but i somehow couldn't resolve it. Even when i tried the distinct query. But the solution from William Robertson seems to work, thanks a lot!!
A bit of a guess as I don't have your data or business logic, but perhaps you wanted something more like the following. I've removed the second instance of gh212_table1 from the subquery and instead linked t3 back to the table being updated:
update gh212_table1 t
set gh212_link =
( select distinct t1.gh201_attributex
from gh210_table3 t3
left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id
where t3.gh210_id = t.gh212_gh210_id )
where gh212_table1.gh212_attributey = '11';
There could still be a problem with this, though, if there are rows in gh212_table1 for which the subquery finds no row, as for those rows gh212_link will be set to null. If that is a problem, you might try rewriting it as a merge:
merge into gh212_table1 tgt
using ( select distinct t3.gh210_id, t1.gh201_attributex
from gh210_table3 t3
left join gh201_table1 t1 on t1.gh201_id = t3.gh210_gh201_id ) src
on ( src.gh210_id = tgt.gh212_gh210_id )
when matched then update
set tgt.gh212_link = src.gh201_attributex;
As you are fetching gh201_attributex from an outer join, it will be null whenever there is no row in gh201_table1 for a gh210_table3 row. Is that what you want? In the merge version, if you make it an inner join then the merge will only be applied where a row exists.
SELECT GH201_TABLE1.GH201_ATTRIBUTEX
FROM GH210_TABLE3
LEFT JOIN GH201_TABLE1
ON GH210_TABLE3.GH210_GH201_ID = GH201_TABLE1.GH201_ID
INNER JOIN GH212_TABLE1
ON GH212_TABLE1.GH212_GH210_ID = GH210_ID
WHERE GH212_TABLE1.GH212_GH210_ID=GH210_TABLE3.GH210_ID
GROUP BY GH201_ATTRIBUTEX
This query may give you multiple row values, due to this you are getting this exception
ORA-01427: single-row subquery returns more than one row 01427. 00000 - "single-row subquery returns more than one row"
Ensure that your sub-query return single row value.

SQL: ORA-00918 column ambiguously defined in INNER JOIN

I'm getting this error no matter what I do with the INNER JOIN Statement
Here is my code:
SELECT Package_Code, Description, Duration, Site_Code
FROM tbl_Holiday_Details
INNER JOIN tbl_Site_Visted
ON tbl_Holiday_Details.Package_Code = tbl_Site_Visted.Package_Code
INNER JOIN tbl_Site_Visted
ON tbl_Site_Details.Site_Code = tbl_Site_Visted.Site_Code
I don't understand what is the problem.
ps. if needed i will provide more code
The immediate problem is that at least Package_Code and Site_Code exist in multiple tables but your select does not specify which table you want to return data from. Yes, you know that you're doing an inner join on those columns so it doesn't matter which table's value is returned but the SQL syntax doesn't allow Oracle to make that inference. Generally, I would advise that you always alias every column both so it is clear which table a particular attribute is coming from and so that you don't break code when you add an attribute to a different table that happens to have the same name.
SELECT tbl_Holiday_Details.Package_Code,
Description,
Duration,
tbl_Site_Visted.Site_Code
FROM tbl_Holiday_Details
INNER JOIN tbl_Site_Visted
ON tbl_Holiday_Details.Package_Code = tbl_Site_Visted.Package_Code
INNER JOIN tbl_Site_Visted
ON tbl_Site_Details.Site_Code = tbl_Site_Visted.Site_Code
will work assuming Description and Duration are defined only in one of the three tables. I would add aliases to Description and Duration as well but I don't know which of the tables should be used. Of course, I would generally use simpler aliases (say, tsv for tbl_Site_Visited) rather than the full table name.
If you want to avoid aliasing your columns, you could use the USING clause rather than the ON clause
SELECT Package_Code,
Description,
Duration,
Site_Code
FROM tbl_Holiday_Details
INNER JOIN tbl_Site_Visted
USING( Package_Code )
INNER JOIN tbl_Site_Visted
USING( Site_Code )

Unable to get a expected output using hive aggregate function

I have a created a table (movies) in Hive as below(id,name,year,rating,views)
1,The Nightmare Before Christmas,1993,3.9,4568
2,The Mummy,1932,3.5,4388
3,Orphans of the Storm,1921,3.2,9062
4,The Object of Beauty,1991,2.8,6150
5,Night Tide,1963,2.8,5126
6,One Magic Christmas,1985,3.8,5333
7,Muriel's Wedding,1994,3.5,6323
8,Mother's Boys,1994,3.4,5733
9,Nosferatu: Original Version,1929,3.5,5651
10,Nick of Time,1995,3.4,5333
I want to write a hive query to get the name of the movie with highest views.
select name,max(views) from movies;
but it gives me an error
FAILED: Error in semantic analysis: Line 1:7 Expression not in GROUP BY key name
but doing a group by with name gives me the complete list (which is expected).
What changes should I make to my query?
It is very possible that there is a simpler way to do this.
select name
from(
select max(views) as views
, name
, row_number() over (order by max(views) desc) as row_num
from movies
group by name
) m
where row_num = 1
After little bit of digging, I found out that the answer is not so straightforward as we do in SQL. Below query gives the expected result.
select a.name,a.views from movies a left semi join(select max(views) views from movies)b on (a.views=b.views);

Hive LATERAL VIEW and WHERE Clause using Sub query

I'm looking for a way to optimize my query.
We have a table with events called lea, with a column app_properties, which are tags, stored as a comma separated string.
I would like to select all the events that match the result of a query that select the desired tags.
My first try:
SELECT uuid, app_properties, tag
FROM events
LATERAL VIEW explode(split(app_properties, '(, |,)')) tag_table AS tag
WHERE tag IN (SELECT source_value FROM mapping WHERE indicator = 'Bandwidth Usage')
But Hive will not allow this...
FAILED: SemanticException [Error 10249]: Line 4:6 Unsupported SubQuery Expression 'tag': Correlating expression cannot contain unqualified column references.
Gave it another try by replacing WHERE tag IN by WHERE tag_table.tag IN but not luck...
FAILED: SemanticException Line 4:6 Invalid table alias tag_table' in definition of SubQuery sq_1 [tag_table.tag IN (SELECT source_value FROM mapping WHERE indicator = 'Bandwidth Usage')] used as sq_1 at Line 4:20.
In the end... The query below gives the desired result, but I've a feeling that this is not the most optimized way of solving this use case. Has anyone ran into the same use case where you need the select from a LATERAL VIEW using a Sub query?
SELECT to_date(substring(events.time, 0, 10)) as date, t2.code, t2.indicator, count(1) as total
FROM events
LEFT JOIN (
SELECT distinct t.uuid, im.code, im.indicator
FROM mapping im
RIGHT JOIN (
SELECT tag, uuid
FROM events
LATERAL VIEW explode(split(app_properties, '(, |,)')) tag_table AS tag
) t
ON im.source_value = t.tag AND im.indicator = 'Bandwidth Usage'
WHERE im.source_value IS NOT NULL
) t2 ON (events.uuid = t2.uuid)
WHERE t2.code IS NOT NULL
GROUP BY to_date(substring(events.time, 0, 10)), t2.code, t2.indicator;
The Hive subquery in the WHERE clause can be used with IN, NOT IN, EXIST, or NOT
EXIST as follows. If the alias (see the following example for the employee table) is not specified before columns (name) in the WHERE condition, Hive will report the error Correlating expression cannot contain unqualified column references. This is a limitation of the Hive subquery.
From Apache Hive Essentials.
I guess this problem is also caused by subquery.
events should have an alias

Resources