Oracle SQL: Update a column from select - mysql

I have to update SRCFILE_FULL_COUNT_HIGH. Set new value from NOVA_HODNOTA. Here is working select.
Thank you!
UPDATE SRCFILE_ID, NOVA_HODNOTA, SRCFILE_FULL_COUNT_HIGH as STARA_HODNOTA
from
(
working select:
SELECT /*+ no_index(rh FILEINS_STATUS_FK_I) */
rh.SRCFILE_ID, rh.FILEINS_RECORD_COUNT, rh.FILEINS_EFFECTIVE_DATE, esf.SRCFILE_FULL_COUNT_HIGH,
100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH) as PROCENTA,
CASE
WHEN (100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH)) >= 80
THEN FILEINS_RECORD_COUNT*1.25
ELSE SRCFILE_FULL_COUNT_HIGH
END AS NOVA_HODNOTA
FROM ETL_SOURCE_FILE_INST rh,
(SELECT MAX(FILEINS_EFFECTIVE_DATE) AS maxdate, SRCFILE_ID
FROM ETL_SOURCE_FILE_INST
GROUP BY SRCFILE_ID) maxresults,
ETL_SOURCE_FILES esf
WHERE rh.SRCFILE_ID = maxresults.SRCFILE_ID
and rh.SRCFILE_ID = esf.SRCFILE_ID
AND rh.FILEINS_EFFECTIVE_DATE= maxresults.maxdate
AND RH.FILEINS_INCREMENTAL_FLAG = 'F'
and ESF.SRCFILE_FULL_COUNT_FLAG = 'Y'
and RH.FILEINS_STATUS = 'COMPLETE'
ORDER BY SRCFILE_ID ASC
END of select
)
WHERE STARA_HODNOTA = SRCFILE_FULL_COUNT_HIGH
SET STARA_HODNOTA = NOVA_HODNOTA
;

merge into
ETL_SOURCE_FILE_INST i
using
(
SELECT /*+ no_index(rh FILEINS_STATUS_FK_I) */
rh.SRCFILE_ID, rh.FILEINS_RECORD_COUNT, rh.FILEINS_EFFECTIVE_DATE, esf.SRCFILE_FULL_COUNT_HIGH,
100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH) as PROCENTA,
CASE
WHEN (100*(rh.FILEINS_RECORD_COUNT/esf.SRCFILE_FULL_COUNT_HIGH)) >= 80
THEN FILEINS_RECORD_COUNT*1.25
ELSE SRCFILE_FULL_COUNT_HIGH
END AS NOVA_HODNOTA
FROM ETL_SOURCE_FILE_INST rh,
(SELECT MAX(FILEINS_EFFECTIVE_DATE) AS maxdate, SRCFILE_ID
FROM ETL_SOURCE_FILE_INST
GROUP BY SRCFILE_ID) maxresults,
ETL_SOURCE_FILES esf
WHERE rh.SRCFILE_ID = maxresults.SRCFILE_ID
and rh.SRCFILE_ID = esf.SRCFILE_ID
AND rh.FILEINS_EFFECTIVE_DATE= maxresults.maxdate
AND RH.FILEINS_INCREMENTAL_FLAG = 'F'
and ESF.SRCFILE_FULL_COUNT_FLAG = 'Y'
and RH.FILEINS_STATUS = 'COMPLETE'
) t on (t.SRCFILE_ID = i.SRCFILE_ID)
when matched then
update
set
i.SRCFILE_FULL_COUNT_HIGH = t.NOVA_HODNOTA
where
i.SRCFILE_FULL_COUNT_HIGH = t.SRCFILE_FULL_COUNT_HIGH
;

Related

Output of 3 queries

Got this 3 in 1 query:
SELECT * FROM
(
SELECT mesures.date j, AVG(mesures.valeur) maxi
FROM mesures
JOIN plages_horaire ON mesures.id_plage = plages_horaire.id_plage
WHERE MONTH(mesures.date) = '9' AND YEAR(mesures.date) = '2016' AND mesures.code_station = 'P02SE' AND mesures.id_crit = '1' AND mesures.id_type = '1'
GROUP BY mesures.date
) maxi
,
(
SELECT AVG(mesures.valeur) mini
FROM mesures
JOIN plages_horaire ON mesures.id_plage = plages_horaire.id_plage
WHERE MONTH(mesures.date) = '9' AND YEAR(mesures.date) = '2016' AND mesures.code_station = 'P02SE' AND mesures.id_crit = '1' AND mesures.id_type = '2'
GROUP BY mesures.date
) mini
,
(
SELECT AVG(mesures.valeur) moy
FROM mesures
JOIN plages_horaire ON mesures.id_plage = plages_horaire.id_plage
WHERE MONTH(mesures.date) = '9' AND YEAR(mesures.date) = '2016' AND mesures.code_station = 'P02SE' AND mesures.id_crit = '1' AND mesures.id_type = '3'
GROUP BY mesures.date
) moy
GROUP BY j
Problem is that I get what I want excepting values of the 2 last columns are the same at every rows:
query output
I believe it's because of the GROUP BY.
From what I can see from your query, you don't need the plage_horaire table. You can also simplify the logic greatly by using conditional aggregation:
SELECT m.date,
AVG(CASE WHEN m.id_type = 1 THEN m.valeur END) maxi,
AVG(CASE WHEN m.id_type = 2 THEN m.valeur END) mini,
AVG(CASE WHEN m.id_type = 3 THEN m.valeur END) maxmoy,
FROM mesures m
WHERE MONTH(m.date) = 9 AND YEAR(m.date) = 2016 AND
m.code_station = 'P02SE' AND m.id_crit = 1 AND m.id_type IN (1, 2, 3)
GROUP BY m.date ;
Notice that I also removed the quotes from the numeric constants. MONTH() and YEAR() return numbers, so quotes are not appropriate. I am guessing that the ids are numeric as well.

Why the query is giving result while the destCurrency is not 0 -

SELECT *
FROM xyz
WHERE applyat = 'anita'
AND applyAt = 'Create'
AND country = '50'
AND dest_country = '108'
AND originCurrency = 'EUR'
AND destCurrency = 0 AND isEnable = 'Y'
AND agentID = 0
AND Module = 'abc'
ORDER BY ruleID DESC
You should add quotes as it is a varchar
SELECT * FROM xyz WHERE applyat = 'anita' AND applyAt = 'Create' AND country = '50' AND dest_country = '108' AND originCurrency = 'EUR' AND destCurrency = '0' AND isEnable = 'Y' And agentID = 0 AND Module = 'abc' ORDER BY ruleID DESC

filter rows based on column values in mysql query

This is my mysql query
SELECT a.model_name,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
ORDER BY a.model_created_on DESC
I want to do one more filtering option in this query. I need to get the records based on release_year = 1 & release_year = 1. I have done release_year and release_month columns through IF STATEMENT in MYSQL QUERY
release_year
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year
release_month
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
How do I get the records based on these values (release_month = 1 & release_year = 1) in this query? I have tried WHERE release_month = 1 AND release_year = 1 but this one returns unknown column
You could do this:
SELECT
*
FROM
(
SELECT
a.model_name,
a.model_created_on,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
) AS tbl
WHERE tbl.release_year=1 AND release_month=1
ORDER BY tbl.model_created_on DESC

Tuning Slow Performing Queries

I have a query which is taking too long to execute.
I used database tuning advisor to check this query and it suggested some missing indexes and statistics. The problem is I can't create missing index and statistics on those tables because it will slowdown insert/update and affect other scripts. They can't sacrifice their scripts performance because of my query.
Without the help of DTA or disturbing other scripts how do I have to tune my query? Can i break it into small pieces? If so, how?
INSERT INTO #val
SELECT lid.orgid,
lid.periodid,
lid.sourceid,
lid.statementtypecode,
lid.financialsbucketid,
lid.statementcurrencycodeiso,
lid.lineitemid,
lll.financialconceptidglobal,
lid.physicalmeasureid,
CASE
WHEN EXISTS(SELECT TOP 1 '1'
FROM trf.dbo.lineitemfundbdescription LIFD(nolock)
WHERE lll.orgid = lifd.orgid
AND lll.lineitemid = lifd.lineitemid) THEN
(SELECT lifd.lineitemshortdescription
FROM trf.dbo.lineitemfundbdescription LIFD(nolock)
WHERE lll.orgid = lifd.orgid
AND lll.lineitemid = lifd.lineitemid)
ELSE lll.lineitemname
END AS LineItemName,
( CASE
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND
lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 1 ) THEN
lid.lineiteminstancevalue *
cds.dbo.Exrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, p.periodenddate)
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 0
AND p.periodlengthunitcode = 'M' ) THEN lid.lineiteminstancevalue
*
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso,
Dateadd("MONTH", -p.periodlength, p.periodenddate), p.periodenddate)
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 0
AND p.periodlengthunitcode = 'W' ) THEN lid.lineiteminstancevalue
*
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso,
Dateadd("WEEK", -p.periodlength, p.periodenddate), p.periodenddate)
ELSE lid.lineiteminstancevalue
END ) AS LineItemInstanceValue,
( CASE
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 1 ) THEN
lid.adjustedforcorporateactionvalue *
cds.dbo.Exrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso, p.periodenddate)
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 0
AND p.periodlengthunitcode = 'M' ) THEN
lid.adjustedforcorporateactionvalue
*
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso,
Dateadd("MONTH", -p.periodlength, p.periodenddate), p.periodenddate)
WHEN ( lid.reportedcurrencycodeiso IS NOT NULL
AND fc.iscurrencydependent = 1
AND lid.statementcurrencycodeiso <> lid.reportedcurrencycodeiso
AND fc.isflowitem = 0
AND p.periodlengthunitcode = 'W' ) THEN
lid.adjustedforcorporateactionvalue
*
cds.dbo.Getaveragefxrate(lid.reportedcurrencycodeiso, lid.statementcurrencycodeiso,
Dateadd("WEEK", -p.periodlength, p.periodenddate), p.periodenddate)
ELSE lid.adjustedforcorporateactionvalue
END ) AS AdjustedForCorporateActionValue,
lid.reportedcurrencycodeiso,
lid.xbrlelementid,
xbrlele.xbrlelementname,
Cast(NULL AS CHAR(3)),
Cast(NULL AS DATETIME),
Cast(NULL AS DATETIME),
Cast(NULL AS CHAR(1)),
Cast(NULL AS DATETIME),
Cast(NULL AS SMALLINT),
src.dcn,
src.docformat,
lid.asreporteditemid,
ari.docbyteoffset,
ari.docbytelength,
ari.bookmark,
ari.itemdisplayednegativeflag,
ari.itemscalingfactor,
ari.itemdisplayedvalue,
ari.reportedvalue,
ari.reporteddescription,
ari.editeddescription,
src.documentid,
lid.isderived,
lid.statementsectioncode,
IsMissMatchPhysicalMeasureID =0,
lid.istotal,
lid.isexcludedfromstandardization,
si.isdetailed AS IsDetailedSection,
si.ispreliminary AS IsPreliminary,
IsCreditSection =Cast(NULL AS BIT),
IsCreditFCC =Cast(NULL AS BIT),
si.isproforma,
lll.instrumentndaid,
InterimTypeID = CASE p.periodicitycode
WHEN 'A' THEN 0
WHEN 'S' THEN 2
WHEN 'T' THEN 3
WHEN 'Q' THEN 4
END,
si.isnotcomparabletopriorperiod,
si.isfundbspecial,
si.isderived AS IsDerivedSI,
lid.systemderivedtypecode
--FBLog.tasktypeid,
--FBLog.systemstartdatetime,
--FBLog.issplit
FROM trf.dbo.lineiteminstance LID(nolock)
--INNER JOIN #fundbbackwardlog FBLog
-- ON FBLog.orgid = lid.orgid
-- AND FBLog.periodid = lid.periodid
-- AND FBLog.sourceid = lid.sourceid
-- AND FBLog.statementtypecode = lid.statementtypecode
-- AND FBLog.financialsbucketid = lid.financialsbucketid
-- AND FBLog.statementcurrencycodeiso =
-- lid.statementcurrencycodeiso
-- AND lid.asreporteditemid IS NOT NULL
-- AND lid.lineiteminstanceasreporteditemid IS NULL
INNER JOIN trf.dbo.statementinstance SI(nolock)
ON lid.orgid = si.orgid
AND lid.periodid = si.periodid
AND lid.sourceid = si.sourceid
AND lid.statementtypecode = si.statementtypecode
AND lid.financialsbucketid = si.financialsbucketid
AND lid.statementcurrencycodeiso = si.statementcurrencycodeiso
INNER JOIN trf.dbo.period P(nolock)
ON lid.orgid = p.orgid
AND lid.periodid = p.periodid
INNER JOIN trf.dbo.lineitem LLL(nolock)
ON lll.orgid = lid.orgid
AND lll.lineitemid = lid.lineitemid
INNER JOIN trf.dbo.financialconcept FC(nolock)
ON lll.financialconceptidglobal = fc.financialconceptid
LEFT OUTER JOIN trf.dbo.xbrlelement XBRLEle(nolock)
ON lid.xbrlelementid = xbrlele.xbrlelementid
INNER JOIN trf.dbo.asreportedinstance ARI(nolock)
ON lid.orgid = ari.orgid
AND lid.sourceid = ari.sourceid
AND lid.asreporteditemid = ari.asreporteditemid
INNER JOIN trf.dbo.[source] SRC(nolock)
ON src.orgid = ari.orgid
AND src.sourceid = ari.sourceid
WHERE ari.reportedvalue IS NOT NULL --AND SRC.DCN > '' --AND SRC.DocFormat > '' --AND ARI.BookMark > ''
Try to remove functions called in the query like : Exrate() and Getaveragefxrate().
Also change the case statement, where you are using subquery as following.
In the end of the query, use
left outer join trf.lineitemfundbdescription lifd
on lll.orgid = lifd.orgid and lll.lineitemid = lifd.lineitemid
and then the case will be changed to
case
when lifd.orgid is null then lll.lineitemname
else lifd.lineitemshortdescription End As LineItemName
It will fine tune your query to a level and gives you a correct execution plan and advice accordingly.
Remember that execution plan does not show the plan of user defined functions called with in the query.

Error in last line near end

While i Execute a stored procedure I don't know why showing error in last line...
I can't find any error with it
The error is telling
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 60
_
CREATE PROCEDURE `PartyBalanceViewByLedgerId`
(
p_ledgerId varchar(50),
p_crOrDr varchar(50),
p_branchId varchar(50)
)
BEGIN
IF (p_crOrDr='Dr')
THEN
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType AS ID,
TEMP.voucherType,
CASE WHEN (TEMP.voucherType = 'Receipt Voucher')
THEN
(SELECT receiptMasterId FROM tbl_ReceiptMaster
WHERE (receiptMasterId = TEMP.voucherNo))
ELSE
(SELECT purchaseMasterId FROM tbl_PurchaseMaster
WHERE (purchaseMasterId = TEMP.voucherNo))
END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2)) AS char(27))
AS amount
FROM(SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.credit, 0)) - SUM(ifnull(A.debit, 0)))
AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Purchase Invoice'
OR A.voucherType = 'Receipt Voucher')
AND A.ledgerId=p_ledgerId
AND A.branchId=p_branchId
AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0 ;
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,
CASE WHEN TEMP.voucherType = 'Payment Voucher' THEN
(SELECT paymentMasterId FROM tbl_PaymentMaster WHERE (paymentMasterId = TEMP.voucherNo))
ELSE
(SELECT salesInvoiceNo FROM tbl_SalesMaster WHERE (salesMasterId = TEMP.voucherNo)) END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2))AS char(27)) AS amount
FROM(
SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.debit, 0)) - SUM(ifnull(A.credit,0))) AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Sales Invoice' OR A.voucherType = 'Payment Voucher'OR A.voucherType = 'Job Invoice') AND A.ledgerId=p_ledgerId AND A.branchId=p_branchId AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance > 0 ;
END
try with this i have update some changes END IF and ; etc
CREATE PROCEDURE `PartyBalanceViewByLedgerId`
(
p_ledgerId varchar(50),
p_crOrDr varchar(50),
p_branchId varchar(50)
)
BEGIN
IF (p_crOrDr='Dr')
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType AS ID,
TEMP.voucherType,
CASE WHEN (TEMP.voucherType = 'Receipt Voucher')
THEN
(SELECT receiptMasterId FROM tbl_ReceiptMaster
WHERE (receiptMasterId = TEMP.voucherNo))
ELSE
(SELECT purchaseMasterId FROM tbl_PurchaseMaster
WHERE (purchaseMasterId = TEMP.voucherNo))
END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2)) AS char(27))
AS amount
FROM(SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.credit, 0)) - SUM(ifnull(A.debit, 0)))
AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Purchase Invoice'
OR A.voucherType = 'Receipt Voucher')
AND A.ledgerId=p_ledgerId
AND A.branchId=p_branchId
AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0;
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,
CASE WHEN TEMP.voucherType = 'Payment Voucher' THEN
(SELECT paymentMasterId FROM tbl_PaymentMaster WHERE (paymentMasterId = TEMP.voucherNo))
ELSE
(SELECT salesInvoiceNo FROM tbl_SalesMaster WHERE (salesMasterId = TEMP.voucherNo)) END AS voucherNo,
CAST(CAST(TEMP.balance AS DECIMAL(24,2))AS char(27)) AS amount
FROM(
SELECT
A.voucherNo,
A.voucherType,
(SUM(ifnull(A.debit, 0)) - SUM(ifnull(A.credit,0))) AS balance
FROM tbl_PartyBalance AS A
WHERE (A.voucherType = 'Sales Invoice' OR A.voucherType = 'Payment Voucher'OR A.voucherType = 'Job Invoice') AND A.ledgerId=p_ledgerId AND A.branchId=p_branchId AND A.optional='False'
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance > 0 ;
END IF;
END;
Are you using a DELIMITER? If not pl go through this description:
Delimiters in MySQL
I hope this will solve your problem.
Remove the semicolon before ELSE in :
GROUP BY A.voucherNo,A.voucherType
)AS TEMP
WHERE TEMP.Balance>0
ELSE
SELECT
TEMP.voucherNo +'_'+ TEMP.voucherType ID,
TEMP.voucherType,