Update statement with join on Oracle - sql

I have this query but it's not working. Can you tell me the solution?
update temp_msisdn_saldo
set saldo=(
select sum(saldo) from (
select a.msisdn as mdn, a.saldo from temp_msisdn_saldo a
union all
select b.mdn as mdn, b.saldo from temp_msisdn_saldo_yesterday b
) z
)
where msisdn=z.mdn

You can rewrite it as :
update temp_msisdn_saldo
set saldo = ( select sum(saldo)
from (select a.msisdn as mdn, a.saldo
from temp_msisdn_saldo a
union all
select b.mdn as mdn, b.saldo
from temp_msisdn_saldo_yesterday b
) z
where z.mdn = temp_msisdn_saldo.msisdn
);
Same you can use CTE :
with cte as (
<your query here>
)
update temp_msisdn_saldo
set saldo = (select sum(c.saldo) from cte c where c.mdn = temp_msisdn_saldo.msisdn);

Related

How to optimize this UNION SQL Query?

I have a query which looks like this:
SELECT DISTINCT * FROM (
SELECT FundId AS Id,
PeriodYearMonth
FROM [Fund.Period] F
INNER JOIN (
SELECT * FROM (
SELECT FundId as Id,
MIN(PeriodYearMonth) AS MinPeriodYearMonth
FROM (
SELECT FundId,
PeriodYearMonth,
PublishedOn
FROM [Fund.Period] FP
UNION ALL --Changed to UNION ALL as it is more efficient and we wont ever need a UNION as the result set would never match
SELECT FundId,
MAX(PeriodYearMonth) + 1,
NULL
FROM [Fund.Period]
GROUP BY FundId
) FP WHERE PublishedOn IS NULL GROUP BY FundId
) MFP
) FP ON F.FundId = FP.Id AND (F.PeriodYearMonth = FP.MinPeriodYearMonth OR (f.PeriodYearMonth +1) = FP.MinPeriodYearMonth)
) FP
If possible, I would like to remove the UNION ALL. Does anyone know how this can be optimized?
removed union all
SELECT DISTINCT * FROM (
SELECT FundId AS Id,
PeriodYearMonth
FROM [Fund.Period] F
INNER JOIN (
SELECT * FROM (
SELECT FundId as Id,
MIN(PeriodYearMonth) AS MinPeriodYearMonth,MAX(PeriodYearMonth) + 1 as PeriodYearMonth
FROM (
SELECT FundId,
PeriodYearMonth,
PublishedOn
FROM [Fund.Period] FP
) FP WHERE PublishedOn IS NULL
GROUP BY FundId
) MFP
) FP ON F.FundId = FP.Id AND (F.PeriodYearMonth = FP.MinPeriodYearMonth OR (f.PeriodYearMonth +1) = FP.MinPeriodYearMonth)
) FP

Alternative solution for below SQL Query ,TableMasterID NOT IN take so much time

Need alternative solution for below SQL Query ,TableMasterID NOT IN take so much time, If i remove AND TableMasterID NOT IN
(SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
WHERE C.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) text from below query then getting result in 20 seconds otherwise result getting arround 4 minus.
SELECT A.SubTitleId,TableMasterID from SubTitle as A JOIN ComTableMaster as B ON a.SubTitle = b.TblName AND TableMasterID NOT IN (SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
WHERE C.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) AND B.TableMasterID IN
(SELECT DISTINCT d.TableMasterID
FROM ComData as D
WHERE D.ComFileID IN
(SELECT Number FROM fn_SplitInt(#ComFileID,','))) ORDER BY A.MainTitleID
SELECT A.SubTitleId ,
TableMasterID
FROM SubTitle AS A
JOIN ComTableMaster AS B ON A.SubTitle = B.TblName
WHERE NOT EXISTS ( SELECT 1
FROM ComFinalDataBS C
WHERE TableMasterID = C.TableMasterID
AND C.ComFileID IN (
SELECT Number
FROM MEFCampus..fn_SplitInt(#ComFileID, ',') ) )
AND NOT EXISTS ( SELECT 1
FROM ComData D
WHERE TableMasterID = D.TableMasterID
AND D.ComFileID IN (
SELECT Number
FROM MEFCampus..fn_SplitInt(#ComFileID,
',') ) )
AND B.IsDeleted = 0
ORDER BY MainTitleID
Have you tried storing the result from the fn_split_string() into an indexed temp-table first? It should help the Query Optimizer a lot.
SELECT DISTINCT Number
INTO #ComFileID
FROM dbo.fn_SplitInt(#ComFileID,',')
CREATE UNIQUE CLUSTERED INDEX uq0_ComFileID ON #ComFileID (Number) WITH (FILLFACTOR = 100)
SELECT A.SubTitleId,TableMasterID
FROM SubTitle as A
JOIN ComTableMaster as B
ON a.SubTitle = b.TblName
/*
AND B.TableMasterID NOT IN (SELECT DISTINCT c.TableMasterID
FROM ComFinalDataBS as C
JOIN #ComFileID CFI
ON CFI.Number = C.ComFileID )
*/
AND NOT EXISTS ( SELECT *
FROM ComFinalDataBS as C
JOIN #ComFileID CFI
ON CFI.Number = C.ComFileID
WHERE c.TableMasterID = B.TableMasterID )
/*
AND B.TableMasterID IN (SELECT DISTINCT d.TableMasterID
FROM ComData as D
JOIN #ComFileID CFI
ON CFI.Number = D.ComFileID
*/
AND EXISTS ( SELECT *
FROM ComData as D
JOIN #ComFileID CFI
ON CFI.Number = D.ComFileID
WHERE D.TableMasterID = B.TableMasterID )
ORDER BY A.MainTitleID

Oracle Table alias in join

is it possible to use the table alias in an ON-Statement?
SQL Statement:
SELECT XMLELEMENT("row", XMLATTRIBUTES(rownum as "order"),
(
SELECT
XMLAGG(XMLELEMENT("attribute",XMLATTRIBUTES(z as "identifier") ) )
FROM b
LEFT JOIN c
ON c.ID = b.cID
AND c.example = table_alias.example
)
)
FROM
(
SELECT example FROM x ORDER BY y
) table_alias
I'm getting the error that table_alias.example is an invalid identifier.
If I move the c.example = table_alias.example into a WHERE Statement it works, but of course I'll get the wrong result.
Anyone have an idea?
TY
frgtv10
try this
SELECT XMLELEMENT("row", XMLATTRIBUTES(rownum as "order"),
( SELECT XMLAGG(XMLELEMENT("attribute"),XMLATTRIBUTES(z as "identifier") ) )
FROM b, c, x
where c.ID = b.cID(+) AND c.example = x.example
)
) from dual

What does ")x" mean in a query?

I have the following stored procedure (in MS SQL):
SET NOCOUNT ON
DECLARE #Cuantos INT
IF EXISTS
(
SELECT TOP 1 * FROM IntProgramas WHERE cod_programa IN
( SELECT cod_programa FROM IntGrupo_programa WHERE cod_grupo IN
( SELECT cod_grupo FROM IntUsuarios WHERE cod_usuario = #cod_usuario
)
)
)
BEGIN
SET nocount ON
SELECT P.cod_programa
,nb_programa
,descripcion
,secuencia
,P.Accion
,P.Controlador
INTO #mitabla1
FROM IntProgramas P
WHERE P.cod_programa
IN (
SELECT cod_programa FROM Intgrupo_programa WHERE cod_grupo IN
(
SELECT cod_grupo FROM Intusuarios WHERE cod_usuario=#cod_usuario
)
)
SET nocount ON
SELECT GP.cod_programa
,P.nb_programa
,P.descripcion
INTO #mitabla2
FROM IntGrupo_Programa GP
JOIN Intprogramas P on GP.cod_programa = P.cod_programa
WHERE GP.cod_grupo IN (SELECT cod_grupo FROM Intusuarios WHERE cod_usuario=#cod_usuario
SELECT #Cuantos = COUNT(*)
FROM( SELECT nb_programa, descripcion FROM IntProgramas
WHERE cod_programa in (select cod_programa from #mitabla1
union select cod_programa from #mitabla2))x
/*si existe en ambas macheo*/
SELECT nb_programa, descripcion, P.cod_programa
INTO #mitabla3
FROM IntProgramas P
WHERE cod_programa in (SELECT cod_programa FROM #mitabla1
union SELECT cod_programa FROM #mitabla2)
select
t.nb_programa
, t.descripcion
, t.cod_programa
, p.secuencia
, ISNULL(et.cod_menu,0) as cod_menu
, ISNULL(et.desc_menu,0) as desc_menu
, ISNULL(et_sprog.cod_sub_menu_programa,0) AS cod_sub_menu_programa
, ISNULL(et_sprov.desc_sub_menu,0) AS desc_sub_menu_N2
, p.Accion
, p.Controlador
from #mitabla3 t
JOIN IntProgramas p
ON t.cod_programa = p.cod_programa
LEFT JOIN IntEstructura_sub_menu_programa et_sprog
ON t.cod_programa = et_sprog.cod_programa
LEFT JOIN IntEstructura_menu_Usuarios et
ON et_sprog.cod_menu = et.cod_menu
LEFT JOIN IntEstructura_sub_menu_Usuarios et_sprov
ON et_sprog.cod_sub_menu_programa = et_sprov.cod_sub_menu
WHERE et_sprog.cod_programa IS NOT NULL
order by et.cod_menu, et_sprog.cod_sub_menu_programa, p.secuencia
--gp.cod_grupo,
drop table #mitabla1
drop table #mitabla2
drop table #mitabla3
END
i am trying to understand it but when I get to the line
union select cod_programa from #mitabla2))x
I don't understand what the x does any help would be apreciated.
I have been trying to run the SP in parts to better understand the flow but that line has really complicated things for me.
X is an alias for this sub-query or derived table:
( SELECT nb_programa, descripcion FROM IntProgramas
WHERE cod_programa in (select cod_programa from #mitabla1
union select cod_programa from #mitabla2))
It's an alias for the subquery:
select #Cuantos = COUNT(*)
from (
select nb_programa, descripcion
from IntProgramas
where cod_programa in (
select cod_programa
from #mitabla1
union
select cod_programa
from #mitabla2
)
) x
When you do SELECT FROM (SELECT... you have to name the subquery / derived table.
It works as if you were doing:
select #Cuantos = COUNT(*)
from x
Unlike an alias in a table name that is used mostly to make it easier to read or to not have to repeat the table name, in this case an alias is mandatory.
X is the alias for the results of the sub-query. There should really be a space before it though

SQL - UNION, priority on the first select statement when doing order by

I'm trying to print out the results from the "GermanDB" Database first, while also showing everything from the Boston DB that was not in the German database. Can this be done in one query?
My query (the bold part functions but does not order the way I want)
select * from (
SELECT DISTINCT a.ProductRef
FROM GERMANDB.dbo.LOCATIONS AS a INNER JOIN GERMANDB.dbo.ITEMS AS b ON a.ProductRef = b.ProductRef
WHERE b.ACTIVE=1
) ta
UNION select * from
SELECT DISTINCT c.ProductRef
FROM BOSTONDB.dbo.LOCATIONS AS c INNER JOIN BOSTONDB.dbo.ITEMS AS d ON c.ProductRef = d.ProductRef
WHERE c.ACTIVE=1 (c.ProductRef NOT IN
(SELECT ProductRef FROM GERMANDB.dbo.ITEMS where ACTIVE=1))
) tb
order by ta.ProductRef** , tb.productRef
Just add one field to signal the priority. Like this:
select *, 0 as Priority from (
SELECT DISTINCT a.ProductRef
FROM GERMANDB.dbo.LOCATIONS AS a INNER JOIN GERMANDB.dbo.ITEMS AS b ON a.ProductRef = b.ProductRef
WHERE b.ACTIVE=1
) ta
UNION select *, 1 as Priority from
SELECT DISTINCT c.ProductRef
FROM BOSTONDB.dbo.LOCATIONS AS c INNER JOIN BOSTONDB.dbo.ITEMS AS d ON c.ProductRef = d.ProductRef
WHERE c.ACTIVE=1 (c.ProductRef NOT IN
(SELECT ProductRef FROM GERMANDB.dbo.ITEMS where ACTIVE=1))
) tb
order by Priority, ProductRef