Query on subquery containing JOINS in Salesforce SQL (SOQL) - sql

SELECT
Email_address, COUNT(Order_date)
FROM
(SELECT
cust.Email_address, COUNT(ol.Variant_name), ord.Order_date
FROM
DMW_Order_Line_v3 ol
JOIN
DMW_Order_v3 ord ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier
AND ol.Brand_country = ord.Brand_country
JOIN
DMW_Customer_v3 cust ON ord.Email_address = cust.Email_address
AND ord.Brand_country = cust.Brand_country
WHERE
ord.Brand_country = 'kiehls-emea_CZ'
AND cust.Address_country = 'CZ'
AND cust.Optin_email != 'False'
AND ol.Line_status = 'SHIPPED'
AND ol.Variant_name = 'Sample'
GROUP BY
cust.Email_address, ord.Order_date
HAVING
COUNT(ol.Variant_name) >= 4)
GROUP BY
Email_address
Please, forgive me that I'm posting the whole body of the code. But it might be helpful somehow, who knows. As you can see it is a query on subquery containing joins. I'm using Salesforce SQL. When I run the code, I get this error:
Error saving the query field. Incorrect syntax near the keyword 'GROUP'.
What am I doing wrong? Besides being a noob ;-)

You don't need the count column in the subquery:
SELECT Email_address, COUNT(*)
FROM (SELECT cust.Email_address, ord.Order_date
FROM DMW_Order_Line_v3 ol JOIN
DMW_Order_v3 ord
ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier AND
ol.Brand_country = ord.Brand_country JOIN
DMW_Customer_v3 cust
ON ord.Email_address = cust.Email_address AND
ord.Brand_country = cust.Brand_country
WHERE ord.Brand_country = 'kiehls-emea_CZ' AND
cust.Address_country = 'CZ' AND
cust.Optin_email <> 'False' AND
ol.Line_status = 'SHIPPED' AND
ol.Variant_name = 'Sample'
GROUP BY cust.Email_address, ord.Order_date
HAVING COUNT(ol.Variant_name) >= 4
) e
GROUP BY Email_Address

Related

SQL Error when using LIKE CASE WHEN with subquery

I'm attempting to write an SQL statement that use Excel parameters to insert data via a drop down menu. This normally works great, but I think because I'm using a subquery in this case I'm getting the error "The multi-part identifier could not be bound." The subquery is used to create a sum of the quantity of an item purchased within the past week. Here's the code:
SELECT DISTINCT ITMMASTER.ITMREF_0 AS ITEMCODE, ITMMASTER.ITMDES1_0 AS ITEMDESC, Sum(ITMMVT.PHYSTO_0) AS AVAILSTOCK, Sum(ITMMVT.ORDSTO_0) AS ONORDER, Sum(ITMMVT.PHYALL_0) AS ALLOCATED, Sum(ITMFACILIT.MAXSTO_0) AS MAX,
INVQTY = (SELECT Sum(SINVOICED.QTY_0) AS INVQTY
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
),
ITMMASTER.ITMWEI_0 AS WEIGHT, ITMCOST.CSTTOT_0 AS COST, ITMBPS.BPSNUM_0 AS VENDOR, BPSUPPLIER.BPSNAM_0 AS VENDNAME, ITMMASTER.TSICOD_2 AS CAT
FROM x3v11.PROD.ITMMASTER ITMMASTER
LEFT OUTER JOIN x3v11.PROD.ITMMVT ITMMVT ON ITMMASTER.ITMREF_0 = ITMMVT.ITMREF_0
LEFT OUTER JOIN x3v11.PROD.ITMFACILIT ITMFACILIT ON ITMMVT.ITMREF_0 = ITMFACILIT.ITMREF_0 AND ITMMVT.STOFCY_0 = ITMFACILIT.STOFCY_0
LEFT OUTER JOIN x3v11.PROD.ITMCOST ITMCOST ON ITMMVT.ITMREF_0 = ITMCOST.ITMREF_0 AND ITMMVT.STOFCY_0 = ITMCOST.STOFCY_0
LEFT OUTER JOIN x3v11.PROD.ITMBPS ITMBPS ON ITMMASTER.ITMREF_0 = ITMBPS.ITMREF_0
LEFT OUTER JOIN x3v11.PROD.BPSUPPLIER BPSUPPLIER ON ITMBPS.BPSNUM_0 = BPSUPPLIER.BPSNUM_0
WHERE ITMCOST.STOFCY_0 <> '115'
AND ITMFACILIT.MAXSTO_0 <> 0
AND ITMBPS.PIO_0 <> 99
AND ITMBPS.BPSNUM_0 LIKE CASE WHEN ? IS NOT NULL THEN ? ELSE '%' END
GROUP BY ITMMASTER.ITMREF_0, ITMMASTER.ITMDES1_0, ITMMASTER.ITMWEI_0, ITMCOST.CSTTOT_0, ITMBPS.BPSNUM_0, BPSUPPLIER.BPSNAM_0, ITMMASTER.TSICOD_2
ORDER BY ITMMASTER.ITMREF_0
I think you have specified the syntax for the subquery incorrectly:
INVQTY = (SELECT Sum(SINVOICED.QTY_0) AS INVQTY
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN
DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
),
I think it should be:
(SELECT Sum(SINVOICED.QTY_0)
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN
DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
) AS INVQTY,

ORA-00971: Missing SET keyword

I'm getting a confusing "ORA97100 missing SET keyword" error when trying to run this simple UPDATE statement.
UPDATE
(SELECT
RKAP_PROYEKSI.ID AS ID,
RKAP_PROYEKSI.TAHUN AS TAHUN,
RKAP_PROYEKSI.KODE_ANGGARAN AS KODE_ANGGARAN,
RKAP_PROYEKSI.JENIS_BIAYA AS JENIS_BIAYA,
RKAP_PROYEKSI.SUBTOTAL AS SUBTOTAL,
RKAP_PROYEKSI.TOTAL AS TOTAL,
RKAP_PROYEKSI.BELONGS_TO AS BELONGS_TO,
RKAP_PROYEKSI.NOMOR AS NOMOR,
RKAP_PROYEKSI.STATUS AS STATUS
FROM WOS.RKAP_PROYEKSI
LEFT JOIN WOS.RKAP_MASTER_KODE ON RKAP_MASTER_KODE.ID_KODE = RKAP_PROYEKSI.KODE_ANGGARAN
LEFT JOIN WOS.RKAP_USER ON RKAP_USER.BIRO = RKAP_MASTER_KODE.BIRO
WHERE TAHUN = '2018' AND RKAP_MASTER_KODE.BIRO = 'BSI') AS helper
SET helper.STATUS = 0
I've also tried to erase the "AS", but still get in error.
Any well thought to advise will be appreciated.
Thanks
You can use WITH..AS clause :
UPDATE RKAP_PROYEKSI R
SET STATUS = (
WITH RP AS
(
SELECT
RKAP_PROYEKSI.ID AS ID,
RKAP_PROYEKSI.TAHUN AS TAHUN,
RKAP_PROYEKSI.KODE_ANGGARAN AS KODE_ANGGARAN,
RKAP_PROYEKSI.JENIS_BIAYA AS JENIS_BIAYA,
RKAP_PROYEKSI.SUBTOTAL AS SUBTOTAL,
RKAP_PROYEKSI.TOTAL AS TOTAL,
RKAP_PROYEKSI.BELONGS_TO AS BELONGS_TO,
RKAP_PROYEKSI.NOMOR AS NOMOR,
RKAP_PROYEKSI.STATUS AS STATUS,
0 AS ZERO_STATUS
FROM WOS.RKAP_PROYEKSI
LEFT JOIN WOS.RKAP_MASTER_KODE ON RKAP_MASTER_KODE.ID_KODE = RKAP_PROYEKSI.KODE_ANGGARAN
LEFT JOIN WOS.RKAP_USER ON RKAP_USER.BIRO = RKAP_MASTER_KODE.BIRO
WHERE TAHUN = '2018' AND RKAP_MASTER_KODE.BIRO = 'BSI')
SELECT RP.ZERO_STATUS
FROM RP
WHERE RP.ID = R.ID
)
I think you can phrase this as:
UPDATE WOS.RKAP_PROYEKSI p
SET STATUS = 0
WHERE p.TAHUN = '2018' AND
EXISTS (SELECT 1
FROM WOS.RKAP_MASTER_KODE mk JOIN
WOS.RKAP_USER u
ON u.BIRO = mk.BIRO
WHERE mk.ID_KODE = p.KODE_ANGGARAN AND
mk.BIRO = 'BSI'
);
I don't see what RKAP_USER is being used for, but I left it in the query, just in case.

Update a join tables using SQL

This is my select query
select s.enabled,
s.totalpoints,
transaction_diff_dates
from loyalty_scheme l
join subscribe_merchant s on (l.scheme_id = s.schemerefid)
join transaction t on (s.unipoint_customer_ref_id = t.unipoint_customer_ref_id)
where (s.transaction_diff_dates = to_char(current_date, 'yyyy-MM-dd'))
and (s.merchant_ref_id = 11)
and (s.schemerefid = (select scheme_id
from loyalty_scheme
where (minimum_purchase_amount = (select min(minimum_purchase_amount)
from loyalty_scheme
where merchant_id = 11))
group by scheme_id))
group by s.unipoint_customer_ref_id,
s.enabled,
s.totalpoints,
s.transaction_diff_dates
This query returns two rows.
I need to update this enabled filed = 'false' , and totalPoint = 0 filed , instead of selecting
So I tried to write an update query
UPDATE subscribe_merchant
SET enabled = 'true'
FROM loyalty_scheme L
JOIN subscribe_merchant S ON (L.scheme_id = S.schemerefid)
JOIN transaction T ON (S.unipoint_customer_ref_id = T.unipoint_customer_ref_id)
WHERE (s.transaction_diff_dates = to_char(CURRENT_DATE, 'yyyy-MM-dd'))
AND (s.merchant_ref_id = 11)
AND (s.schemerefid = (SELECT scheme_id
FROM loyalty_scheme
WHERE (minimum_purchase_amount = (SELECT MIN(minimum_purchase_amount)
FROM loyalty_scheme
WHERE merchant_id = 11))
GROUP BY scheme_id))
GROUP BY
s.unipoint_customer_ref_id, s.enabled,
s.totalpoints, s.transaction_diff_dates
but this update query dose not work.
Please help me to write the update query

how to use more than 1 sub query in hive

I am executing the below query getting the error.
FAILED: SemanticException [Error 10249]: Line 13:15 Unsupported SubQuery Expression 'master_cd': Only 1 SubQuery expression is supported.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM cust_focus cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
WHERE cfs.roll_no NOT IN (SELECT roll_no FROM class_hist)
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
AND (cfs.master_cd IN (SELECT msk5.msk5_master_cd from msk5_mst_tbl as msk5 WHERE cfs.master_cd=msk5.msk5_master_cd and msk5_m_code=9)
OR cfs.master_cd IS NULL)
group by cfs.roll_no;
Could you please help me how to resolve this error.
Thanks in Advance.
SELECT
cfs.roll_no,
max(cclas.crdm_cd) as crdm_cd,
max(cclas.kjtm_cd) as kjtm_cd
FROM(select cf.* from cust_focus cf
join class_hist ch on cf.roll_no!=ch.roll_no
join msk5_mst_tbl msk5 on cf.master_cd = msk5.msk5_master_cd where
msk5_m_code=9))cfs
LEFT JOIN cust_class cclas
ON (cfs.CF_CLAS_NO = cclas.CLAS_NO
AND cfs.DFS_CD = cclas.DFS_CD
AND cclas.D_AREA = 'US'
AND cclas.active_flag = 'Y')
AND UPPER(TRIM(cfs.D_AREA)) = 'US'
OR cfs.master_cd IS NULL
These many joins would impact the performance though!!
Only multiple join subqueries are supported.
below query works without any issue.
select * from (select id from test where id>10) a
join (select id from test where id>20) b on a.id=b.id;
In your case ,both filters are being used against same table(cust_focus) only otherwise you could have applied filters on different tables like above example.

Using subquery to find sum for individuals for reporting

I'm trying to calculate the sum of unposted transactions for individuals. I'm thinking I need a subquery to do this but I receive the
error (Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.)
I should have known that I would get that error but I'm not sure how to tie in the table. Here's the full query:
select ar.id_num,
nm.last_name,
nm.first_name,
am.ADDR_LINE_1,
am.ADDR_LINE_2,
am.CITY,
am.STATE,
am.ZIP,
nm.EMAIL_ADDRESS,
ar.ar_bal_to_dte,
ar.UNAPPLIED_FIN_AID,
lt.Last_term_end_date
,ar.ar_bal_to_dte + ar.UNAPPLIED_FIN_AID as 'Unapplied Aid',
case when getdate()<= arm.end_dte then 'Dir Deposit' else 'Check' end as 'pmt type',
'Unposted Charges' = (select SUM (trans_amt) from TRANS_HIST where SOURCE_CDE = '#C' AND SUBSID_CDE = 'AR' group by ID_NUM )
from SUBSID_MASTER ar left outer join ccsc_last_term_end_date_v lt on ar.ID_NUM = lt.id_num
left outer join SUBSID_MASTER_EPAY_ACCT arm ON AR.ID_NUM = arm.ID_NUM
inner join TRANS_HIST th ON AR.ID_NUM = th.ID_NUM
JOIN NAME_MASTER nm ON ar.ID_NUM = nm.ID_NUM
JOIN ADDRESS_MASTER am on ar.ID_NUM = am.ID_NUM
where ar.SUBSID_CDE = 'AR'
and ar.AR_BAL_TO_DTE < 0 and am.ADDR_CDE = '*LHP'
order by lt.Last_term_end_date
Does anyone have some advice for me? It would be greatly appreciated.
This is the only subquery in the query:
'Unposted Charges' = (select SUM(trans_amt) from TRANS_HIST where SOURCE_CDE = '#C' AND SUBSID_CDE = 'AR' group by ID_NUM )
First, this looks really awkward. Only use single quotes for string and date constants. Use square braces:
[Unposted Charges] = (select SUM(trans_amt) from TRANS_HIST where SOURCE_CDE = '#C' AND SUBSID_CDE = 'AR' group by ID_NUM )
The problem is the GROUP BY. It returns a different row for each ID_NUM, hence the error.
One easy fix to the syntax problem is:
[Unposted Charges] = (select SUM(trans_amt) from TRANS_HIST where SOURCE_CDE = '#C' AND SUBSID_CDE = 'AR')
However, you probably intend:
[Unposted Charges] = (select SUM(th.trans_amt)
from TRANS_HIST th
where th.SOURCE_CDE = '#C' AND
th.SUBSID_CDE = 'AR' AND
th.ID_NUM = ar.ID_NUM
)
A nice alternative to the subselect would be to join to a derived table of
... INNER JOIN (SELECT TH.ID_NUM, SUM(TH.TRANS_AMT) AS UNPOSTED_CHARGES FROM TRANS_HIST TH WHERE TH.SOURCE_ID = '#C' AND TH.SUBSID_CDE = 'AR' GROUP BY TH.ID_NUM) DT ON DT.ID_NUM = AR.ID_NUM ...
So then you can just use DT.UNPOSTED_CHARGES in your select list.