Unsupported subquery with table in join predicate - google-bigquery

I am tried to run the next query but the next message appearance "Unsupported subquery with table in join predicate." Do you have some idea? I appreciate your comments.
WITH
D1 AS (
SELECT
SETID,
CUST_ID,
MAX(EFFDT) AS EFFDT
FROM
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT`
WHERE
EFFDT <= CURRENT_TIMESTAMP()
GROUP BY
1,
2 ),
LEFT OUTER JOIN
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT` D
ON
A.SETID = D.SETID
AND A.CUST_ID = D.CUST_ID
AND D.EFFDT = (
SELECT
MAX(EFFDT) --select 2
FROM
`g4s-data-reporting.g4s_data.PS_CUST_CREDIT` D1
WHERE
D1.SETID = D.SETID
AND D1.CUST_ID = D.CUST_ID
AND D1.EFFDT <= CURRENT_TIMESTAMP() )
WHERE
B.BUSINESS_UNIT IN ('00001',
'00048')
AND B.ITEM_STATUS = 'O'
AND B.ENTRY_TYPE NOT IN ('AO',
'CNVBC',
'MC',
'OA')

Related

How to select only unique values from this table based on criteria?

I want to select only the first record from each group. So basically I only want the data for first processing date column and don't want the rest of the date for each product.
My current code as below
SELECT
C.AccountID as ABN_ACC ,
C.ProductSymbol,
C.ProductShortName,
S.ReactorProduct AS REC_PROD,
C.CurrencyCode AS PROD_CCY,
C.CountryOfPayment AS PAYCONTRY,
C.DividendValueCur AS PAYCCY,
C.DividendValue AS DIV_AMNT,
CONCAT ((IIF (C.QuantitySettledNoTax_LS = 'S' ,(-1*C.QuantitySettledNoTax),C.QuantitySettledNoTax )),(IIF(C.QuantitySettledTax_LS = 'S',(-1*C.QuantitySettledTax),QuantitySettledTax))) AS DIVQTY ,
C.ExdividendDate AS EXDATE,
C.Recorddate AS RECDATE,
C.DividendPayDate AS PAYDATE
From "LiquidCDW". [Staging].[CA_CorporateActions] AS C
RIGHT JOIN "LiquidCDW".[Staging].[POS_SettledPositions] AS P ON C.Recorddate = P.ProcessingDate AND C.AccountID = p.AccountID AND C.ProductSymbol = P.ProductSymbol AND C.DividendValueCur = P.CurrencyCode
INNER JOIN "LiquidCDW".[Transformation].[Mapping_Product_ABNReactor] AS S ON C.ProductSymbol = S.ReactorProduct AND S.ValidTo IS NULL AND S.ProductType = 'E' AND c.DividendValueCur = S.Currency
where C.ExdividendDate >= '20210201'
ORDER by C.ProductSymbol , C.CurrencyCode
One approach uses ROW_NUMBER:
WITH cte AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY C.ProductSymbol ORDER BY processingDate) rn,
C.AccountID AS ABN_ACC,
C.ProductSymbol,
C.ProductShortName,
S.ReactorProduct AS REC_PROD,
C.CurrencyCode AS PROD_CCY,
C.CountryOfPayment AS PAYCONTRY,
C.DividendValueCur AS PAYCCY,
C.DividendValue AS DIV_AMNT,
CONCAT((IIF(C.QuantitySettledNoTax_LS = 'S',
(-1*C.QuantitySettledNoTax), C.QuantitySettledNoTax)),
(IIF(C.QuantitySettledTax_LS = 'S',
(-1*C.QuantitySettledTax),QuantitySettledTax))) AS DIVQTY,
C.ExdividendDate AS EXDATE,
C.Recorddate AS RECDATE,
C.DividendPayDate AS PAYDATE
FROM [LiquidCDW].[Staging].[CA_CorporateActions] AS C
RIGHT JOIN "LiquidCDW".[Staging].[POS_SettledPositions] AS P
ON C.Recorddate = P.ProcessingDate AND
C.AccountID = p.AccountID AND
C.ProductSymbol = P.ProductSymbol AND
C.DividendValueCur = P.CurrencyCode
INNER JOIN [LiquidCDW].[Transformation].[Mapping_Product_ABNReactor] AS S
ON C.ProductSymbol = S.ReactorProduct AND
S.ValidTo IS NULL AND
S.ProductType = 'E' AND
C.DividendValueCur = S.Currency
WHERE
C.ExdividendDate >= '20210201'
)
SELECT *
FROM cte
WHERE rn = 1
ORDER BY ProductSymbol, CurrencyCode;

SQL Select only unique values in column

I have the below query that is pulling from a table where a record (Employee ID) can have more than 1 row of data. Because of this I have some rows with the highest (Max) EXAM_DATE. I have added in the MAX function into the SELECT Statement, however I am still retrieving rows with 2 or more dates per empl. This must be something simply that I am overlooking. Any help is appreciated.
SQL Query:
SELECT A.EMPLID, A.BUSINESS_TITLE, A.DEPTID, A.LOCATION, A.LAST_NAME,
A.FIRST_NAME, A.MIDDLE_NAME,
MAX(CONVERT(CHAR(10),D.EXAM_DT,121))AS EXAM_DATE, D.EXAM_TYPE_CD,
(CONVERT(CHAR(10),D.GH_EXPIRE_DATE,121)) AS EXPIRE_DATE, AA.NAME AS
Manager_Name
FROM ((PS_EMPLOYEES A INNER JOIN PS_EMPLMT_SRCH_QRY A1 ON (A.EMPLID =
A1.EMPLID AND A.EMPL_RCD = A1.EMPL_RCD AND A1.OPRID = 'HD065' ))
LEFT OUTER JOIN PS_GHS_HS_PPDSCRN D ON A.EMPLID = D.EMPLID)
--LEFT OUTER JOIN PS_GHS_REPORTS_TO H ON A.EMPLID = H.EMPLID
LEFT OUTER JOIN PS_EMPLOYEES AA ON AA.POSITION_NBR = A.REPORTS_TO
AND (( AA.EFFDT =
(SELECT MAX(A_ED.EFFDT) FROM PS_EMPLOYEES A_ED
WHERE AA.POSITION_NBR = A_ED.POSITION_NBR
AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ))
WHERE ( ( A.EFFDT =
(SELECT MAX(A_ED.EFFDT) FROM PS_EMPLOYEES A_ED
WHERE A.EMPLID = A_ED.EMPLID
AND A.EMPL_RCD = A_ED.EMPL_RCD
AND A_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
AND A.EFFSEQ =
(SELECT MAX(A_ES.EFFSEQ) FROM PS_EMPLOYEES A_ES
WHERE A.EMPLID = A_ES.EMPLID
AND A.EMPL_RCD = A_ES.EMPL_RCD
AND A.EFFDT = A_ES.EFFDT)
AND D.EXAM_TYPE_CD = 'PPD'))
AND A.EMPL_STATUS = 'A'
--AND NOT EXISTS (SELECT D.EMPLID, COUNT(*) AS 'TotalOccur' FROM PS_GHS_HS_PPDSCRN D GROUP BY D.EMPLID HAVING COUNT(*) > 1)
GROUP BY A.EMPLID, A.BUSINESS_TITLE, A.DEPTID, A.LOCATION, A.LAST_NAME,
A.FIRST_NAME, A.MIDDLE_NAME, D.EXAM_TYPE_CD, D.GH_EXPIRE_DATE, AA.NAME
ORDER BY 5
I tried running it with the Commented out code for NOT EXISTS, however when I run this I do not retrieve any results.
Sample Output:
The MAX in your WHERE should be fine.
Your EXPIRE_DATE is also different between the two records, and as the EXPIRE_DATE is included in the GROUP BY, it will cause the "duplicate".

SQL sum and subtract issue with two queries

OK, I need somehelp. I have the following two queries:
SELECT DA.OWNG_OCD AS OFFICE, 'FL' AS STATE, SUM(S.STK_END_SEQ_NUM -
S.STK_STRT_SEQ_NUM) + COUNT(*) AS TOTSTK FROM STKRNG S, DFACCT DA, CMPNT C
WHERE RANGE_USED_SW = 'N' AND S.DFTACCT_CANUM = DA.DFTACCT_CANUM
AND DA.OWNG_OCD = C.OCD AND C.ST = 'FL' AND S.STK_TYP = 'R' GROUP
BY DA.OWNG_OCD;
AND
SELECT C.OCD, COUNT(*) AS USED FROM DRAFT D
JOIN STKRNG S ON S.DFTACCT_CANUM = D.DFTACCT_CANUM
JOIN DFACCT DA ON S.DFTACCT_CANUM = DA.DFTACCT_CANUM
JOIN CMPNT C ON CMPNT.OCD = DA.OWNG_OCD
WHERE D.DRFT_SEQ_NUM >= (SELECT MIN(S.STK_STRT_SEQ_NUM) FROM STKRNG S
WHERE D.DFTACCT_CANUM = S.DFTACCT_CANUM AND S.RANGE_USED_SW = 'N')
AND D.DRFT_SEQ_NUM <= (SELECT MAX(S.STK_END_SEQ_NUM) FROM STKRNG S WHERE
D.DFTACCT_CANUM = S.DFTACCT_CANUM AND S.RANGE_USED_SW = 'N')
AND S.STK_TYP = 'R'
AND S.RANGE_USED_SW = 'N'
AND C.ST = 'FL'
GROUP BY C.OCD;
I am trying to write one query where the results of the COUNT in the second query are subtracted from the results of the COUNT in the first query. Any idea on how to do this?
Put your queries in the from clause of your final query:
select q1.totstk - q2.used
from ( <your first query here> ) q1
join ( <your second query here> ) q2 on q2.ocd = q1.office;
try Something like this:
with STKRNGMINMAX as (
SELECT S.DFTACCT_CANUM,
MIN(S.STK_STRT_SEQ_NUM) MINNUM, MAX(S.STK_END_SEQ_NUM) MAXNUM,
SUM(S.STK_END_SEQ_NUM - S.STK_STRT_SEQ_NUM) DIFFNUM
FROM STKRNG S
WHERE (S.RANGE_USED_SW, S.STK_TYP)=('N', 'R')
group by S.DFTACCT_CANUM
)
SELECT C.OCD, S.DIFFNUM - COUNT(*) AS TOTSTK,
FROM DRAFT D
INNER JOIN STKRNGMINMAX S ON S.DFTACCT_CANUM = D.DFTACCT_CANUM and D.DRFT_SEQ_NUM between S.MINNUM AND S.MAXNUM
INNER JOIN DFACCT DA ON S.DFTACCT_CANUM = DA.DFTACCT_CANUM
INNER JOIN CMPNT C ON C.OCD = DA.OWNG_OCD and C.ST='FL'
GROUP BY C.OCD;

0 Results Effecting Entire Query

I was wondering if there is a way to make a results from a JOIN of a subquery result a predefined entry.
The query below pulls in the MAX(ReceiveDate) for the uniqueID JobNo. However, if nothing has been received the entire result will not show up. There are several other joins that could have data.
--***SUB QUERY (Receiver)
JOIN
(
SELECT
MAX(cast(r.ReceiveDate as DATE)) as ReceiveDate,
por.JobNo
FROM
POReleases as por
INNER JOIN
Receiver as r on por.PONum = r.PONum
GROUP BY por.JobNo
) r
ON r.JobNo = o.JobNo
The query will ultimately pull in data from purchase orders, result the most recent date, and the receiver with its most recent date based on the JobNo.
If nothing is received then result 'Whatever' or NULL. Anything.
The entire query is below:
DECLARE #now DATETIME
DECLARE #90daysago DATETIME
SET #now = GETDATE()
SET #90daysago = DATEADD(day, -90, #now)
;with cte as
(
SELECT
o.PartNo,
o.JobNo,
ord.DateEnt as oDateEnt,
por.MAXPONum,
po.DateEnt as poDateEnt,
tt.MAXtt,
r.ReceiveDate,
CASE
WHEN po.DateEnt >= r.ReceiveDate AND po.DateEnt >= tt.MAXtt THEN cast(po.DateEnt as DATE)
WHEN r.ReceiveDate >= po.DateEnt AND r.ReceiveDate >= tt.MAXtt THEN cast(r.ReceiveDate as DATE)
WHEN tt.MAXtt >= po.DateEnt AND tt.MAXtt >= r.ReceiveDate THEN cast(tt.MAXtt as DATE)
ELSE po.DateEnt
END AS MostRecentDate,
POProrate.TotalCost,
WIPProrate.WIPProrateCost,
(POProrate.TotalCost+WIPProrate.WIPProrateCost) as ProratedCost,
(ROUND(cast((o.QtyToMake - o.QtyShipped2Stock) as FLOAT)/o.QtyToMake,3))*(POProrate.TotalCost+WIPProrate.WIPProrateCost) as TotalProratedCost,
ROW_NUMBER() OVER(PARTITION BY o.JobNo ORDER BY tt.MAXtt DESC) as RowNum
FROM
--***MAIN QUERY (OrderDet)***
OrderDet as o
--***SUB QUERY (Order)***
JOIN
(
SELECT
cast(DateEnt as DATE) as DateEnt,
OrderNo
FROM
Orders
) ord
ON ord.OrderNo = o.OrderNo
--***SUB QUERY (POReleases)***
JOIN
(
SELECT
MAX(PONum) as MAXPONum,
JobNo
FROM
POReleases
GROUP BY
JobNo
) por
ON por.JobNo = o.JobNo
--***SUB QUERY (PO)***
JOIN
(
SELECT
PONum,
cast(DateEnt as DATE) as DateEnt
FROM
PO
) po
ON po.PONum = por.MAXPONum
--***SUB QUERY (TimeTicketDet)
JOIN
(
SELECT MAX(CAST(TicketDate as DATE)) as MaxTT,
JobNo
FROM TimeTicketDet as tt
GROUP BY JobNo
) tt
ON tt.JobNo = o.JobNo
--***SUB QUERY (Receiver)
JOIN
(
SELECT
MAX(cast(r.ReceiveDate as DATE)) as ReceiveDate,
por.JobNo
FROM
POReleases as por
INNER JOIN
Receiver as r on por.PONum = r.PONum
GROUP BY por.JobNo
) r
ON r.JobNo = o.JobNo
--***SUB QUERY (POProrate)***
JOIN
(
SELECT
j.JobNo,
SUM(j.TotalCost) as TotalCost
FROM
(
SELECT
por.JobNo,
CASE
WHEN pod.Unit = 'LOT' THEN SUM(pod.UnitCost*1)
ELSE SUM(por.Qty*pod.UnitCost)
END as TotalCost
FROM
PODet as pod
INNER JOIN
POReleases as por ON pod.PONum = por.PONum and pod.partno=por.partno
GROUP BY por.JobNo, pod.Unit
) j
GROUP BY j.JobNo
) POProrate
ON o.JobNo = POProrate.JobNo
--***SUB QUERY (WIPProrate)***
JOIN
(
SELECT
j.JobNo,
SUM(j.ProratedCost) as WIPProrateCost
FROM
(
SELECT
tt.StepNo,
tt.JobNo,
tt.ActualPayRate,
tt.BurdenRate,
tt.CycleTime,
tt.SetupTime,
tt.CycleTime + tt.SetupTime as TotalTime,
(tt.CycleTime + tt.SetupTime) * tt.ActualPayRate as LaborCost,
(tt.CycleTime + tt.SetupTime) * tt.BurdenRate as BurdenCost,
((tt.CycleTime + tt.SetupTime) * tt.ActualPayRate) + ((tt.CycleTime + tt.SetupTime) * tt.BurdenRate) as ProratedCost
FROM
TimeTicketDet as tt
) j
GROUP BY j.JobNo
) WIPProrate
ON WIPProrate.JobNo = o.JobNo
WHERE
o.Status = 'Open'
AND o.JobNo <> ''
AND ord.DateEnt <= #90daysago
AND po.DateEnt <= #90daysago
AND tt.MAXtt <= #90daysago
AND r.ReceiveDate <= #90daysago
)
SELECT *
FROM cte
WHERE
RowNum = 1
Your sub query needs to be a LEFT JOIN instead of a regular JOIN which will allow it to include results if present and NULL otherwise.
--***SUB QUERY (Receiver)
LEFT JOIN
(
SELECT
MAX(cast(r.ReceiveDate as DATE)) as ReceiveDate,
por.JobNo
FROM
POReleases as por
INNER JOIN
Receiver as r on por.PONum = r.PONum
GROUP BY por.JobNo
) r
ON r.JobNo = o.JobNo

Using a group by to group a select statement

Using a group by to group a select stament
SELECT
k.Ivalue, k.JOBDESCRIPTION ,
count( k.Ivalue) as TOTAL
FROM
(SELECT
a."ID" as Ivalue, b."JOBDESCRIPTION", rq."CURRENTSTATUS"
FROM
tblG2o_Requests a
INNER JOIN
tblG2o_JOBS b ON a."JOBPOSTID" = b."ID"
INNER JOIN
(SELECT
r.REQUESTID, ir."CURRENTSTATUS"
FROM
TBLG2O_RESULTSPOOL r
INNER JOIN
tblG2o_Requests ir ON r.RequestID = ir."ID"
WHERE
r.ShortListed = '1') rq ON rq.REQUESTID = a."ID"
WHERE
"ACTIVE" = '1'
AND "DATECOMPLETED" IS NULL
ORDER BY
"REQUESTDATE" DESC) k
GROUP BY
k.JOBDESCRIPTION
What is the question? You seem to be missing the group by clause, and you do not need double quotes around field names unless you have spaces in them, and even then, if TSQL for example, you would use [] in preference.
I had to remove an ORDER BY in the subquery, that isn't allowed unless other conditions demand it (like TOP n in TSQL)
SELECT
k.Ivalue
, k.JOBDESCRIPTION
, COUNT(k.Ivalue) AS TOTAL
FROM (
SELECT
a.ID AS Ivalue
, b.JOBDESCRIPTION
, rq.CURRENTSTATUS
FROM tblG2o_Requests a
INNER JOIN tblG2o_JOBS b
ON a.JOBPOSTID = b.ID
INNER JOIN (
SELECT
r.REQUESTID
, ir.CURRENTSTATUS
FROM TBLG2O_RESULTSPOOL r
INNER JOIN tblG2o_Requests ir
ON r.RequestID = ir.ID
WHERE r.ShortListed = '1'
) rqenter
ON rq.REQUESTID = a.ID
WHERE ACTIVE = '1'
AND DATECOMPLETED IS NULL
) k
GROUP BY
k.Ivalue
, k.JOBDESCRIPTION
Finally worked
SELECT
k.Ivalue
, l.JOBDESCRIPTION
, k.TOTAL,
k.CURRENTSTATUS
FROM (
SELECT
a.ID AS Ivalue
,b.ID as JobPostID
, rq."CURRENTSTATUS"
,COUNT(a.ID) AS TOTAL
FROM tblG2o_Requests a
INNER JOIN tblG2o_JOBS b
ON a."JOBPOSTID" = b.ID
INNER JOIN (
SELECT
r."REQUESTID"
, ir."CURRENTSTATUS"
FROM TBLG2O_RESULTSPOOL r
INNER JOIN tblG2o_Requests ir
ON r."REQUESTID" = ir.ID
WHERE r."SHORTLISTED" = 1
) rq
ON rq."REQUESTID" = a.ID
WHERE ACTIVE = '1'
AND DATECOMPLETED IS NULL
GROUP BY
a.ID ,b.ID
, rq."CURRENTSTATUS" ) k
inner join tblG2o_JOBS l on k.JobPostID =l.ID
enter code here