I am writing an equivalent logic in BQ as in my source system. In source SQL server side it is working fine. But in Big query it is failing with the OR condition in the last left outer join condition. If I am moving the OR condition in the where clause it is giving wrong count. Need help to fix this issue. How can I re write the below query ?
SELECT count(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON (DIM.diSet = PRO.diSet)
INNER JOIN DQConfig CFG
ON (CFG.ConSet = PRO.ConSet)
LEFT OUTER JOIN AgSt CCT
ON (CCT.StSet = PRO.StSet)
INNER JOIN stprof SummPRO
ON (SummPRO.diSet = DIM.SummdiSet AND
SummPRO.dIntervalStart = PRO.dIntervalStart AND
SummPRO.SiteId = PRO.SiteId AND
SummPRO.nDuration = PRO.nDuration)
LEFT OUTER JOIN AgSt SummCCT
ON (SummCCT.StSet = SummPRO.StSet)
LEFT OUTER JOIN AgentStatus SummSTS
ON (
SummSTS.StSet = SummPRO.StSet
OR
SummSTS.StSet = PRO.StSet)
WHERE DIM.cType = 'A'
You can replace LEFT JOIN with CROSS JOIN and move condition from ON clause to WHERE clause as in below example
#standardSQL
SELECT COUNT(*)
FROM stprof PRO
INNER JOIN stdim DIM
ON DIM.diSet = PRO.diSet
INNER JOIN DQConfig CFG
ON CFG.ConSet = PRO.ConSet
LEFT OUTER JOIN AgSt CCT
ON CCT.StSet = PRO.StSet
INNER JOIN stprof SummPRO
ON SummPRO.diSet = DIM.SummdiSet
AND SummPRO.dIntervalStart = PRO.dIntervalStart
AND SummPRO.SiteId = PRO.SiteId
AND SummPRO.nDuration = PRO.nDuration
LEFT OUTER JOIN AgSt SummCCT
ON SummCCT.StSet = SummPRO.StSet
CROSS JOIN AgentStatus SummSTS
WHERE DIM.cType = 'A'
AND (
SummSTS.StSet = SummPRO.StSet
OR SummSTS.StSet = PRO.StSet
)
I have a query result tabletable of result i would like to sum the bill amount such that it returns one row with a distinct account ,balance,sum billed amount fPreviousReading,
fCurrentReading,
fConsumption .
result should be
1.account 11074
2.balance269.49
3.sumbilledamount 520.48
4. fPreviousReading 574
5 fCurrentReading 612
6 fConsumption 38
Thanks
query
select
Ten.Account,
ten.DCBalance AS Balance,
SUM(T.fInclusiveAmount)AS BilledAmount,
MRD.fPreviousReading,
MRD.fCurrentReading ,
MRD.fConsumption ,
T.cDescription
from _mtblTransactions T
left join _mtblProperties P ON P.idProperty = T.iPropertyID
left join _mtblPropertyPortions PP ON PP.idPropertyPortions = T.iPortionID
left join _mtblPropertyPortionServices PPS ON PPS.idPropertyPortionServices = T.iPropertyPortionServiceID
left join _mtblCategories Cat ON Cat.idCategory = PP.iPortionUsageID
left join _mtblServices S ON PPS.iPortionServiceID = S.idService
left join _mtblServiceGroups SG ON S.iServiceGroupID = SG.idServiceGroup
left join _mtblRateTariffs RT ON RT.idRateTariffs = PPS.iServiceRateTariffID
left join Client Ten ON T.iCustomerID = Ten.DCLink
left join _mtblMeters M ON PPS.iPropertyPortionMeterID = M.idMeter
left join _mtblWalkDetails WD ON WD.iWalkMeterID = PPS.iPropertyPortionMeterID
left join _mtblWalks W ON WD.iWalkID = W.idWalk
left join Client Own ON P.iPropertyOwnerID = Own.DCLink
left outer join _mtblRegions R on R.idRegions = P.iPropertyRegionID
left outer join _mtblSubRegions SR on SR.idSubRegions = P.iPropertySubRegionID
left outer join _mtblAreas A on A.idAreas = P.iPropertyAreaID
left join _etblPeriod PER ON T.iPeriodID = PER.idPeriod
left join _mtblMeterReadingDetails MRD ON T.iMeterID = MRD.iMeterReadingsMeterID and T.iPeriodID = MRD.iBillingPeriodID
and MRD.iReadingType=0
Where
oWN.Account='11074'
and idPeriod='79'
GROUP BY Ten.Account,ten.DCBalance,MRD.fPreviousReading, MRD.fCurrentReading, MRD.fConsumption, T.cDescription
As I don't know your data, there is a possibility I wrote you a code that would return some double rows. But that is a problem you can easily handle.
Try it, nevertheless:
SELECT t1.Account,
t1.Balance,
t1.BilledAmount,
t1.fPreviousReading,
t1.fCurrentReading,
t1.fConsumption,
t2.cDescription
FROM (SELECT Ten.Account,
ten.DCBalance AS Balance,
SUM (T.fInclusiveAmount) AS BilledAmount,
SUM (MRD.fPreviousReading) AS fPreviousReading,
SUM (MRD.fCurrentReading) AS fCurrentReading,
SUM (MRD.fConsumption) AS fConsumption
FROM _mtblTransactions T
left join _mtblProperties P ON P.idProperty = T.iPropertyID
left join Client Ten ON T.iCustomerID = Ten.DCLink
left join Client Own ON P.iPropertyOwnerID = Own.DCLink AND oWN.Account='11074'
left join _etblPeriod PER ON T.iPeriodID = PER.idPeriod
left join _mtblMeterReadingDetails MRD ON T.iMeterID = MRD.iMeterReadingsMeterID and T.iPeriodID = MRD.iBillingPeriodID
and MRD.iReadingType=0
and idPeriod='79'
GROUP BY Ten.Account,ten.DCBalance) t1
JOIN (SELECT T.cDescription,
Ten.Account,
ten.DCBalance
FROM _mtblTransactions T
left join Client Ten ON T.iCustomerID = Ten.DCLink) t2 ON t2.Account = t1.Account AND t2.DCBalance = t1.DCBalance
I experienced a strange behaviour, and I already was thinking of looking for a proper shrink. Can anyone explain this one here ?
This select returns, what I want:
SELECT struc~node,struc~tree_level,struc~parent,
strut~ltext,
mar~matnr, mar~mtart, mar~matkl,
mtx~maktx
FROM wrf_matgrp_struc AS struc
LEFT OUTER JOIN wrf_matgrp_strct AS strut
ON strut~hier_id = struc~hier_id
AND strut~node = struc~node
LEFT OUTER JOIN wrf_matgrp_sku AS sku
ON sku~hier_id = struc~hier_id
AND sku~node = struc~node
LEFT OUTER JOIN mara AS mar
ON mar~matnr = sku~matnr
LEFT OUTER JOIN maw1 AS maw
ON maw~matnr = mar~matnr
LEFT OUTER JOIN makt AS mtx
ON mtx~matnr = mar~matnr
AND mtx~spras = strut~spras
INTO TABLE #lt_result_tab
WHERE struc~hier_id = #gs_act_hier-hier_id
AND struc~node IN #grt_node
AND struc~date_from <= #sy-datum
AND struc~date_to >= #sy-datum
AND strut~spras = #sy-langu
AND mar~matnr IN #grt_matnr
AND mar~matkl IN #grt_matkl
AND maw~wekgr IN #grt_wekgr
I remove the join AND mtx~spras = strut~spras and place it into a where condition like this, and it does not work as expected ( resultset is incomplete ).
SELECT struc~node,struc~tree_level,struc~parent,
strut~ltext,
mar~matnr, mar~mtart, mar~matkl,
mtx~maktx
FROM wrf_matgrp_struc AS struc
LEFT OUTER JOIN wrf_matgrp_strct AS strut
ON strut~hier_id = struc~hier_id
AND strut~node = struc~node
LEFT OUTER JOIN wrf_matgrp_sku AS sku
ON sku~hier_id = struc~hier_id
AND sku~node = struc~node
LEFT OUTER JOIN mara AS mar
ON mar~matnr = sku~matnr
LEFT OUTER JOIN maw1 AS maw
ON maw~matnr = mar~matnr
LEFT OUTER JOIN makt AS mtx
ON mtx~matnr = mar~matnr
INTO TABLE #lt_result_tab
WHERE struc~hier_id = #gs_act_hier-hier_id
AND struc~node IN #grt_node
AND struc~date_from <= #sy-datum
AND struc~date_to >= #sy-datum
AND strut~spras = #sy-langu
AND mar~matnr IN #grt_matnr
AND mar~matkl IN #grt_matkl
AND maw~wekgr IN #grt_wekgr
AND mtx~spras = #sy-langu.
I've been struggling with this for some time now. It's something to do with how I'm joining the MMP_PPRVU I believe. Essentially, I'm trying to join CLARITY_TDL_TRAN --> CLARITY_EAP1 --> MMP_PPRVU. As well as CLARITY_TDL_TRAN --> MMP_STAGING_UNITS --> MMP_PPRVU.
All left outer joins. I've tried multiple different approaches but cannot come up with a solution that works. Any help is greatly appreciated.
SELECT........
FROM
CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join
dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join
CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join
CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join
CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join
CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join
CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join
dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join
CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join
CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
left outer join
CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join
dbo.MMP_PPRVU as MMP_PPRVU on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join
dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join
MMP_PPRVU ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
WHERE (CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10)) AND (mmp_PatientsTestaYes.PAT_ID IS NULL) AND (CLARITY_EAF_TIN.LINE = 1)
When you give a table an alias in your FROM clause, it's superflous to name the alias the same as the table name.
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
Furthermore, you join in the same table a few JOIN's down and it's name is the same. One of these two tables will need to have an alias that is different from the table name. Perhaps:
LEFT OUTER JOIN dbo.MMP_PPRVU as MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
So it doesn't conflict with:
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I find it helpful to use slightly different indentation when writing my FROM clause so that these things stand out a bit more and it's easier to parse.
FROM CLARITY.dbo.CLARITY_TDL_TRAN AS CLARITY_TDL_TRAN
LEFT OUTER JOIN dbo.MMP_STAGING_UNITS AS MMP_STAGING_UNITS
ON MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID
AND CLARITY_TDL_TRAN.DETAIL_TYPE IN (
1,
10
)
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP
ON CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1
ON CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
LEFT OUTER JOIN CLARITY.dbo.CLARITY_POS AS CLARITY_POS
ON CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_DEP AS CLARITY_DEP
ON CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_LOC AS CLARITY_LOC
ON CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
LEFT OUTER JOIN dbo.mmp_PatientsTestaYes AS mmp_PatientsTestaYes
ON CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SA AS CLARITY_SA
ON CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_SER AS CLARITY_SER
ON CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
LEFT OUTER JOIN CLARITY.dbo.CLARITY_EAF_TIN AS CLARITY_EAF_TIN
ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
LEFT OUTER JOIN dbo.MMP_PPRVU AS MMP_PPRVU_1
ON MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
LEFT OUTER JOIN dbo.MMP_revctrs$ AS MMP_revctrs$
ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
LEFT OUTER JOIN MMP_PPRVU
ON CLARITY_EAP_1.PROC_CODE = MMP_PPRVU.HCPCS
I like to use indentation to help keep joins understandable.
You can see below that you give the second join against CLARITY_EAP the alias of CLARITY_EAP1 but then when you try to join against it you use the alias CLARITY_EAP_1 which isn't defined and is likely throwing an error.
SELECT........
FROM CLARITY.dbo.CLARITY_TDL_TRAN as CLARITY_TDL_TRAN
left outer join dbo.MMP_STAGING_UNITS as MMP_STAGING_UNITS on MMP_Staging_Units.eTDL_ID = CLARITY_TDL_TRAN.TDL_ID and CLARITY_TDL_TRAN.DETAIL_TYPE in (1, 10)
left outer join dbo.MMP_PPRVU as MMP_PPRVU1 on MMP_Staging_Units.eFinMod = MMP_PPRVU.MOD
left outer join CLARITY.dbo.CLARITY_EAP as CLARITY_EAP on CLARITY_EAP.PROC_ID = CLARITY_TDL_TRAN.PROC_ID
left outer join dbo.MMP_revctrs$ as MMP_revctrs$ ON CLARITY_EAP.PROC_CODE = MMP_revctrs$.CPT
left outer join CLARITY.dbo.CLARITY_EAP AS CLARITY_EAP1 on CLARITY_EAP1.PROC_ID = CLARITY_TDL_TRAN.PROC_ID --look into this alias
left outer join MMP_PPRVU as MMP_PPRVU2 ON CLARITY_EAP1.PROC_CODE = MMP_PPRVU2.HCPCS
left outer join CLARITY.dbo.CLARITY_POS as CLARITY_POS on CLARITY_TDL_TRAN.POS_ID = CLARITY_POS.POS_ID
left outer join CLARITY.dbo.CLARITY_DEP as CLARITY_DEP on CLARITY_TDL_TRAN.DEPT_ID = CLARITY_DEP.DEPARTMENT_ID
left outer join CLARITY.dbo.CLARITY_LOC as CLARITY_LOC on CLARITY_TDL_TRAN.LOC_ID = CLARITY_LOC.LOC_ID
left outer join CLARITY.dbo.CLARITY_EAF_TIN as CLARITY_EAF_TIN ON CLARITY_LOC.LOC_ID = CLARITY_EAF_TIN.FACILITY_ID
left outer join dbo.mmp_PatientsTestaYes as mmp_PatientsTestaYes on CLARITY_TDL_TRAN.INT_PAT_ID = mmp_PatientsTestaYes.PAT_ID
left outer join CLARITY.dbo.CLARITY_SA as CLARITY_SA on CLARITY_TDL_TRAN.SERV_AREA_ID = CLARITY_SA.SERV_AREA_ID
left outer join CLARITY.dbo.CLARITY_SER as CLARITY_SER on CLARITY_TDL_TRAN.PERFORMING_PROV_ID = CLARITY_SER.PROV_ID
WHERE
CLARITY_TDL_TRAN.DETAIL_TYPE IN (1, 10) AND
mmp_PatientsTestaYes.PAT_ID IS NULL AND
CLARITY_EAF_TIN.LINE = 1
SELECT
dbo.pi_employee.emp_firstname, dbo.pi_employee.emp_lastname,
dbo.pi_employee.emp_no, dbo.pi_employee.emp_cnic,
dbo.pi_employee.emp_currentadd, dbo.pi_employee.emp_cellph,
dbo.pi_employee.emp_birthday, pi_jobtitle_1.jobtitle_name,
dbo.pi_employee.emp_joindate, dbo.pi_education.edu_degree,
dbo.pi_education.edu_year, dbo.pi_employee.emp_pension,
dbo.pi_employee.emp_age, dbo.pi_employee.emp_service,
dbo.pi_employee.emp_terminate, dbo.pi_employee.emp_termdate,
dbo.pi_employee.emp_basicofpay, dbo.pi_employee.emp_terminationreason,
dbo.pi_employee.emp_terminationdate, dbo.pi_employee.emp_status,
dbo.pi_employee.emp_gender, dbo.pi_employee.emp_maritalstatus,
dbo.pi_employee.emp_paymethod, dbo.pi_employee.emp_leaveentitle,
dbo.pi_employee.emp_confirmation, dbo.pi_employee.emp_title,
dbo.pi_employee.emp_basicamount, dbo.pi_salgrade.salgrade_name,
dbo.tbl_emp_status.StatusName, dbo.pi_skills.skill_type,
dbo.pi_location.loc_name, pi_location_1.loc_name AS wcity,
dbo.pi_jobtitlehist.jthSaleGradetype, dbo.pi_workexp.exp_serperiod,
dbo.pi_employee.emp_domicile, dbo.pi_skills.skill_type AS Skill,
dbo.pi_skills.skill_exp, dbo.pi_education.edu_degree AS Degree,
dbo.pi_education.edu_uni, dbo.pi_education.edu_distinction,
dbo.pi_lochistory.lhstart_date, dbo.pi_lochistory.lhend_date
FROM
dbo.pi_location
RIGHT OUTER JOIN
dbo.pi_workexp
RIGHT OUTER JOIN
dbo.pi_employee ON dbo.pi_workexp.emp_no = dbo.pi_employee.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist ON dbo.pi_employee.emp_no = dbo.pi_jobtitlehist.emp_no ON
dbo.pi_location.loc_id = dbo.pi_employee.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade ON dbo.pi_employee.emp_salgrade_id = dbo.pi_salgrade.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status ON dbo.pi_employee.emp_status = dbo.tbl_emp_status.StatusID
LEFT OUTER JOIN
dbo.pi_skills ON dbo.pi_employee.emp_no = dbo.pi_skills.emp_no
LEFT OUTER JOIN
dbo.pi_location AS pi_location_1
INNER JOIN
dbo.pi_lochistory ON pi_location_1.loc_id = dbo.pi_lochistory.loc_id ON dbo.pi_employee.emp_no = dbo.pi_lochistory.emp_no
LEFT OUTER JOIN
dbo.pi_education ON dbo.pi_employee.emp_no = dbo.pi_education.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS pi_jobtitle_1 ON dbo.pi_employee.emp_jobtitle_id = pi_jobtitle_1.jobtitle_id
I am writing sql query to implement different scenario, but problem is that it gives repeated values. I write distinct and order by too but result was same can any one help me to solve this issue.
EDIT – The same query with table names aliased:
SELECT
em.emp_firstname, em.emp_lastname,
em.emp_no, em.emp_cnic,
em.emp_currentadd, em.emp_cellph,
em.emp_birthday, jt.jobtitle_name,
em.emp_joindate, ed.edu_degree,
ed.edu_year, em.emp_pension,
em.emp_age, em.emp_service,
em.emp_terminate, em.emp_termdate,
em.emp_basicofpay, em.emp_terminationreason,
em.emp_terminationdate, em.emp_status,
em.emp_gender, em.emp_maritalstatus,
em.emp_paymethod, em.emp_leaveentitle,
em.emp_confirmation, em.emp_title,
em.emp_basicamount, sg.salgrade_name,
es.StatusName, s.skill_type,
L.loc_name, L1.loc_name AS wcity,
jh.jthSaleGradetype, we.exp_serperiod,
em.emp_domicile, s.skill_type AS Skill,
s.skill_exp, ed.edu_degree AS Degree,
ed.edu_uni, ed.edu_distinction,
Lh.lhstart_date, Lh.lhend_date
FROM
dbo.pi_location AS L
RIGHT OUTER JOIN
dbo.pi_workexp AS we
RIGHT OUTER JOIN
dbo.pi_employee AS em ON we.emp_no = em.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist jh ON em.emp_no = jh.emp_no ON L.loc_id = em.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade AS sg ON em.emp_salgrade_id = sg.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status AS es ON em.emp_status = es.StatusID
LEFT OUTER JOIN
dbo.pi_skills AS s ON em.emp_no = s.emp_no
LEFT OUTER JOIN
dbo.pi_location AS L1
INNER JOIN
dbo.pi_lochistory AS Lh ON L1.loc_id = Lh.loc_id ON em.emp_no = Lh.emp_no
LEFT OUTER JOIN
dbo.pi_education ed ON em.emp_no = ed.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS j ON em.emp_jobtitle_id = j.jobtitle_id
Yeah, since you're doing a crap ton of Cartesian products with those joins without on clauses, that will cause plenty of what seem like 'repeated values', but are really just the product of the two tables combining sets.