ON clause in HIVE (for version < 0.14) - mysql

I have tried running below query in Hive but it is showing following error:
" Both left and right aliases encountered in JOIN 'eff_start_dt' ". I think there is a problem with ON clause in below hive query.
I have tried running the same query on Teradata and it runs perfectly fine.
So my question is:
What all alzebraic expression we can use in 'ON' clause in hive?
How can I make my below query run in hive?
SELECT
q.calendar_dt as calendar_dt ,
x.corp_id as corp_id ,
x.mkt_cd as mkt_cd ,
x.bill_curr_cd as bill_curr_cd
FROM
corpmis_daily_time_dim as q
INNER JOIN corpmis_daily_global_corp_daily_expsr as x
INNER JOIN (select max(eff_start_dt) as max_eff_start_dt from corpmis_daily_global_corp_daily_expsr) as y
ON
q.calendar_dt between x.eff_start_dt and x.eff_end_dt WHERE
q.calendar_dt <= y.max_eff_start_dt;

Related

Issue with a subquery MySQL 5.5 vs 5.6

I have been running a website on MySQL 5.5 (percona) and using this query, which works:
SELECT
bds.FromStationPositionID
FROM
BusDestinationSegments AS bds
LEFT JOIN BusDestinationSegmentPrices AS bdsp ON bds.SegmentID = bdsp.BusDestinationSegmentID
AND EXISTS (
SELECT
BusDestinationScheduleID
FROM
BusDestinationSchedule
WHERE
BusDestinationScheduleDate + INTERVAL bdssf.DayOfset DAY = '2016-01-26'
),
BusDestinationStations AS bdssf
I needed to upgrade to 5.6 (again Percona) so I just did (on a development server).
However, this query now does not work. It tells me:
[Err] 1054 - Unknown column 'bdssf.DayOfset' in 'where clause'
I should add that I've checked and the column is there.
I'm guessing there is some kind of change from 5.5 to 5.6 which does not allow me to use columns in sub-queries, but I can't find anything on the topic.
Has anyone had any experience with this or can anyone suggest a way that what I'm trying to accomplish will work ?
You appear to be using a mix of old and new JOIN syntax. And expecting BusDestinationStations (which is bdssf from which the DayOfset column comes) to CROSS JOIN.
This used to work but one of the changes in MySQL 5.6 is to the precedence of the joins:-
http://dev.mysql.com/doc/refman/5.6/en/join.html :-
Previously, the comma operator (,) and JOIN both had the same
precedence, so the join expression t1, t2 JOIN t3 was interpreted as
((t1, t2) JOIN t3). Now JOIN has higher precedence, so the expression
is interpreted as (t1, (t2 JOIN t3)). This change affects statements
that use an ON clause, because that clause can refer only to columns
in the operands of the join, and the change in precedence changes
interpretation of what those operands are.
So I think what is happening is that MySQL is trying to perform the processing of the sub query before the implicit join to BusDestinationStations, and as such it doesn't know anything about bdssf.DayOfset when the sub query is executed.
Think the following would be the equivalent of what you are trying to do:-
SELECT bds.FromStationPositionID
FROM BusDestinationSegments AS bds
CROSS JOIN BusDestinationStations AS bdssf
LEFT JOIN BusDestinationSegmentPrices AS bdsp ON bds.SegmentID = bdsp.BusDestinationSegmentID
AND EXISTS
(
SELECT BusDestinationScheduleID
FROM BusDestinationSchedule
WHERE BusDestinationScheduleDate + INTERVAL bdssf.DayOfset DAY = '2016-01-26'
)
(although I am a bit dubious about doing a cross join like that just to get values for the sub query - looks very inefficient).

How can I execute the MSSQL query without error?

I'm trying to execute mssql query but its showing error.The same query I executed in MySQL its working fine.
The Query is:
SELECT tst_flow_name, tst_flow_desc,COUNT(tst_flow) tot
FROM test_flow_details
LEFT OUTER JOIN tst_flow ON tst_flow_name=tst_flow
AND test_flow_details.project=tst_flow.project
WHERE test_flow_details.project='JupiterQA'
ERROR IS:
Column 'test_flow_details.tst_flow_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
How can I execute the MSSQL query without error.
You can't mix normal column selects with aggregate function call like count().
Group by the columns you want to be unique and then you can add count()
SELECT tst_flow_name, tst_flow_desc, COUNT(*) tot
FROM test_flow_details
LEFT OUTER JOIN tst_flow ON tst_flow_name=tst_flow
AND test_flow_details.project=tst_flow.project
WHERE test_flow_details.project='JupiterQA'
GROUP BY tst_flow_name, tst_flow_desc

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1

Convert MS Access "TOP" to MySQL "LIMIT" in subquery

I am trying to convert an MS Access query to MySQL and the problem is converting MS Access top to MySQL limit to get the same result. When I change query to limit I get the error that this version of MySQL does not support limit in subquery.
This is the MS Access query:
SELECT a.FK_CONTRIBUTOR_ID
FROM tPUBLISHERS
INNER JOIN (tCONTRIBUTORS AS b
INNER JOIN tCLIPS AS a ON b.CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID)
ON tPUBLISHERS.PUBLISHER_ID = b.FK_PUBLISHER_ID
WHERE ((a.CLIP_ID) In
(select top 5 CLIP_ID
from tCLIPS
where FK_CONTRIBUTOR_ID = a.FK_CONTRIBUTOR_ID
AND SUSPEND = a.SUSPEND))
AND ((a.FK_CONTRIBUTOR_ID) In (1922,2034,2099))
Previously answered at:
MySQL Subquery LIMIT
basically change the subquery to a Join
Google for more with "mysql limit on subquery"

Self-referencing updates in HQL

I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.