I have the query below, TBL1 X is my source table and TBL2 Y is my target table.
I want to get only the data from TBL1 X that any of the fields indicated below don't match their corresponding fields in TBL2 Y.
I am not a 100% sure that the code is correct. Can someone help please
SELECT
Col1,
Col2,
PRESC_ID,
PRSC_NPI_N,
FROM TBL1 X
JOIN
(
SELECT
CLAIM_ID,
ColA,
ColB,
ColC,
ColD
FROM TBL2 Y
)
ON X.PHARM_ID = Y.CLAIM_ID
---- If this condition X.PHARM_ID= Y.CLAIM_ID and any of the ones below is satisfied, I want the record to be return.
WHERE X.PHARM_ID= Y.CLAIM_ID
OR X.Col1 <> Y.ColA
OR X.Col2 <> Y.ColB
OR X.PRESC_ID <> Y.ColC
OR X.PRSC_NPI_N <> Y.ColD;
You're close:
SELECT
Col1,
Col2,
PRESC_ID,
PRSC_NPI_N,
FROM TBL1 X
JOIN TBL2 AS Y
ON X.PHARM_ID = Y.CLAIM_ID
WHERE X.Col1 <> Y.ColA
OR X.Col2 <> Y.ColB
OR X.PRESC_ID <> Y.ColC
OR X.PRSC_NPI_N <> Y.ColD;
But I would prefer a Correlated Subquery:
SELECT
Col1,
Col2,
PRESC_ID,
PRSC_NPI_N,
FROM TBL1 X
WHERE EXISTS
( SELECT *
FROM TBL2 AS Y
WHERE X.PHARM_ID = Y.CLAIM_ID -- same ID
AND -- any other column is different
( X.Col1 <> Y.ColA
OR X.Col2 <> Y.ColB
OR X.PRESC_ID <> Y.ColC
OR X.PRSC_NPI_N <> Y.ColD
)
);
Both versions will fail if columns contain NULLs.
I think this is the correct logic, based on your description:
SELECT
Col1,Col2,PRESC_ID,PRSC_NPI_N
FROM TBL1 X
WHERE PHARM_ID in (
SELECT CLAIM_ID FROM TBL2 Y
where X.PHARM_ID= Y.CLAIM_ID
AND (
X.Col1 <> Y.ColA
OR X.Col2 <> Y.ColB
OR X.PRESC_ID <> Y.ColC
OR X.PRSC_NPI_N <> Y.ColD
)
)
Related
How to tune the below second VT?
DT_RANGE-PERIOD(STRT_DT,END_DT+1)
CREATE MULTISET VOLATILE TABLE TABLE1 AS
(
SELECT COL1, COL2,COL3
BEGIN(DT_RANGE) AS START_DT,
END(DT_RANGE) -1 AS END_DT,
row_number() over(partition by COL1, COL2,COL3 order by BEGIN(DT_RANGE) ,END(DT_RANGE) -1) as rown
FROM (
SELECT NORMALIZE ON MEETS OR OVERLAPS
MCOL1, COL2,COL3, DT_RANGE
FROM TABLE0
)X
)WITH DATA ON COMMIT PRESERVE ROWS;
--Identify overlapping spans
--overlp_rn - this column determines the overlapping rownumber
CREATE MULTISET VOLATILE TABLE TABLE2_COMBINED_OVERLAPS AS
(
WITH RECURSIVE RecursiveParent( COL1, COL2,COL3,START_DT,END_DT,orig_rn, overlp_rn,query_lvl)
AS
(
SELECT COL1, COL2,COL3,START_DT,END_DT,rown as orig_rn,0 as overlp_rn, 1 as query_lvl
FROM TABLE1
UNION ALL
SELECT a.COL1,a.COL2,a.COL3,a.START_DT,a.END_DT,a.orig_rn as orig_rn,b.rown as overlp_rn, 1+ a.query_lvl as query_lvl
FROM RecursiveParent a inner JOIN TABLE1 b
on a.COL1 = b.COL1
and a.COL2 = b.COL2
and ((a.START_DT between b.START_DT and b.END_DT) or (a.END_DT between b.START_DT and b.END_DT)
or (b.START_DT between a.START_DT and a.END_DT) or (b.END_DT between a.START_DT and a.END_DT)
)
and a.orig_rn < b.rown
where query_lvl <= b.rown
)
SELECT distinct COL1, COL2,COL3,START_DT,END_DT,orig_rn, max(overlp_rn) as overlp_rn
FROM RecursiveParent
group by COL1, COL2,COL3,START_DT,END_DT,orig_rn
)WITH DATA ON COMMIT PRESERVE ROWS;
I am running a CREATE TABLE TBL AS SELECT statement as below. I want to write a CASE STATEMENT that will compare
values from column X.PRESC_ID to values from column Y.PRSC_NPI and if there is match, it should INSERT to TBL.PRESC_ID,
and for all the X.PRESC_ID that do not match with any value in Y.PRSC_NPI should be INSERTED to TBL.PRSC_NPI_N
CREATE TABLE TBL (
Col1,
Col2,
PRESC_ID,
PRSC_NPI_N,
AS
(
SELECT
Col1,
Col2,
PRESC_ID,
PRSC_NPI_N,
FROM TBL2 X
JOIN
(SELECT CLAIM_ID,PRSC_NPI FROM TBL3) Y
ON Y.CLAIM_ID = Y.Col1
I have tried the one below but it is not working
CASE
WHEN X.PRESC_ID = Y.PRSC_NPI THEN TBL.PRESC_ID
ELSE TBL.PRSC_NPI_N
END
Seems you really want two CASE expressions, one for each result column. Something like
CASE WHEN X.PRESC_ID = Y.PRSC_NPI THEN X.PRESC_ID END AS PRESC_ID,
CASE WHEN NOT(X.PRESC_ID = Y.PRSC_NPI) THEN X.PRSC_NPI_N END AS PRSC_NPI_N
In DB2 is there a way to basically say:
case when sku (select * from table1 where tb1field = 'SMOMD') then 'True' end
Okay so this is my query so far, I've been going at this for at least a month now so any help would be great.
select tb4.customer, tb4.sku, tb4.qty, tb4.retqty, tb4.stipqty, tb4.lastdate, tb4.firstdate, tb4.stipdate
from(
--Table 4
select tb3.Customer as Customer, tb3.sku as SKU, tb3.qty as Qty, tb3.retqty as RetQty, tb3.stipqty as STIPQty,
case when tb3.lastdate is null then '00/0000' else substr(tb3.lastdate,5,2)||'/'||substr(tb3.lastdate,1,4) end as LastDate,
case when tb3.firstdate is null then '00/0000' else substr(tb3.firstdate,5,2)||'/'||substr(tb3.firstdate,1,4) end as FirstDate,
case when tb3.stipdate is null then '00/0000' else substr(tb3.stipdate,5,2)||'/'||substr(tb3.stipdate,1,4) end as STIPDate
from(
--Table 3
select tb2.Customer as Customer, tb2.SKU as SKU, tb2.Qty as Qty, tb2.RetQty as RetQty, tb2.STIPQty as STIPQty,
max(case when tb2.TranID in ('010','100') then tb2.datenum end) as LastDate,
min(case when tb2.TranID in ('010','100') then tb2.datenum end) as FirstDate,
case when tb2.RC = '4M' then tb2.datenum end as STIPDate
from(
--Table 2
select tb1.Customer as Customer, tb1.SKU as SKU,
sum(case when tb1.TranID in ('010','100') then abs(tb1.OrdNet) else '0' end) as Qty,
sum(case when tb1.TranID = '500' and tb1.rc != '4M' then abs(tb1.OrdNet) else '0' end) as RetQty,
count(case when tb1.rc = '4M' then tb1.sku end) as STIPQty,
tb1.datenum as datenum, tb1.TranID as tranid, tb1.RC as rc
from(
--Table 1
select distinct stkund as Customer, sthptg||space(1)||stmodl||space(1)||stvari||space(1)||stfarb||space(1)||stgroe as SKU,
stvorg as TranID, stggru as RC, stprg09 as PG9, stprg08 as PG8, stperi as datenum, ormne1 as OrdNet
from st_usus.s_stati_pv
join caspdtau.cospf440 on stadrn = jadr40
where trim(stvert) in ('111S','122S')
and sthptg != 'V'
and aktv40 = 'A'
and stprg01 in ('01','04')
and stprg02 = '01'
and stvorg in ('500','010','100')
and stperi >= '20160100'
) as tb1
group by tb1.Customer, tb1.SKU, tb1.datenum, tb1.tranid, tb1.rc
) as tb2
group by tb2.customer, tb2.sku, tb2.qty, tb2.retqty, tb2.stipqty, tb2.tranid, tb2.rc, tb2.datenum
) as tb3
group by tb3.customer, tb3.sku, tb3.qty, tb3.retqty, tb3.stipqty, tb3.lastdate, tb3.firstdate, tb3.stipdate
) as tb4
order by tb4.Customer, tb4.sku
I'm not going to try to decipher exactly what you're trying to do...
Some general advice, rather than using Nested Table Expressions (NTE)
select <..> from (select <...>from mytable)
Consider Common Table Expressions (CTE)
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
, table2 as (select <...> from table1)
, table3 as (select <...> from table2)
, table4 as (select <....> from table3)
select <...> from table4;
Each CTE (ie. tableX) can refer to a prior CTE or a physical table/view as needed. The final select can refer to one or more CTE's along with one or more physical tables or views.
Nice thing about building with CTE's, is that you can check your results after each step..
with
table1 as (select <...> from st_usus.s_stati_pv join caspdtau.cospf440 on stadrn = jadr40)
select * from table1;
How to achieve the result set in sql query
You could work along
WITH
T1 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table1
),
T2 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table2
),
T3 AS (
SELECT
val,
ROW_NUMBER() OVER (PARTITION BY NULL ORDER BY id) rn
FROM Table3
)
SELECT
T1.val column1
, T2.val column2
, T3.val column3
FROM T1
JOIN T2
ON T1.rn = T2.rn
JOIN T3
ON T2.rn = T3.rn
ORDER BY T1.rn
;
You'd need to
put the statements, which are now going into the UNION into "T1" through "T3", and
move your current sort orders to the ROW_NUMBER analytic functions respectively.
… and should be done: SQL Fiddle
Please comment, if and as further detail is required.
I have a query (portion of it) like this below, when my GridView is loaded with this query, those rows that has got no value for IssCount and UsedCount will be blank. How can I set a default 0 value for these columns for such records?
SELECT
CASE
WHEN #SortByTypeCode = 1 THEN ROW_NUMBER() OVER(ORDER BY vt.Code)
WHEN #SortByTypeName = 1 THEN ROW_NUMBER() OVER(ORDER BY vt.Name)
WHEN #SortByTypeIssued = 1 THEN ROW_NUMBER() OVER(ORDER BY count(X.IssCount))
WHEN #SortByTypeUsed = 1 THEN ROW_NUMBER() OVER(ORDER BY count(Y.UsedCount ))
ELSE ROW_NUMBER() OVER(ORDER BY vt.AutoID)
END AS RowNum
,vt.AutoID
,vt.Code
,X.IssCount as Issued
,Y.UsedCount as Used
INTO #tmp_Results --Dont Change This
FROM VoucherType vt
LEFT JOIN (
SELECT VoucherType_AutoID,COUNT(VoucherNo) IssCount
FROM Voucher
WHERE VoidedBy IS NULL AND VoidedOn IS NULL
GROUP BY VoucherType_AutoID
) X ON vt.AutoID = X.VoucherType_AutoID
LEFT JOIN (
SELECT V.VoucherType_AutoID, COUNT(VUsed.AutoID) UsedCount
FROM voucherUsedLog VUsed
INNER JOIN Voucher V ON VUsed.Voucher_AutoID = V.AutoID
WHERE VUsed.VoidedBy IS NULL AND VUsed.VoidedOn IS NULL
GROUP BY V.VoucherType_AutoID
) Y
ON vt.AutoID = Y.VoucherType_AutoID
You can just wrap a COALESCE around X.IssCount and Y.UsedCount to make them 0, like so:
,COALESCE(X.IssCount,0) as Issued
,COALESCE(Y.UsedCount,0) as Used
Try this way..
ISNULL(X.IssCount,0) as Issued
,ISNULL(Y.UsedCount,0) as Used