return value from nested sql - sql

I have the following SQL and i want theorder_item_wo_id column to return null if the item does not exist in the nested sql and return the value of a column oi.cop_workorder_id if it does.
How can i accomplish this?
SELECT
cop.wo_id,
CASE
WHEN EXISTS (SELECT oi.cop_workorder_id
FROM oseo_orderitem oi
WHERE oi.cop_workorder_id = cop.wo_id) THEN
oi.cop_workorder_id
ELSE null
END AS order_item_wo_id
FROM oseo_cop_wo cop
where cop.wo_id = '123';
It looks like the oi.cop_workorder_id does not get recognized outside of the nested sql.
ERROR: missing FROM-clause entry for table "oi"

Unless I am missing something, it seems you are overcomplicating this.
Just use a LEFT JOIN:
SELECT
cop.wo_id,
oi.cop_workorder_id AS order_item_wo_id
FROM oseo_cop_wo AS cop
LEFT JOIN oseo_orderitem AS oi
ON oi.cop_workorder_id = cop.wo_id
WHERE cop.wo_id = '123';

use can use the COALESCE function. It works similar to Oracle's NVL function. I don't have PostgreSQL available to write and test this, however.

Related

Ambigously defined column in a subquery

I've the following subquery in an sql query:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, NOMBRE AS NOMBREUNIDAD FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY ID_PLAN, ID_CURSO, NEDICION) ASIS
Problem I have I believe lies in that both table ALUMNOS and UNIDADES have a column named 'NOMBRE' so if I attempt to execute the query I obtain:
00000 - "column ambiguously defined"
To avoid that I thought about changing NOMBRE AS NOMBREUNIDAD to:
UNIDADES.NOMBRE AS NOMBREUNIDAD
But if I do that I get a:
00000 - "not a GROUP BY expression"
So, I don't know what to do so that subquery executes properly.
What should I change to properly execute query without changing the column name?
Aliases are pretty useful, if you use them. The simplify queries and make them easier to read and maintain. I'd suggest you to do so, as it'll also help query to work because Oracle doesn't know which table you actually meant when you selected those 4 columns - which tables do they belong to?
This is just a guess as I don't know your tables so you'll have to fix it yourself. Also, I literally JOINed tables; try to avoid comma-separating them in FROM clause and doing join in WHERE clause as it is supposed to filter data.
GROUP BY, as already commented, is probably useless. If you wanted to fetch distinct set of values, then use appropriate keyword: distinct.
SELECT DISTINCT n.id_plan,
s.id_curso,
u.nedicion,
u.nombre
FROM asisten n
JOIN alumnos s ON n.cod = s.cod
JOIN unidades u
ON u.idestructura = s.idestructura
AND u.cdundorg = s.cdundorg
WHERE UPPER (TRANSLATE (u.nombre, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
I managed to solve my problem:
(
SELECT ID_PLAN, ID_CURSO, NEDICION, UNIDADES.NOMBRE AS NOMBREUNIDAD
FROM ASISTEN, ALUMNOS, UNIDADES
WHERE ASISTEN.COD = ALUMNOS.COD AND UNIDADES.IDESTRUCTURA = ALUMNOS.IDESTRUCTURA
AND UNIDADES.CDUNDORG = ALUMNOS.CDUNDORG
AND UPPER(TRANSLATE(UNIDADES.NOMBRE, 'áéíóúÁÉÍÓÚ', 'aeiouAEIOU')) LIKE '%CONSEJERIA%'
GROUP BY UNIDADES.NOMBRE,ID_PLAN, ID_CURSO, NEDICION
)

SQL Query Returning Unwanted Data Which was Filtered on Where Statement

I've got the following query: When I execute the query, I get a lot of results for ad.Branch, including 106804. If I remove BOR.SSN like and COB.SSN like, and just leave branch, it returns only 106804 records which is the desired result. So I believe the like statement is what's causing the issues.
declare #Social varchar(5);
set #social = '%####'
SELECT
ABC.AccountNumber
FROM gcc.AccountDailyFact ABC
JOIN gcc.Today T ON ABC.CalendarDate = T.Today
JOIN gcc.ADReference AD ON ABC.ADReferenceRowId = AD.ADReferenceRowId
JOIN gcc.CDAccountData BOR ON ABC.BDLeaseHolderRowId = BOR.CDAccountDataRowId
JOIN gcc.CDAccountData COB ON ABC.CoBDLeaseHolderRowId = COB.CDAccountDataRowId
JOIN gcc.VDAssetFile VD ON ABC.VDAssetFileRowID = VD.VDAssetFileRowID
WHERE BOR.SSN like #social or COB.Ssn like #social and ad.Branch in
('106804')
I think you just need parentheses:
WHERE (BOR.SSN like #social or COB.Ssn like #social) and
ad.Branch in ('106804')

Using 'LIKE' in SQL Statement doesn't seem to work

I seem to be able to do a simple query that uses the equal sign, but am unable to find any results when I use the 'LIKE' function. Am I missing something here?
SELECT ISARPapers.*, AuthorList.FirstName, AuthorList.LastName
FROM AuthorList INNER JOIN (ISARPapers INNER JOIN PaperAuthor
ON ISARPapers.PaperID = PaperAuthor.PaperID)
ON AuthorList.AuthorID = PaperAuthor.AuthorID
WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
be carrefuly with capital letters
... WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
maybe Mianzi does not exists but mianzi does.
if you do not care about capitals, try:
... WHERE lower(ISARPapers.PaperTitle) LIKE '%mianzi%'

HIVE - hive subquery is not working with case when statement with IN clause

I am trying to migrate data from mysql to hive.I am not able to write a subquery case when statement with IN clause.This is my query. Can you Please help in this regard. AM i not following the proper syntax .
CREATE TABLE HIVE_TPCE_TEMP.TMP_CDMA_CD AS
SELECT A.DRI,C.BOUND_ID,A.CT_ID,A.CD_ID,A.CID,
A.TID,A.TASK_SEQ_ID,A.DIV_ID,C.BLOCK_GROUP_ID,C.ZIP_CODE,C.ROAD_CATEGORY_ID,A.RXPOWER,"${hiveconf:C_CDMA_DEVICE_ONLINE_RXPOWER_METRIC_ID}" METRIC_ID,
CASE WHEN
((A.DRI,A.DIV_ID,A.RFID) in (SELECT DRI,DIV_ID,HOME_RFID FROM HIVE_TPCE_TEMP.TMP_HOME_NETWORKS)) THEN
CASE WHEN MODE IN ('A','N') THEN "${hiveconf:HAD}" ELSE "${hiveconf:HD}" END
WHEN (COALESCE(A.RFID,0) = 0) AND ((A.DRI,A.DIV_ID,D.FR,D.SUBBAND) IN (SELECT DRI,DIV_ID,HOME_FR,
HOME_SUBBAND FROM HIVE_TPCE_TEMP.TMP_HOME_NETWORKS))
THEN CASE WHEN MODE IN ('A','N') THEN "${hiveconf:HAD}" ELSE "${hiveconf:HD}" END
ELSE CASE WHEN MODE IN ('A','N') THEN "${hiveconf:PAI}" ELSE "${hiveconf:PDI}" END END HPDA_ID
FROM HIVE_TPCE.VW_CDMA_CD A INNER JOIN HIVE_TPCE.STG_CURRENT_FILES B
ON A.DRI = B.DRI AND A.SOURCE_FILE_ID = B.SOURCE_FILE_ID
INNER JOIN
HIVE_TPCE.WRK_LOCATION C
ON A.DRI = C.DRI AND A.LOCATION_ID = C.LOCATION_ID
INNER JOIN
HIVE_TPCE.LU_RADIO D
ON A.RADIO_ID = D.RADIO_ID WHERE A.CID > 0 AND D.MODE IN ('A','N') AND A.RXPOWER IS NOT NULL AND A.CALL_RESULT_ID BETWEEN 1 AND 16;
My error signature is
FAILED: ParseException line 10:42 mismatched input ',' expecting ) near 'DRI' in expression specification
According to the Hive Language Manual: "Hive supports subqueries only in the FROM clause".
Your CASE WHEN is part of the SELECT clause, but it includes includes a SELECT subquery. Seems like that is not supported, so your syntax is not correct (in Hive).
Perhaps you could stage the data in MySQL using the query you have and then load it into Hive using a simple SELECT without CASE WHEN?
See official document.
It says
Assumptions
We plan to limit the scope with the following assumptions and limitations.
Subqueries could only be top-level expressions in SELECT. That is, subqueries in complex expressions, aggregates, UDFs, etc. will not be supported for now

SQL server syntax error in update statement, but I can't see it

I'm getting a syntax error on this query, but I can't figure it out.
Incorrect syntax near the keyword
'group'.
I believe its on the last group by, but I don't see whats wrong. Can anyone suggest how to correct this?
UPDATE [NCLGS].[dbo].[CP_CustomerShipTo]
SET TimesUsed = TimesUsed + B.NewCount
from [NCLGS].[dbo].[CP_CustomerShipTo] CST
INNER JOIN (
Select
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip,
Count(recid) As NewCount
from avanti_packingslipheader PKH
where pksdate > dbo.ufn_StartOfDay(DATEADD(d, -1, GETDATE() ) )
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
) B
ON CST.CustomerCode = B.CompanyCode
AND CST.ShipToName = B.CompanyName
AND CST.ShipToAddress1 = B.Addr1
AND CST.City = B.City
AND CST.PostalCode = B.Zip
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
BACKGROUND - I'm trying to do an update statement with a Count(), but of course you can't use agg. functions in an update set statement, so I'm trying to use a subquery.
You have already got GROUP BY inside the subselect, so what does the outer GROUP BY stand for?
You can't reference an alias in a subselect from an outer GROUP BY. But in any event you can't use GROUP BY with an UPDATE statement, and that's what the error message is about.
Try removing the last Group By. What exactly are you hoping this last group by will do?
Change the code to this:
update mytable set
mycolumn = mycolumn + (select x from ...);