I am trying to find all records in my #TempTable that are not in the staging table.
Its important to note that the comparison needs to take place over 16 fields.
I have tried several combinations and nothing seems to work.
SELECT CustomerAccountNo FROM #TempTable
WHERE NOT EXISTS
(SELECT e.[CustomerAccountNo] ,
e.[MeterNo] ,
e.[CustomerName1] ,
e.[ServiceAddress1] ,
e.[ServiceAddress2] ,
e.[ServiceCity] ,
e.[ServiceState] ,
e.[ServiceZip] ,
e.[BillingAddress1] ,
e.[BillingAddress2] ,
e.[BillingAddress3] ,
e.[BillingCity] ,
e.[BillingState] ,
e.[BillingZip] ,
e.[BillingZip4] ,
e.[PrimaryPhoneNumber] FROM #TempTable e
JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)
Instead of a JOIN, try using Except.
SELECT CustomerAccountNo, MeterNo -- and so on
FROM #TempTable
EXCEPT
SELECT CustomerAccountNo, MeterNo -- and so on
FROM Staging
Just do a join and look for null. Like this
SELECT e.*
FROM #TempTable e
LEFT JOIN dbo.Staging s
ON e.CustomerAccountNo = s.CustomerAccountNo AND
e.MeterNo = s.MeterNo AND
e.CustomerName1 = s.CustomerName1 AND
e.ServiceAddress1 = s.ServiceAddress1 AND
e.ServiceAddress2 = s.ServiceAddress2 AND
e.ServiceCity = s.ServiceCity AND
e.ServiceState = s.ServiceState AND
e.ServiceZip = s.ServiceZip AND
e.BillingAddress1 = s.BillingAddress1 AND
e.BillingAddress2 = s.BillingAddress2 AND
e.BillingAddress3 = s.BillingAddress3 AND
e.BillingCity = s.BillingCity AND
e.BillingState = s.BillingState AND
e.BillingZip = s.BillingZip AND
e.BillingZip4 = s.BillingZip4 AND
e.PrimaryPhoneNumber= s.PrimaryPhoneNumb
WHERE s.CustomerAccountNo is null
You should provide more detailed circumstance to get correct answer.
Clearly, you don't have any connection between your FROM clause and WHERE clause so the query will return all.
Try this one:
SELECT CustomerAccountNo FROM #TempTable t
WHERE NOT EXISTS
(SELECT 1 FROM dbo.Staging s WHERE
t.CustomerAccountNo = s.CustomerAccountNo AND
t.MeterNo = s.MeterNo AND
t.CustomerName1 = s.CustomerName1 AND
t.ServiceAddress1 = s.ServiceAddress1 AND
t.ServiceAddress2 = s.ServiceAddress2 AND
t.ServiceCity = s.ServiceCity AND
t.ServiceState = s.ServiceState AND
t.ServiceZip = s.ServiceZip AND
t.BillingAddress1 = s.BillingAddress1 AND
t.BillingAddress2 = s.BillingAddress2 AND
t.BillingAddress3 = s.BillingAddress3 AND
t.BillingCity = s.BillingCity AND
t.BillingState = s.BillingState AND
t.BillingZip = s.BillingZip AND
t.BillingZip4 = s.BillingZip4 AND
t.PrimaryPhoneNumber= s.PrimaryPhoneNumber
)
Related
I would like to ask if it is possible to select again from a result set if a column contains a specific value?
For example, from the below query I want to select it as subquery and check if that subquery's first column contains both 2 and 3 result. Otherwise, no values should be return.
select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name
from tms.t_evaluation e
inner join tms.m_q_category c
on e.nendo=c.nendo
and e.q_category_id = c.q_category_id
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'
and e.delete_flg = '0'
and e.evaluator_id in ('2' , '3')
group by e.empl_id, e.nendo, e.q_category_id,
c.q_category_name, e.evaluator_id, e.history_no
Result contains both 2 and 3 in first column. Is this possible?
select e.evaluator_id, ROUND(avg(cast(e.rating_score as int))::numeric,1)::varchar, c.q_category_name
from tms.t_evaluation e
inner join tms.m_q_category c
on e.nendo=c.nendo
and e.q_category_id = c.q_category_id
and c.delete_flg = '0'
inner join tms.m_q_subcategory qs
on e.q_category_id = qs.q_category_id
and e.q_subcategory_id = qs.q_subcategory_id
and c.nendo = qs.nendo
and qs.delete_flg = '0'
where e.nendo = '2018'
and e.empl_id = 'empl05'
and e.delete_flg = '0'
and e.evaluator_id in (select case when evaluator_id=2 or evaluator_id=3 then evaluator_id else null from t_evaluation order by evaluator_id asc)
group by e.empl_id, e.nendo, e.q_category_id,
c.q_category_name, e.evaluator_id, e.history_no
Can I JOIN the table on the basis of the case statement in PostgreSQL. I have written the one SQL in stored procedure into that I'm passing the one flag on that basis I want to jon the table. Please see the case statement,
JOIN lease_intervals li_active
ON ( li_active.cid = l.cid AND l.id = li_active.lease_id AND
l.active_lease_interval_id = li_active.id )
LEFT JOIN applications a
ON ( a.cid = li.cid AND li.lease_id = a.lease_id AND
a.lease_interval_id = li.id )
**CASE
WHEN pIsFromUI = TRUE
THEN JOIN property_integration_databases pid ON ( pid.cid = l.cid AND pid.property_id == l.property_id )
JOIN integration_databases id ON ( id.id = pid.integration_database_id AND id.cid = pid.cid )
ELSE
1
END**
Please let me know is there any alternate solution for above.
join
lease_intervals li_active on
li_active.cid = l.cid and l.id = li_active.lease_id and
l.active_lease_interval_id = li_active.id
left join
applications a on
a.cid = li.cid and li.lease_id = a.lease_id and
a.lease_interval_id = li.id
left join
property_integration_databases pid on
pid.cid = l.cid and pid.property_id = l.property_id and pisfromui
left join
integration_databases id on
id.id = pid.integration_database_id and id.cid = pid.cid and pisfromui
i want to know if it is possible to get "Rowtype" for a cursor in postgresql (pl/pgsql).
..
my cursor has a select from multiple table and i don't know how to get the rowtype.
c_Data CURSOR FOR
select
dst_description
, cnt_number
, prd_name
, grt_code
, res_currency
, res_date
, prm_installmentdate
from tbl_xxx, tbl_yyy, tbl_aaa
where cnt_id = res_xxx
and prd_id = cnt_yyy
and dst_id = prd_aaa
and grt_id = res_xxx
and prm_id = res_aaa;
l_Data c_Data%rowtype;
please help
More like this way round.
declare
l_data record;
begin
select
INTO l_data --<<
dst_description
, cnt_number
, prd_name
, grt_code
, res_currency
, res_date
, prm_installmentdate
from tbl_xxx, tbl_yyy, tbl_aaa
where cnt_id = res_xxx
and prd_id = cnt_yyy
and dst_id = prd_aaa
and grt_id = res_xxx
and prm_id = res_aaa;
l_data will then be of the rowtype you need.
First of all sorry for my english,
I'm trying to insert a record into a table ... the record is the difference between the project table and temp table ...
The code I came into is
insert into pro_updatelog
select *, #user_n, GETDATE()
from pro
where cod = (select cod
from (SELECT * FROM pro
EXCEPT
SELECT * FROM temp
UNION ALL
SELECT * FROM temp
EXCEPT
SELECT * FROM pro) as T1);
then merge the temp table and original table ...
Not sure what should I do ...
All I want is that if there is any differences between the original table and temp table get recorded the original values into pro_updatelog table ... it would be best if only updated value will be recorded in table but it doesn't matter if a full row will be recorded but I want the original values before the update recorded!
Any ideas would be much appreciated!
thanks
I have succeeded that by using merge ... how ever it will take much longer to complete the stored procedure but it will do as I want ->
insert into pro_updatelog
select * , #user_n, GETDATE() from
(SELECT * FROM pro
EXCEPT
SELECT * FROM temp
UNION ALL
SELECT * FROM temp
EXCEPT
SELECT * FROM pro) as T1;
merge pro_updatelog as tar
using pro as sor
on (tar.cod = sor.cod)
when matched then
update set
tar.cod = sor.cod ,
tar.name_pr = sor.name_pr ,
tar.name_pe = sor.name_pe ,
tar.en = sor.en ,
tar.ending = sor.ending ,
tar.b = sor.b ,
tar.date_p = sor.date_p ,
tar.nek = sor.nek ,
tar.date_kh = sor.date_kh ,
tar.mp = sor.mp ,
tar.mt = sor.mt ,
tar.no_p = sor.no_p ,
tar.mas = sor.mas ,
tar.mablag = sor.mablag ,
tar.np = sor.np ,
tar.nf = sor.nf ,
tar.nn = sor.nn ,
tar.hpp1 = sor.hpp1 ,
tar.hpp2 = sor.hpp2 ,
tar.hpp3 = sor.hpp3 ,
tar.hpp4 = sor.hpp4 ,
tar.hpp5 = sor.hpp5 ,
tar.hpp6 = sor.hpp6 ,
tar.hpp7 = sor.hpp7 ,
tar.hpp8 = sor.hpp8 ,
tar.hpf1 = sor.hpf1 ,
tar.hpf2 = sor.hpf2 ,
tar.hpf3 = sor.hpf3 ,
tar.hpf4 = sor.hpf4 ,
tar.hpf5 = sor.hpf5 ,
tar.hpf6 = sor.hpf6 ,
tar.hpf7 = sor.hpf7 ,
tar.hpf8 = sor.hpf8 ,
tar.mola = sor.mola ,
tar.name1 = sor.name1 ,
tar.name2 = sor.name2 ,
tar.name3 = sor.name3 ,
tar.name4 = sor.name4 ,
tar.name5 = sor.name5 ,
tar.name6 = sor.name6 ,
tar.mab_t = sor.mab_t ,
tar.zarib1 = sor.zarib1 ,
tar.zarib2 = sor.zarib2 ,
tar.zarib3 = sor.zarib3 ,
tar.zarib4 = sor.zarib4 ,
tar.datet1 = sor.datet1 ,
tar.datet2 = sor.datet2 ,
tar.grup = sor.grup ,
tar.mablag1 = sor.mablag1 ,
tar.cod_g = sor.cod_g ,
tar.cod_m = sor.cod_m ,
tar.user_n = #user_n ,
tar.[date]=getdate();
I am trying to improve the performance of stored procedure which is currently taking more than 5 hours..
I have done below things to improve performance of this procedure-
-- i have created indexes for each table.
--Instead of count(*), i am using count(1).
But i am just wondering what to use in place of while loop, if possible, Can i use cursor? will there be any impact because i have read cursors do take a lot of time in processing..
Summary of procedure:
- Calculating row count of a table.
- for every row count selecting paramters.
- These parameters are being used by another procedure for the execution.
DECLARE #lnTotalCount INT
SELECT #lnTotalCount = COUNT(*) FROM
firstTable chksts
SELECT #lnRecordCount = 1, #lnRowsProcessed =0
WHILE #lnRecordCount <= #lnTotalCount
BEGIN
-- SET ROWCOUNT 1
SELECT #lcCKPY_REF_ID = ckck.CKPY_REF_ID,
#lnCKCK_SEQ_NO = ckck.CKCK_SEQ_NO,
#lcPYPY_ID = ckck.PYPY_ID,
#lnCKCK_CK_NO = ckck.CKCK_CK_NO,
#ldtCKCK_CASHED_DT = ckck.CKCK_CASHED_DT,
#ldtCKCK_PRINTED_DT = ckck.CKCK_PRINTED_DT,
#ldtCKCK_REISS_DT = ckck.CKCK_REISS_DT,
#lcCKCK_TYPE = ckck.CKCK_TYPE,
#lcCKCK_PAYEE_NAME = ckck.CKCK_PAYEE_NAME,
#lcCKCK_CURR_STS = LTRIM(RTRIM(chksts.[TRANSACTION])),
#lnCKST_SEQ_NO = ckck.CKST_SEQ_NO,
#lcCKCK_REISS_USUS_ID = ckck.CKCK_REISS_USUS_ID,
#lnCKCK_LOCK_TOKEN = ckck.CKCK_LOCK_TOKEN,
#ldtATXR_SOURCE_ID = ckck.ATXR_SOURCE_ID,
#lnBPID_CK_NO = chksts.SERIAL_NUMBER
FROM
firstTable chksts,
secondTable ckck,
thirdTable bpid
WHERE ckck.CKPY_REF_ID = bpid.CKPY_REF_ID
AND bpid.BPID_CK_NO = chksts.SERIAL_NUMBER
AND ckck.CKCK_SEQ_NO = bpid.CKCK_SEQ_NO
AND ckck.CKCK_CURR_STS IN ('03','X3')
AND chksts.ID = #lnRecordCount
IF(##ROWCOUNT<>0) /* 1.5 If-Else condition*/
BEGIN
EXEC #lnRetCd = ABProcedure
NULL,
NULL,
#lcCKPY_REF_ID,
#lnCKCK_SEQ_NO,
#lcPYPY_ID,
#lnCKCK_CK_NO,
#ldtCKCK_CASHED_DT,
#ldtCKCK_PRINTED_DT,
#ldtCKCK_REISS_DT,
#lcCKCK_TYPE,
#lcCKCK_PAYEE_NAME,
#lcCKCK_CURR_STS,
#lnCKST_SEQ_NO,
#lcCKCK_REISS_USUS_ID,
#lnCKCK_LOCK_TOKEN,
#ldtATXR_SOURCE_ID
SELECT #lnRowsProcessed = #lnRowsProcessed + ##ROWCOUNT
SELECT #lnRecordCount = #lnRecordCount + 1