My goal here is to combine the joint name, type 1 (Ex.1), to its primary account (Ex.2). This should be done for each share.
Should I be creating a temporary table to merge them into? I'm using SQL Server 2014
Something like this:
select t1.account,
t1.primename,
t2.primename,
t1.shareid,
t1.sharedesc
from table t1
left join table t2 on t1.account = t2.account and
t1.shareid = t2.shareid and
t1.nametype = 0 and
t2.nametype = 1
where t1.nametype = 0
To get your data, you'll need a query similar to this one. I'm guessing at the keys on your table, though.
SELECT
Ex1.*, Ex2.JointName
FROM
Ex1 LEFT JOIN Ex2 ON Ex1.Account = Ex2.Account
AND Ex1.ShareDesc = Ex2.ShareDesc
AND Ex1.ShareID = Ex2.ShareID
AND Ex1.NameDesc = 'Joint'
This will return a result set that you can use where you need to. You can wrap this in a view, which will then act like a table and always be up to date, if you need to.
Related
I am trying to pair 2 tables (nicknamed PERSIP and ECIF) on their ID field, (labeled TABLE1 & TABLE2) to create a RESULTTABLE, where the ym_id (for both tables) variable is set to my timekey0 variable for a specific datetime.
I am wondering why this code produces 0 rows of resulting data. After looking online, this was the format people posted as solutions to similar problems.
%let timekey0 = 202110;
proc sql;
CREATE TABLE RESULTTABLE AS
SELECT
PERSIP.col1,
PERSIP.col2,
PERSIP.col3,
ECIF.col1,
ECIF.col2,
ECIF.col3,
ECIF.col4
FROM DB.TABLE1 PERSIP
LEFT JOIN DB.TABLE2 ECIF
ON PERSIP.ID = ECIF.ID
WHERE ECIF.ym_id = &timekey0.
AND PERSIP.ym_id = &timekey0.;
quit;
I got a result of 0 rows with many columns. Not sure if my join type is incorrect but I have 0 rows in the table.
There may be two reasons for this:
There is no records matching to your where criteria (ECIF.ym_id = &timekey0.
AND PERSIP.ym_id = &timekey0.)
There is no records to join matching your on criteria (ON PERSIP.ID = ECIF.ID)
Your logic seems off. You say you want a LEFT JOIN then use a variable from the "RIGHT" table in your WHERE condition.
Most likely you just want to add those conditions to the ON condition.
FROM TABLE1 PERSIP
LEFT JOIN TABLE2 ECIF
ON PERSIP.ID = ECIF.ID
AND ECIF.ym_id = &timekey0.
AND PERSIP.ym_id = &timekey0.
Or perhaps just keep the condition that will limit the observations read from the "LEFT" table in the WHERE condition
FROM TABLE1 PERSIP
LEFT JOIN TABLE2 ECIF
ON PERSIP.ID = ECIF.ID
AND PERSIP.ym_id = ECIF.ym_id
WHERE PERSIP.ym_id = &timekey0.
i have a table attribute_name in which a column c_type indicate what type of value we have like 1,2,3,4 so that base on that value i decide which table to join .
so i select that table first Join (case statment) On (case statment)
but i does not work.
SELECT attribute_names.*,attributes_trans_name.*,
(CASE
WHEN attribute_names.c_type=1
THEN attribute_values_text.c_fk_files_id
WHEN attribute_names.c_type=3
THEN attribute_values_longtext.c_fk_files_id
WHEN attribute_names.c_type=8
THEN attribute_values_file.c_fk_files_id
END) as file_id
From attributes_trans_name,
attribute_names JOIN
(CASE
WHEN attribute_names.c_type=1
THEN attribute_values_text
WHEN attribute_names.c_type=3
THEN attribute_values_longtext
WHEN attribute_names.c_type=8
THEN attribute_values_file
END)
ON
(CASE
WHEN attribute_names.c_type=1
THEN attribute_values_text.c_fk_attribute_names_id
WHEN attribute_names.c_type=3
THEN attribute_values_longtext.c_fk_attribute_names_id
WHEN attribute_names.c_type=8
THEN attribute_values_file.c_fk_attribute_names_id
END) = attribute_names.c_id
WHERE
attribute_names.c_id=attributes_trans_name.c_fk_attribute_names_id
With proper JOIN/LEFT JOIN context, you can do in single query. Left join means I want the record from the left side always, but OPTIONAL if there is a match on the right side. So, I have adjusted your query to reflect. I have also rewritten to use "alias" names for the file names so it is shorter for read and write than bulky long table names.
So, the main table is the attribute_names as that appears to be the basis of all the joins with the C_ID column into each of the others. Notice indentation helps me know / follow what is linked to what, and not just all tables listed in bulk.
Now, by having each of the left-joins in place, it will ALWAYS TRY to link to their respective other tables by the foreign key, but as you know your data, only one of them will really have the piece of information you need. So your CASE construct is simplified down. If = 1, then look at the ATV (alias) table and its column, otherwise AVLT alias if = 3 and finally AVF if = 8
SELECT
AN.*,
ATN.*,
CASE WHEN AN.c_type = 1
THEN ATV.c_fk_files_id
WHEN AN.c_type = 3
THEN AVLT.c_fk_files_id
WHEN AN.c_type = 8
THEN AVF.c_fk_files_id END as file_id
From
attribute_names AN
JOIN attributes_trans_name ATN
ON AN.c_id = ATN.c_fk_attribute_names_id
LEFT JOIN attribute_values_text AVT
ON AN.c_id = AVT.c_fk_attribute_names_id
LEFT JOIN attribute_values_longtext AVLT
ON AN.c_id = AVLT.c_fk_attribute_names_id
LEFT JOIN attribute_values_file AVF
ON AN.c_id = AVF.c_fk_attribute_names_id
I have an update query where I am trying to find the net interest from multiple accounts on three different transaction tables deposits, checks, and general entries. The problem I'm running into is when one of those tables has no entries for a loan number to an interest account the select returns nothing and subsequently displays a null in the production table too. Negating the other tables if they return rows from interest account. If all three tables have a row for interest the query works fine.
Here's the code:
UPDATE Prod
SET Prod.InterestSpread = (T1.Amount+T2.Amount-T3.Amount)
FROM dbo.Production_Pipeline as Prod
LEFT JOIN
(
SELECT LoanNumber,COALESCE(SUM(JournalLineAmount),0)as Amount
FROM dbo.GeneralLedger
WHERE (JournalLineAccountRefListID = 'BB0000-1103842703'
OR JournalLineAccountRefListID = '800001DA-1202763722')
) AS T1
ON T1.LoanNumber = Prod.LoanNumber
LEFT JOIN
(
SELECT LoanNumber,COALESCE(SUM(DepositLineAmount),0)as Amount
FROM dbo.DepositLedger
WHERE ( DepositLineAccountRefListID = 'BB0000-1103842703'
OR DepositLineAccountRefListID = '800001DA-1202763722')
) AS T2
ON T2.LoanNumber = Prod.LoanNumber
LEFT JOIN
(
SELECT LoanNumber,COALESCE(SUM(ExpenseLineAmount),0) AS Amount
FROM dbo.CheckLedger
WHERE(ExpenseLineAccountRefListID = '800002B4-1308771936'
OR ExpenseLineAccountRefListID = 'D30000-1105022008'
OR ExpenseLineAccountRefListID = '8000029E-1283179936'
OR ExpenseLineAccountRefListID = 'BB0000-1103842703'
OR ExpenseLineAccountRefListID = '800001DA-1202763722')
) AS T3
ON T3.LoanNumber = Prod.LoanNumber
So an inccorect result update looks like
T1Amount: 496.08
T2Amount:
T3Amount: 373.92
Interest Spread: NULL
I've done some quite a bit of research on this site but, have been unable to apply your wisdom to my specific issue so any help here would greatly be appreciated.
Move your COALESCE statement to outside of the JOIN and remove it from the subqueries:
UPDATE Prod
SET Prod.InterestSpread =
COALESCE(T1.Amount,0)+COALESCE(T2.Amount,0)-COALESCE(T3.Amount,0)
...
The problem is your subqueries return no results, thus coalesce inside of that cannot be applied to the field. By using it outside of the outer join, you ensure if the result is null, it converts properly.
I am going to update a table using the sum of specific value from 3 different tables. For this purpose I wrote this query. But it takes too much time, what is the most efficient query for this purpose?
UPDATE dbo.dumpfile_doroud
SET dumpfile_doroud.sms_count_on_net = (SELECT sms_count_on_net
FROM dbo.dumpfile139201
WHERE
dbo.dumpfile_doroud.msisdn = dbo.dumpfile139201.msisdn)
+ (SELECT sms_count_on_net
FROM dbo.dumpfile139202
WHERE
dbo.dumpfile_doroud.msisdn = dbo.dumpfile139202.msisdn)
+ (SELECT sms_count_on_net
FROM dbo.dumpfile139203
WHERE
dbo.dumpfile_doroud.msisdn = dbo.dumpfile139203.msisdn)
P.S: dumpfile_doroud is small table but other three tables are really big.
Try this:
UPDATE t1
SET t1.sms_count_on_net=isnull(t2.sms_count_on_net,0) +
isnull(t3.sms_count_on_net,0) +
isnull(t4.sms_count_on_net,0)
FROM dbo.dumpfile_doroud t1
LEFT JOIN dbo.dumpfile139201 t2
ON t2.msisdn = t1.msisdn
LEFT JOIN dumpfile139202 t3
ON t3.msisdn = t1.msisdn
LEFT JOIN dumpfile139203 t4
ON t4.msisdn = t1.msisdn
I don't think it's possible to make faster query, so you can try put indexes. I think you can create nonclustered index on column msisdn on all tables. Syntax:
CREATE NONCLUSTERED INDEX IX_doroud_dumpfile139201
ON dbo.dumpfile139201(msisdn);
You can run SQL Management studio and turn on display estimated execution plan this sometimes gives good advices on creating indexes.
Create a subquery to calculate the totals then join the table to it
UPDATE o
SET o.sms_count_on_net = n.sms_count_on_net
FROM
dbo.dumpfile_doroud o
JOIN
(SELECT
d.msisdn, sms_count_on_net = (d1.sms_count_on_net+d2.sms_count_on_net+d3.sms_count_on_net)
FROM
dbo.dumpfile_doroud d
LEFT JOIN dbo.dumpfile139201 d1 ON d1.msisdn = d.msisdn
LEFT JOIN dbo.dumpfile139202 d2 ON d2.msisdn = d.msisdn
LEFT JOIN dbo.dumpfile139203 d3 ON d3.msisdn = d.msisdn) n
ON o.msisdn = n.msisdn
Note that if the value is missing from any of those tables the total will be null. That may or may not be what you want
I've made a Join between tables. I simply want to take the values from some data and put into the other columns. But it doesn't work.
How could I rewrite the query?
I want to OVERWRITE the values in wkfc table with the values from swkf.
I know it's right how can I make Oracle simply do it?
UPDATE (
SELECT --FROM
swkf.swkf_stato_workflow_id "swkf_swkf_stato_workflow_id",
swkf.swkf_data_ini "swkf_swkf_data_ini",
swkf.swkf_versione "swkf_swkf_versione",
swkf.spwkf_stato_pubblico_id "swkf_spwkf_stato_pubblico_id",
swkf.spwkf_data_ini "swkf_spwkf_data_ini",
swkf.spwkf_versione "swkf_spwkf_versione",
--TO
wkfc.swkf_stato_workflow_id "wkfc_swkf_stato_workflow_id",
wkfc.swkf_data_ini "wkfc_swkf_data_ini",
wkfc.swkf_versione "wkfc_swkf_versione",
wkfc.spwkf_stato_pubblico_id "wkfc_spwkf_stato_pubblico_id",
wkfc.spwkf_data_ini "wkfc_spwkf_data_ini",
wkfc.spwkf_versione "wkfc_spwkf_versione"
--
FROM wkfb_stati_workflow swkf, wkf_cronologia wkfc
WHERE twkf_tipo_workflow_id =
(SELECT twkf_tipo_workflow_id
FROM wkf_istanze_workflow wkfi, RET_PUNTI_EROGAZIONE RPUN
WHERE RPUN.PUN_PUNTO_EROGAZIONE_COD = '8001375567' --codice puntero
AND RPUN.PUN_PUNTO_EROGAZIONE_ID = wkfi.ogg_oggetto_id
AND wkfi.tog_tipo_oggetto_id = 'RET_PUN1'
AND wkfi.WKFI_FLAG_ANN = 'N')
AND swkf_descrizione = '(O)Occupato'
AND wkfc.wkfc_cronologia_id = 'ApAJ0qCudNphjLxj'
) a1
set
"wkfc_swkf_stato_workflow_id" = "swkf_swkf_stato_workflow_id" ,
"wkfc_swkf_data_ini" = "swkf_swkf_data_ini" ,
"wkfc_swkf_versione" = "swkf_swkf_versione" ,
"wkfc_spwkf_stato_pubblico_id" = "swkf_spwkf_stato_pubblico_id" ,
"wkfc_spwkf_data_ini" = "swkf_spwkf_data_ini" ,
"wkfc_spwkf_versione" = "swkf_spwkf_versione" ;
It's the same of doing as follow, but he have to find the values by itself.
UPDATE wkf_cronologia
SET swkf_stato_workflow_id = 'o3gE1tlSdcDIC6FF',
swkf_data_ini = TO_TIMESTAMP ('19-06-2010 18:28:10,556000000','DD-MM-RRRR HH24:MI:SS,FF'),
swkf_versione = 0,
SPWKF_STATO_PUBBLICO_ID = '*1UNICOO',
SPWKF_DATA_INI = TO_TIMESTAMP ('01-01-0001 00:00:00,000000000', 'DD-MM-RRRR HH24:MI:SS,FF'),
SPWKF_VERSIONE = 0
WHERE wkfc_cronologia_id = 'ApAJ0qCudNphjLxj'; --id del record di cronologia da aggiornare (estratto nella prima query)
If you want to UPDATE (SELECT ... FROM A INNER JOIN B ON condition) SET A.X = ... then the join condition must constrain all the columns of some uniqueness constraint on B to a single value.
Firstly We cannot update two tables using single query through join view, We can opt DB Procedure in that case.
ORA-01776: cannot modify more than one base table through a join view
01776. 00000 - "cannot modify more than one base table through a join view"
*Cause: Columns belonging to more than one underlying table were either
inserted into or updated.
But we can use join to compare the base table with reference table and we can update one table.
UPDATE
(
SELECT t1.d1 x, t2.d1 y FROM t1, t2 WHERE t1.d2 = t2.d2 AND t2.d2 = 2
) t3
SET t3.x = 6;
-- t3.y = 1; this is wrong updating multiple table
Now performing this join (t1.d2 = t2.d2) should happen on unique columns.
Here both tables d2 column should be unique.
Else result in below
Oracle: multiple table updates => ORA-01779:
cannot modify a column which maps to a non key-preserved table
Below URL explains how to perform this join on non unique columns
Oracle - update join - non key-preserved table