cannot recognize input near 'SELECT' 'DISTINCT' '(' in expression specification -Hive - hive

Trying to execute below query in hive. throwing exception cannot recognize input near 'SELECT' 'DISTINCT' '(' in expression specification. any help is appreciated.
SELECT DISTINCT (FISCAL_QUARTER_START_DATE)
FROM TEST_STAGE.TEST_TABLE
WHERE FISCAL_YEAR_ID =
(SELECT FISCAL_YEAR_ID
FROM TEST_STAGE.TEST_TABLE
WHERE CALENDAR_DATE = FROM_UNIXTIME (UNIX_TIMESTAMP ()))
AND SUBSTR (FISCAL_QUARTER_NAME, 0, 2) =
(SELECT SUBSTR (FISCAL_QUARTER_NAME, 0, 2)
FROM TEST_STAGE.TEST_TABLE
WHERE CALENDAR_DATE = FROM_UNIXTIME (UNIX_TIMESTAMP ()))
Thanks

Use proper table aliases and IN clause instead of = for sub-queries.
SELECT DISTINCT (t1.FISCAL_QUARTER_START_DATE)
FROM TEST_STAGE.TEST_TABLE t1
WHERE t1.FISCAL_YEAR_ID IN (
SELECT t2.FISCAL_YEAR_ID
FROM TEST_STAGE.TEST_TABLE t2
WHERE t2.CALENDAR_DATE = FROM_UNIXTIME(UNIX_TIMESTAMP())
)
AND SUBSTR(t1.FISCAL_QUARTER_NAME, 0, 2) IN (
SELECT SUBSTR(t3.FISCAL_QUARTER_NAME, 0, 2)
FROM TEST_STAGE.TEST_TABLE t3
WHERE t3.CALENDAR_DATE = FROM_UNIXTIME(UNIX_TIMESTAMP())
);
I think your query is equivalent and could be converted to a single sub-query with EXISTS.
SELECT DISTINCT t1.FISCAL_QUARTER_START_DATE
FROM TEST_STAGE.TEST_TABLE t1
WHERE EXISTS (
SELECT 1
FROM TEST_STAGE.TEST_TABLE t2
WHERE SUBSTR(t1.FISCAL_QUARTER_NAME, 0, 2) = SUBSTR(t2.FISCAL_QUARTER_NAME, 0, 2)
AND t1.FISCAL_YEAR_ID = t2.FISCAL_YEAR_ID
AND t1.CALENDAR_DATE = FROM_UNIXTIME(UNIX_TIMESTAMP())
);

Related

recursive query sqlite3.OperationalError: near "SELECT": syntax error

So I have the following recursive
WITH RECURSIVE length(len, id, ord) AS (
SELECT 0, t.id, 1
FROM temp t
UNION
SELECT len + compute(
SELECT w2.nodeid
FROM waypoint w2
WHERE w2.ordinal = ord AND w2.wayid =t.id,
SELECT w2.nodeid
FROM waypoint w2
WHERE w2.ordinal = ord + 1 AND w2.wayid =t.id
), t.id, w1.ordinal
FROM waypoint w1, length, temp t
WHERE w1.wayid = t.id AND w1.ordinal = ord + 1
),
SELECT *
FROM length
The issue is that I keep getting this error
sqlite3.OperationalError: near "SELECT": syntax error
compute is just a user defined sqlite function that takes two node ids to compute the distance between them.
You have a syntax error in FROM waypoint w1, length, , temp t two commas apeaer near each other.
Try this:
WITH RECURSIVE length(len, id, ord) AS (
SELECT 0 as len, t.id, 1 as ord
FROM temp t
UNION
SELECT (len + compute(
SELECT w2.nodeid
FROM waypoint w2
WHERE w2.ordinal = ord AND w2.wayid =t.id,
SELECT w2.nodeid
FROM waypoint w2
WHERE w2.ordinal = ord + 1 AND w2.wayid =t.id
) )as len, t.id, w1.ordinal as ord
FROM waypoint w1, length, temp t
WHERE w1.wayid = t.id AND w1.ordinal = ord + 1
),
SELECT *
FROM length

USING CASE AS PART OF JOIN ORACLE SQL

I have a query in Oracle SQL Developer to grab data from a raw table (all the data on the raw table are varchars) where I need to "clean" the data when I select it. Have to use a CASE statement to get the store number because sometimes that data isn't in the STORENBR column but can be found in a substring of another column -
SELECT
CASE WHEN m.STORENBR = '0' AND (SUBSTR(m.SENDING_QMGR, 1, 5) = 'PDPOS')
THEN TO_NUMBER((SUBSTR(m.SENDING_QMGR, 8, 4)))
WHEN m.STORENBR = '0' AND (SUBSTR(m.SENDING_QMGR, 1, 8) = 'PROD_POS')
THEN TO_NUMBER((SUBSTR(m.SENDING_QMGR, 9, 4)))
ELSE TO_NUMBER(NVL(m.STORENBR, '0'))
END AS STORENBR,
TO_NUMBER(NVL(m.CONTROLNBR,'0')) AS CONTROLNBR,
TO_NUMBER(NVL(m.LINENBR,'0')) AS LINENBR,
TO_DATE(m.TRANDATE,'YYYY-MM-DD') AS TRANDATE,
TO_NUMBER(NVL(m.NUMMISPRINTED,'0.00'),'99.99') AS NUMMISPRINTED
FROM MISPRINTS_RAW m
WHERE TO_DATE(m.TRANDATE,'YYYY-MM-DD') = '15-MAR-21'
ORDER BY m.STORENBR;
Now I need to also pull an account number from another table (TRANSACTIONS t - not a raw table, so I don't need any CASE or TO_NUMBER to pull data) but I need to join that table on STORENBR, CONTROLNBR, and LINENBR. So how do I use that CASE statement as part of the join to JOIN m.STORENBR on t.STORENBR?
Even if I am not sure about the data structure the following statement should be helpful.
The table with the raw data is "converted" in a subselect, so that a normal join is possible.
SELECT *
FROM (SELECT CASE
WHEN m.storenbr = '0'
AND ( Substr(m.sending_qmgr, 1, 5) = 'PDPOS' ) THEN
To_number(
( Substr(m.sending_qmgr, 8, 4) ))
WHEN m.storenbr = '0'
AND ( Substr(m.sending_qmgr, 1, 8) = 'PROD_POS' ) THEN
To_number(( Substr(m.sending_qmgr, 9, 4) ))
ELSE To_number(Nvl(m.storenbr, '0'))
END AS STORENBR,
To_number(Nvl(m.controlnbr, '0')) AS CONTROLNBR,
To_number(Nvl(m.linenbr, '0')) AS LINENBR,
To_date(m.trandate, 'YYYY-MM-DD') AS TRANDATE,
To_number(Nvl(m.nummisprinted, '0.00'), '99.99') AS NUMMISPRINTED
FROM misprints_raw m) m1,
TRANSACTION t
WHERE t.storenbr = m1.storenbr
AND t.controlnbr = m1.controlnbr
AND t.linenbr = m1.linenbr
AND m1.trandate = DATE '2021-03-15'
ORDER BY m1.storenbr;

how to run a different select statement based on condition in Hive SQL

I would like to know how to run a different select statement based on condition in Hive SQL.
The following query does not work but throws an error.
Error while compiling statement: FAILED: ParseException line 4:2
cannot recognize input near '(' 'SELECT' '1' in expression
specification
SELECT
CASE WHEN '${UN}'!= '' THEN
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in ('${UN}')
)
ELSE
(
SELECT *
from table1 t
WHERE t.yymmddval BETWEEN '${D1}' AND '${D2}'
AND t.un in (
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
)
)
END
Use UNION ALL with your queries + add conditions for switching corresponding query:
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' and '${D2}')
and t.un in ('${UN}')
and '${UN}'!= '' --switching condition
union all
select *
from table1 t
where (t.yymmddval BETWEEN '${D1}' AND '${D2}')
and t.un in
(SELECT
o.unq_num as un
FROM table2 as o
WHERE o.date >= '2017-01-01'
AND upper(o.srl_num) in ('${R}')
LIMIT 1)
and '${UN}'= '' --switching condition

sql zero leading in comparing 2 record set

I have a problem compare 2 set of data with and without leading zero.
In the Inventory_NBP, there is as 000000000000909120.
However, in PDM_Analysis, there exist only 909120 without the leading zero.
The current query below failed to retrieve any 000000000000909120 or 909120 as the "IN" condition is not met.
How do i modify the query below to fulfill my requirement?
sel * FROM Inventory_NBP.v_dmnd_rsrv_dpnd_rqr_mrp
WHERE plnt_id ='WA01'
and mtrl_id in('G29329-001', '000000000000909120', '13-0006-001')
and
(mtrl_id, plnt_id)
IN
( SELECT itm_cd, sap_plnt_cd
FROM PDM_Analysis.v_itm_plnt_extn
)
Do casting. This may solve your problem. Please check.
SELECT * FROM Inventory_NBP.v_dmnd_rsrv_dpnd_rqr_mrp
WHERE plnt_id ='WA01'
and mtrl_id in('G29329-001',CAST(CAST('000000000000909120' AS INT) AS VARCHAR(10)), '13-0006-001')
and
(mtrl_id, plnt_id)
IN
( SELECT itm_cd, sap_plnt_cd
FROM PDM_Analysis.v_itm_plnt_extn
)
Here's an example of how to match values regardless of leading zeros by padding (SQL Server 2008 syntax):
WITH T1
AS
(
SELECT *
FROM (
VALUES ('000000000000909120'),
('00000000099'),
('000000055'),
('22'),
('152')
) AS T (data_col)
),
T2
AS
(
SELECT *
FROM (
VALUES ('909120'),
('99'),
('00055'),
('0000000022'),
('152')
) AS T (data_col)
)
SELECT *
FROM T1 INNER JOIN T2
ON T1.data_col
= REPLICATE('0', LEN(T1.data_col) - LEN(T2.data_col))
+ T2.data_col
UNION
SELECT *
FROM T1 INNER JOIN T2
ON T2.data_col
= REPLICATE('0', LEN(T2.data_col) - LEN(T1.data_col))
+ T1.data_col;

Oracle Missing Right Parenthesis

Here is my query:
SELECT * FROM Auta WHERE SUBSTR(spz, 1, 2) =
(SELECT SUBSTR(spz, 1, 2) FROM Auta WHERE typ = 'BMW' AND specifikacia_typu = 'Z1' LIMIT 1);
And when I run it I get this error:
ORA-00907: missing right parenthesis
I'm getting a little desperate, I've already tried adding parentheses everywhere in the query and I still get the error? There are 3 left and 3 right parentheses in the query so everything should be all right.
The LIMIT clause doesn't exist in Oracle. Instead you would use rownum:
SELECT *
FROM Auta
WHERE SUBSTR(spz, 1, 2) = (SELECT SUBSTR(spz, 1, 2)
FROM Auta
WHERE typ = 'BMW'
AND specifikacia_typu = 'Z1'
AND ROWNUM = 1);
What's that "LIMIT 1" for? I don't believe that's correct Oracle syntax. If you're trying to limit the output to one row, use:
WHERE rownum = 1
LIMIT 1 is a MySQLism, as far as I know.
SELECT *
FROM Auta
WHERE SUBSTR(spz, 1, 2) =
(SELECT SUBSTR(spz, 1, 2)
FROM Auta
WHERE typ = 'BMW'
AND specifikacia_typu = 'Z1'
AND ROWNUM = 1);
...should give you the expected results