help in the CASE statement
Hello I want to put a condition in CASE the statement translated which excludes zerios.SELECT
CASE WHEN substr(a.gbobj,1,1) < 4
THEN (a.GBAPYC + a.GBAN01 + a.GBAN02 + a.GBAN03 + a.GBAN04 + a.GBAN05 +)
a.GBAN06+a.GBAN07)/100 ELSE a.GBAN07/100 END FINALNUMBERS
OF MMSDM. SRC_E1_MMS_F0902 one
WHERE
A.GBCTRY <>0 AND A.GBLT = 'AA' and a.gbfy = "11"
My results are
FINALNUMBERS
0
122323.34
45456
How don't exclude zero record in my results? I tried to not equal to "0" FINALNUMBERS in which the clause, but it did not work.
Thank you
Published by: user610131 on October 27, 2011 13:49
Published by: user610131 on October 27, 2011 13:50
user610131 wrote:
Thank you guys. This has been useful.I understand what Solomon did, but I'm confused when I need to run the full query. How to do this?
I do a query online when I have to pull additional fields.
You need a subquery to reference the alias finalnumbers.
Do everything except the test for finalnumbers (and cklause ORDER, of course) in the subquery. view online, such as used Salomon or a WITH clause that:
WITH got_finalnumbers AS
(
SELECT 'AA' Ledger, CASE WHEN a.GBCO='00001' THEN 'C86001' ELSE 'C' || Trim(a.GBCO) END Company,
'G000' Region,
CASE WHEN length(TRIM(a.GBMCU))=7 AND substr(TRIM(a.GBMCU),6,2)='00' AND substr(TRIM(a.GBMCU),1,1)<>'4'
THEN 'GS'|| TRIM(a.GBMCU) ELSE 'G'|| TRIM(a.GBMCU) END departments,
'E' || a.gbobj Accounts,
CASE WHEN d.SUB_CLASS IS NULL THEN 'NA' ELSE d.code END market,
a.gbfy Cal_year,
sum(CASE WHEN substr(a.gbobj,1,1)<4 THEN (a.GBAPYC+a.GBAN01+a.GBAN02+a.GBAN03+a.GBAN04+a.GBAN05+
a.GBAN06+a.GBAN07)/100 ELSE a.GBAN07/100 END) FINALNUMBERS
FROM MMSDM.SRC_E1_MMS_F0902 A LEFT OUTER JOIN MMSDM.SRC_E1_MMS_F0901 B
ON (A.GBCO = B.GMCO) AND (A.GBAID = B.GMAID)
LEFT OUTER JOIN mmsdm.src_e1_mms_f0006 c ON a.gbmcu=c.mcmcu
LEFT OUTER JOIN getcb30.MMS_GL_SUB_CLASS d ON trim(c.MCRP11)=d.SUB_CLASS
WHERE A.GBCTRY<>0
AND A.GBLT='AA' and a.gbfy = '11'
GROUP BY a.gbco, trim(a.gbmcu), a.gbobj, a.gbfy, d.SUB_CLASS, d.code
)
SELECT *
FROM got_finalnumbers
WHERE finalnumbers != 0
ORDER BY GBOBJ;
Tags: Database
Similar Questions
-
Help in the CASE statement with amount
Hello Experts
I amw Group on
Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production
With partitioning, OLAP, Data Mining and Real Application Testing options
I'm unable to solve this Case statement.
My requirement is that if the value of the attribute is greater than 50, then add 1 to the sum for attribute 4 the sum should be 4
WITH T AS
(
Select MATH_CONV_SCR 70, 68 MATH_PERC, writ_conv_scr 66, 67 writ_per Union double all the
Select MATH_CONV_SCR 70, MATH_PERC 48, writ_conv_scr 66, 67 writ_per Union double all the
Select MATH_CONV_SCR 70, 68 MATH_PERC, writ_conv_scr 66, 67 double writ_per
)
(
Select)
CASE
WHEN MATH_CONV_SCR > 50 THEN 1
WHEN MATH_PERC > 50 THEN 1
WHEN WRIT_CONV_SCR > 50 THEN 1
WHEN WRIT_PER > 50 THEN 1
TOTAL END)
T
)
The expected answer is
4
3
4
for the respective folders
Please give me the solution
Thank you
RBHello
Rb2000rb65 wrote:
Hello ExpertsI amw Group on
Oracle Database 11 g Enterprise Edition Release 11.1.0.7.0 - 64 bit Production...
Thanks for posting the version and sample data; It is very useful.
... Select)
CASE
WHEN MATH_CONV_SCR > 50 THEN 1
WHEN MATH_PERC > 50 THEN 1
WHEN WRIT_CONV_SCR > 50 THEN 1
WHEN WRIT_PER > 50 THEN 1
TOTAL END)
TDon't forget that the WHEN of a CASE expression clauses are mutually exclusive. If none of them are evaluated to TRUE, then all of the following are not even tried.
)
The expected answer is
4
3
4
for the respective foldersPlease give me the solution
Here's one way:
SELECT CASE WHEN math_conv_scr > 50 THEN 1 ELSE 0 END + CASE WHEN math_perc > 50 THEN 1 ELSE 0 END + CASE WHEN writ_conv_scr > 50 THEN 1 ELSE 0 END + CASE WHEN writ_per > 50 THEN 1 ELSE 0 END AS total FROM t;
-
Need help in the CASE statement
Hello
My query gives the result like this
ONC BO DT countkey
RF036 I HAVE 1 156
RF036 I HAVE 8 445
RF036 O 3 1320
RF036 O D 2547
I need output like
ONC DM CC MC BC
156 445 1320 2547 RF036
Thank you
LonyBased on your data...
WITH D AS ( SELECT 'RF036' AS CNO, 'I' AS BO, '1' AS DT, 156 AS COUNTKEY FROM DUAL UNION ALL SELECT 'RF036' , 'I' , '8' , 445 FROM DUAL UNION ALL SELECT 'RF036' , 'O' , '3' , 1320 FROM DUAL UNION ALL SELECT 'RF036' , 'O' , 'D' , 2547 FROM DUAL ) SELECT CNO, MAX(DM) AS DM, MAX(CC) AS CC, MAX(MC) AS MC, MAX(BC) AS BC FROM ( SELECT CNO, CASE WHEN BO = 'I' AND DT = '1' THEN COUNTKEY ELSE NULL END DM, CASE WHEN BO = 'I' AND DT = '8' THEN COUNTKEY ELSE NULL END CC, CASE WHEN BO = 'O' AND DT = '3' THEN COUNTKEY ELSE NULL END MC, CASE WHEN BO = 'O' AND DT = 'D' THEN COUNTKEY ELSE NULL END BC FROM D ) GROUP BY CNO
-
Help: How to use the Case statement in the ODI11g Interface?
Hello
My basic source I get 'Year' values and I want that these values result code in the interface and after translation want to push on the target system.
Example:
Database source, I get value for
Year
2010
2011
2012
When I get the year 2010 I want to change the value in "FY10".
When I get year 2011 I want to change the value in "FY11.
and even for the year 2012 to "FY12.
I've tried to make the Case statement, but had no success.
I don't want to create the lookup table in the source system.
Any help in this matter.
Thank you
Concerning
Sher
Published by: Sher Ullah Baig on August 26, 2012 17:52CASE
WHEN source_column = '2010' THEN 'FY10.
WHEN source_column = '2011' and THEN 'FY11.
WHEN source_column = '2012' and THEN 'FY12.
END -
call the statement box inside the case statement
I am writing a program which requires me to run a statement box inside another case statement. Although this sounds like a simple thing, I need to be able to call the case statement using a sequence structure, where s1 sends the true value to the case, s2, a fake. Then I need to record the results of this instruction box in a text file. Attatched is a simplified version of what I'm trying to do. Please note that the way my program runs may not call the case statement without using a sequence structure, two nesting box instructions is not feasible. I hope someone out there can help me because I was stuck trying to find this for awhile.
Thank you
LVStudent wrote:
[...] I can't call the case statement without using a sequence structure
Yes you can. You just need to be smart.
LVStudent wrote:
I don't think I can use a state machine to fix this.
I think you probably can.
With respect to your original post. What are these s1 and s2 are you talking about? My opinion on the matter, it is that you want to select a case based on the values of several controls Boolean. If this is correct, I do this:
Build your Boolean controls in a table, convert the table number and insert it into the structure of your business. No button pressed = 0, s1 = only 1, s2 = only 2 both = 3. This works for Boolean values as much as you want and is an easy way to make a decision that depends on many entries.
-
Syntax of the case statement / WHEN
The table I use a depreciation per fiscal year and the fiscal period. I try to have the amount of depreciation to go to 2 different columns based on the fiscal year and the fiscal year. I'm doing it with a nested case statement. I know that is not correct, because I get the message ORA-00905. I'm relatively new to sql and it is contribtuing to my problem as well. Here is the code I have and suggestions / corrections would be appreciated. Thanks for the help...
SELECT
lao PDR. DEPTID as DEPTID,
lao PDR. ASSET_ID as ASSET_NO,
PA. Descr as DESCRIPTION,
lao PDR. ACCOUNT_AD as AD_ACCT,
PDL. DE_ACCT, to take into ACCOUNT
lao PDR. ADEATH as AMT_DEPR,
PDL. JOURNAL_ID as JRNL_ID,
PDL. JOURNAL_DATE as JRNL_DT,
lao PDR. FISCAL_YEAR as FY,
lao PDR. ACCOUNTING_PERIOD AP,
CASE
WHEN RDP. FISCAL_YEAR = 2014 THEN
WHEN RDP. PERIOD ACCOUNTANT = 11 THEN pdr. DEPR
END AS CURR_MONTH,
CASE
WHEN RDP. FISCAL_YEAR <>2014
WHEN RDP. ACCOUNTING PERIOD <>11 THEN pdr. DEPR
END AS PRIOR_MONTH
OF PS_DEPR_RPT pdr
INNER JOIN PS_DIST_LN pdl
THE pdl. BOOK = pdr. BOOK
AND pdl. BUSINESS_UNIT = pdr. BUSINESS_UNIT
AND pdl. FISCAL_YEAR = pdr. FISCAL_YEAR
AND pdl. ACCOUNTING_PERIOD = pdr. ACCOUNTING_PERIOD
AND pdl. ASSET_ID = pdr. ASSET_ID
AND pdl. CF_SEQNO = pdr. CF_SEQNO
INNER JOIN PS_ASSET PA
WE pa. ASSET_ID = pdl. ASSET_ID
AND pa. BUSINESS_UNIT = pdl. BUSINESS_UNIT
WHERE
lao PDR. BUSINESS_UNIT = "A0465.
AND pdr. BOOK = 'RUN '.
AND ((pdr. FISCAL_YEAR = 2014 AND pdr. ACCOUNTING_PERIOD = 11) OR (pdr. FISCAL_YEAR = 2014 AND pdr. ACCOUNTING_PERIOD = 10))
Hello
2713822 wrote:
Thank you... I appreciate the information you provide when you answer these questions. I always try to get the amount (from the same column) for 2 rows in different columns. I tried the LAST_VALUE and LAG but it took a long time for the queries to run, I'm looking for another way to do the same. I'm only using SQL to retrieve data. I don't have the ability to create or insert.
I looked the information above and the CASE statement to look like this:
CASE
WHEN RDP. FISCAL_YEAR = 2014
AND pdr. ACCOUNTING_PERIOD = 11
THEN the RDP. ADEATH AS CURR_MONTH
ON THE OTHER
lao PDR. ADEATH AS MONTHS PREVIOUS
END
But I'm now getting an "ORA-00905: lack of keyword" message.
What I'm trying to do is to draw 2 lines 1 to 2014 / 11 and another for 2014 / 10. The amount for the period 2014 / 11 should go in the current column and the amount for the period 2014 / 10 should go in the previous column.
Before current assets management
01 AB01 50.01 50.03
....
If you want to give an alias for a column, then you can say "AS nome_alias" after that tell you what that is in this column.
'AS nome_alias' applies to the entire column. Cannot use 'alias_name' in the middle of an expression, for example, in the middle of a CASE expression, before the END keyword.
If you want to have 2 separate output columns, curr_month and prior_month to your output, you must then 2 separate columns in your SELECT clause. for example:
SELECT pdr.branch
pdr.asset
CASE
WHEN pdr.fiscal_year = 2014
AND pdr.accounting_period = 11
THEN pdr.depr
END AS curr_month
CASE
WHEN...
THEN...
END AS prior_month
PDR
;
If post you some sample data (CREATE TABLE and INSERT statements), the results and explanations, I could show you how to complete the... sections.
To find out what version of Oracle you have, use
SELECT *.
SINCE the release of v$.
The output can be messy, like this:
BANNER
-------------------------------------------------------------------
CON_ID
----------
12 c Oracle database Release 12.1.0.1.0 - 64 bit Production
0
PL/SQL Release 12.1.0.1.0 - Production
0
CORE Production 12.1.0.1.0
0
AMT for 64-bit Windows: Version 12.1.0.1.0 - Production
0
NLSRTL Version 12.1.0.1.0 - Production
0
The important thing is the number 5 parts on the first line; 12.1.0.1.0 in the example above.
-
HelloHow can I convert a code for the below sql case statement:
If)
(length of (tmp_co_orig_val) < = 0 OR tmp_co_orig_val < = 0 OR tmp_co_orig_val == 1 OR tmp_co_orig_val > = 9999999)
OR
((tmp_co_orig_val == 999999 AND (length of (tmp_rcr_orgn_ltv_rt) < = 0 OR tmp_rcr_orgn_ltv_rt < tmp_rcr_orgn_ltv_rt > 1.3 GOLD 0.15)))
Start
tmp_collatvalue = «»
tmp_msg_cd = 115
end
Is the case statement below that I wrote is correct. I don't have an environment now to test. Please advice.
BOX WHEN (CHAR_LENGTH (tmp_co_orig_val) < = 0 OR tmp_co_orig_val < 0 OR tmp_co_orig_val = 1 OR tmp_co_orig_val = > = 9999999)
OR (tmp_co_orig_val = 999999 AND (CHAR_LENGTH (tmp_rcr_orgn_ltv_rt) < = 0 OR tmp_rcr_orgn_ltv_rt < tmp_rcr_orgn_ltv_rt > 1.3 GOLD 0.15))
THEN tmp_collatvalue IS NULL AND tmp_msg_cd = 115
END
Hello
937454 wrote:
Thanks Frank. Really helps. But I have to write a sql statement
But can you also specify, if I made a mistake in my code, or it's ok.
There are no CASES reported in Oracle SQL.
Oracle SQL has BOX expressions, but they only return a single value. You cannot set 2 columns in the same expression BOX.
Perhaps the best thing to do in pure SQL for you is to write a CASE expression, very similar to the CASE statement above, in a subquery and use the results of this in 2 separate expressions of BOX (very simple) in a Super application.
-
NOT IN clause in State of the CASE statement does not
Dear friends,
I want to use on the condition in the WHERE clause of my SELECT statement.
WHERE <>...
<>...
AND eihf.exr_aip_asset_sid NOT IN
(CASE when safa.fund_relation = 'C' and safa.fund_account_code! = vFundAccount)
SO (37874,37909,43424,55040,59513,59598,58570,76148,75885,75927)
ON THE OTHER
(0)
END
)
But it gives me an error like "missing of good parenthis."
Help, please.
Kind regards
JonathanYou can not write a list of values within the case statement.
try something like this,
WHERE <-------------> AND CASE WHEN safa.fund_relation = 'C' AND safa.fund_account_code != vFundAccount THEN eihf.exr_aip_asset_sid ELSE 1 END NOT IN (37874, 37909, 43424, 55040, 59513, 59598, 58570, 76148, 75885, 75927) AND CASE WHEN safa.fund_relation = 'C' AND safa.fund_account_code != vFundAccount THEN 1 ELSE eihf.exr_aip_asset_sid END != 0;
-
Question of the CASE statement
Is it OK to mix conditions inside the case statement? as for example:
SUM (CASE WHEN b.SUPPLIER_NUM = 12345 AND c.VPN IN ('3245', '5345', '87894') THEN a.TRAN_AMT ELSE 0 END) SUMSPEND_MOS
???
I put in parentheses between the two conditions?
SUM (CASE WHEN (b.SUPPLIER_NUM = 12345 AND c.VPN IN ('3245 ', '5345', ' 87894')) THEN a.TRAN_AMT ELSE 0 END) SUMSPEND_MOS
is this legal syntax? because its not complaining do not, but I want to just make sure that these things are good to use and the data comes right back.
Thank you!Kodiak_Seattle wrote:
Is it OK to mix conditions inside the case statement? as for example:SUM (CASE WHEN b.SUPPLIER_NUM = 12345 AND c.VPN IN ('3245', '5345', '87894') THEN a.TRAN_AMT ELSE 0 END) SUMSPEND_MOS
???
Yes, you can do that. The conditions must be related somehow.
I put in parentheses between the two conditions?
SUM (CASE WHEN (b.SUPPLIER_NUM = 12345 AND c.VPN IN ('3245 ', '5345', ' 87894')) THEN a.TRAN_AMT ELSE 0 END) SUMSPEND_MOS
You have no need, but if it helps you (or one who has to maintain the code) to understand it better, then go ahead. The cost can be one millisecond extra when the query is compiled (it will run everything as fast so be it), and the benefits can be enormous.
I find that formatting code contributes much more clarity to have additional parentheses. Why not use something like that?
SUM ( CASE WHEN ( b.SUPPLIER_NUM = 12345 AND c.VPN IN ('3245', '5345', '87894') ) THEN a.TRAN_AMT ELSE 0 END ) AS SUMSPEND_MOS
If you have really complicated conditions, where you mix ANDS and ORs, then you should certainly use parentheses. It goes the same for the WHERE clause.
is this legal syntax? because its not complaining do not, but I want to just make sure that these things are good to use and the data comes right back.
The syntax is fine.
If the data comes in good or bad is another matter. What data are, and what you mean by 'comes back good' (in other words, what results do you get off these data)? -
TDE is not "transparent" to the CASE statement
Hello
I have installed oracle (10) database with TDE. It works very well with almost all queries. But I have problems when I try to use the CASE statement.
He comes to the table, I created:
{color: #0000ff} create table t_test)
col1 varchar (128)); {color}
This query returns an empty result (as planned, because there is still no data in the table).
{color: #0000ff} to select)
When col1 = 'test' then 'test '.
end) as Carter
of t_test
{color}
I now encrypt the column with the following statement:
{color: #0000ff} change alter table t_test (col1 encrypt using "AES256" without salt); {color}
And try again the same statement:
{color: #0000ff} to select)
When col1 = 'test' then 'test '.
end) as Carter
of t_test
{color}
He now returns {color: #ff0000} ORA-00932 inconsistent data types: expected BINARY got {color} CHAR
But if I try:
{color: #0000ff} select *.
of t_test
{color}
There is no errors (returns empty result, as expected).
I tried even with data, with the same result.
Could someone please tell me what I'm doing wrong here?
Thank you.You touch the Bug 6262107 'ORA-932 of the CASE expression sought with encrypted column:
Description:
"ORA-932 incompatible data types: expected BINARY got TANK ' is triggered.
on a query with a case expression sought on a column with TDE.for example:
CREATE TABLE TDE_TEST (COL1 VARCHAR2 (1));
INSERT INTO TDE_TEST VALUES('1');
ALTER TABLE TDE_TEST CHANGE (COL1 ENCRYPT WITHOUT SALT);
SELECT CASE WHEN COL1 = '1', THEN 'A' OF ANOTHER END 'B' OF TDE_TEST;
^
ORA-00932: inconsistent data types: expected BINARY got TANKWorkaround solution:
Convert a simple case of the searched case expression expression.So, I updated the sql works:
SQL > select (case col1
2 when 'test' then 'test '.
3 end) as Carter
4 * of t_test
/
no selected lineI hope this helps.
-
I got confused on this CASE
(1) SELECT BOX WHEN 111 = "111" THEN 'YES' ELSE ' ' END OF THE DOUBLE - YES
(2) SELECT CASE WHEN COALESCE (111 111) DOESN'T = "111" THEN 'YES' ELSE ' ' END OF THE DOUBLE - YES
(3) SELECT CASE WHEN COALESCE (111, '111') = '111' THEN 'YES' IS ' ' END OF THE DOUBLE
ORA-00932: inconsistent data types: expected NUMBER obtained TANK
What exactly the things happening between 2 & 3. ' nt it implicitly convert as case 2 is because that in case 3, the first value is anyway not null as CAS2... This leads to confusion.
I have read the Oracle documentation and there is an implicit data type conversion table. It also indicates that can be converted. There is always some confusion raised. Anyone could wrap this confusion?
No, the function coalesce requires that all entries to be of the same data type because they are arguments of this function.
Conversion of implicit data type occurs generally when things are compared with operators (as you do in examples 1 and 2.
There is not many functions/procedures of this nature that the mixed data types and convert them for you.
So, it is nothing to do with the CASE statements, it's in with him COALESCE function. It is overloaded for particular data types, but it requires all arguments to be of the same data type.
-
Hi all
I need to replace the value selected,
Here's the query... make a mistake... make me ORA-00923: KEYWORD not found or provided.
Could you please correct me.
SELECT sper.assettxt =
CASE
WHEN (select description of flv fnd_lookup_values where flv.lookup_type = 'ZZCUS_SPER_ASSET_CODES'
and flv.description = sper.assettxt);
(select the meaning of flv fnd_lookup_values where flv.lookup_type = 'ZZCUS_SPER_ASSET_CODES'
and flv.description = sper.assettxt)
NVL (resp_cat. (Text, 'Total');
NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 24 h, sper.total, 0)), 0) day1.
NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 48 but > 24', sper.total, 0)), 0) day2.
NVL (SUM (DECODE (sper.author_create_ta_interval,' < = 72 but > 48', sper.total, 0)), 0) day 3,.
NVL (SUM (DECODE (sper.author_create_ta_interval,' > 72 h, sper.total, 0)), 0) day4.
NVL (SUM (DECODE (sper.author_create_ta_interval, "N/a", sper.total, 0)), 0) 'open ',.
NVL (SUM (sper.total), 0) "Grand Total".
(SELECT "point proven" TEXT data)
OF THE DOUBLE
UNION
SELECT "data updated item.
OF THE DOUBLE
UNION
SELECT "other answers.
Resp_cat FROM DUAL)
LEFT OUTER JOIN
(SELECT assettxt,
s.sper_status_text,
s.author_create_ta_interval,
Total COUNT (*)
OF s zzcus.zzcus_sper_data
WHERE 1 = 1
AND s.sper_dates =: P_SR_DATES
AND s.sper_month = substr(:P_SR_DATES,5,2)
AND s.customer_id =: P_CUSTOMER_ID
- AND s.task_inquiry_type AS ' descriptive data Challenge %.
AND s.assettxt <>'! MAD
S.assettxt, s.sper_status_text, s.author_create_ta_interval)
SPER partition by (SPER. ASSETTXT)
((CASE
WHEN sper.sper_status_text = "data confirmed."
THEN
"Question confirmed the data.
WHEN sper.sper_status_text = "updating data"
THEN
"Item updated data.
ON THE OTHER
"Other answers.
END) = resp_cat.text)
GROUP BY ROLLUP (resp_cat.text), sper.assettxt
ORDER BY sper.assettxt, (CASE resp_cat.text
WHEN 'confirmed data element' THEN 1
WHEN 'updated data element' THEN 2
WHEN "Others" THEN 3
END)
I need to replace sper.assettxt with the meaning of fnd_lookup_values table.
-Well, it is interpretation since I'm guessing by your intention with the CASE statement.
Do you mean
CASE WHEN EXISTS ( SELECT description FROM fnd_lookup_values flv WHERE flv.lookup_type ='ZZCUS_SPER_ASSET_CODES' AND flv.description =sper.assettxt ) THEN ( SELECT meaning FROM fnd_lookup_values flv WHERE flv.lookup_type ='ZZCUS_SPER_ASSET_CODES' AND flv.description =sper.assettxt ) END,
If yes then it is much better represented as
( SELECT meaning FROM fnd_lookup_values flv WHERE flv.lookup_type ='ZZCUS_SPER_ASSET_CODES' AND flv.description =sper.assettxt )
Assuming that the code returns 0 - 1 line (no more and you will get an error).
-
need help with the Update statement
Hello
I received a question in a course and I tried my best to respond, and now my brain is giving. I would really appreciate help with the update statement. I don't mind if you do not validate a solution, a little nudge in the right direction would be really useful. I'll post that I got.
THE QUESTION
/ * For these agents disabled on more than seven missions, change their date of deactivation of the first date of deactivation of all the agents that have been activated in the same year as the agent that you update currently.
*/
I have it divided into parts, here is my select statement to agents disabled on more than 7 missions, which produces the deactivation_dates in the agents table that I want to update...
.. .and the code for the first date of deactivation for each year of activation agentSELECT s.deactivation_date FROM ( SELECT a.deactivation_date, count(m.mission_id) as nomissions FROM agents a INNER JOIN missions_agents m on a.agent_id=m.agent_id GROUP BY a.deactivation_date ) s WHERE s.nomissions>7 AND s.deactivation_date IS NOT NULL
..... I am not real to marry these two statements together in the Update statement. I can't extract each date of deactivation produced in the first select statement and their match against the first date of deactivation in the year they have been activated for the second select statement.select a2.deactivation_date from agents a2 where a2.deactivation_date= ( select min(a.deactivation_date) from agents a where to_number(to_char(a.activation_date,'YYYY'))=to_number(to_char(a2.activation_date,'YYYY')) )
Any help greatly appreciated... :))I began to wonder how things would :)
user8695469 wrote:
First of all why he chooses the date the earliest of all agentsUPDATE AGENTS_COPY AC /* (1) */ SET DEACTIVATION_DATE = ( SELECT MIN(AGS.DEACTIVATION_DATE) FROM AGENTS_COPY AGS , AGENTS_COPY AC /* (2) */ WHERE TRUNC(AGS.ACTIVATION_DATE,'YEAR') = TRUNC(AC.ACTIVATION_DATE,'YEAR') /* (3) */ )
He recovers as soon as the subquery has not been correctly set in the SET clause. It seems you are trying to update a correlated, but we are still having a conceptual shift. I have added a few comments to your code above and below will explain.
(1): when you do a correlated update it is useful to the table alias that you did right here.
(2): this table statement is not necessary and is the reason why the FIRST deactivation date is selected. The alias that you use (3) refers to THIS table, not the one defined in the update statement. Remove the line indicated by (2) in the FROM clause and a correlated update will happen.
and secondly why is it to update each row, when I thought that I'm just the lines where the agents are disabled and missions > 7? Pointers on where I'm wrong would be very appreciated. (SQL = stupid query language!) :)
user8695469 wrote: then why is it to update each row, when I thought that I'm just the lines where the agents are disabled and missions > 7? Pointers on where I'm wrong would be very appreciated. (SQL = stupid query language!) :)
WHERE EXISTS ( SELECT a.agent_id, count(m.mission_id) FROM agents a /* INNER JOIN AC ON AC.AGENT_ID = A.AGENT_ID */ INNER JOIN missions_agents m ON a.agent_id=m.agent_id GROUP BY a.agent_id, a.deactivation_date HAVING count(m.mission_id)>7 AND a.deactivation_date IS NOT NULL )
Once again this problem is similar to the question above that a correlation update doesn't work. Test existence of lines in an EXISTS subquery. Since your subquery is not related to the table that you are trying to update, it will be always return a line and, therefore, it returns true for EACH LINE in the AGENTS table. To limit the game to only agents > 7 missions results, you need to add a join condition that references the table in your update statement. I added one above (with comments) as a sample.
I recommend you look over all material that you have associated with correlated subqueries, including documents that I posted above. This seems to be what you're having the problem more with. If you need me to explain the concept of correlated queries any better please let me know.
Thank you!
-
Use the CASE statement in a query of LOV
Hello
I wrote a select statement to be used in my list of values query, and it works fine when I run with SQL Developer. But when I put it in the LOV I get the LOV query is invalid error message. Here's the query I use:
If the CASE statement is the source of the problem?select case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')' else nt.TAXON_NAME end display_species ,case when nt.COMMON_NAME is not null then nt.COMMON_NAME || ' (' || nt.TAXON_NAME || ')' else nt.TAXON_NAME end return_species FROM NBN_TAXON nt WHERE lower(nt.INPUT_CATEGORY) = decode(lower(:P312_TAXON_GROUP_ADD), 'fish', 'fishes', lower(:P312_TAXON_GROUP_ADD)) order by 1;
Hello
Try to remove the colon semi at the end.
See you soon
Ben
-
Help with making SQL query references to column aliases in the Case statement
I need help with a sql query that I'm trying. I can go about it the wrong way, but I would be grateful if I could get any suggestions on possible solutions. This is my query:
SELECT DISTINCT spriden_pidm, spriden_id id, spriden_last_name | ',' | spriden_first_name name,
CASE
WHEN rcresar_comm_code_01 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_01
WHEN rcresar_comm_code_02 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_02
WHEN rcresar_comm_code_03 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_03
WHEN rcresar_comm_code_04 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_04
WHEN rcresar_comm_code_05 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_05
WHEN rcresar_comm_code_06 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_06
WHEN rcresar_comm_code_07 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_07
WHEN rcresar_comm_code_08 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_08
WHEN rcresar_comm_code_09 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_09
WHEN rcresar_comm_code_10 IN ('268 ', '269', ' 270') THEN rcresar_comm_code_10
END acg_elig_comm_code
CASE
WHEN acg_elig_comm_code = ' 268' THEN 'rigorous HS course. "
WHEN acg_elig_comm_code = '269' THEN ' 2 or several AP or IB"
WHEN acg_elig_comm_code = '270' THEN 'NOC as possible ".
END comm_code_description
OF spriden, rcresar, rcrapp1
WHERE (rcresar_comm_code_01 IN ('268 ', '269', ' 270')
OR rcresar_comm_code_02 ('268 ', '269', ' 270')
OR rcresar_comm_code_03 ('268 ', '269', ' 270')
OR rcresar_comm_code_04 ('268 ', '269', ' 270')
OR rcresar_comm_code_05 ('268 ', '269', ' 270')
OR rcresar_comm_code_06 ('268 ', '269', ' 270')
OR rcresar_comm_code_07 ('268 ', '269', ' 270')
OR rcresar_comm_code_08 ('268 ', '269', ' 270')
OR rcresar_comm_code_09 ('268 ', '269', ' 270')
OR rcresar_comm_code_10 ('268 ', '269', ' 270'))
Rcresar_aidy_code = & aidy_code
AND rcrapp1_aidy_code = rcresar_aidy_code
AND rcrapp1_curr_rec_ind = 'Y '.
AND rcrapp1_seq_no = rcresar_seq_no
AND spriden_pidm = rcresar_pidm
AND rcrapp1_pidm = rcresar_pidm
AND spriden_change_ind IS NULL
ORDER BY name
The second case statement is where I don't know exactly what it takes to get what I want.
Output should be like:
spriden_pidm name ID acg_elig_comm_code comm_code_description
«0000000000', ' 1111111111 ","John Doe","268", «rigorous HS race"»
If I take the second case statement it works great except that I do not have my comm_code description column. My question is how can I use my first statement value box to determine this column? I think that I need a case statement as I have, but I don't know how to reference the value of acg_elig_comm_code. Any help would be greatly appreciated. Thank you.
Published by: blackhole82 on January 20, 2009 09:20Hello
You cannot use the alias column in the query, even where it is set (except in the ORDER BY clause).
You can set the alias in a subquery and then use it in a great query, like this:WITH sub_q AS ( SELECT DISTINCT spriden_pidm,spriden_id id, spriden_last_name||', '||spriden_first_name name, CASE WHEN rcresar_comm_code_01 IN ('268','269','270') THEN rcresar_comm_code_01 WHEN rcresar_comm_code_02 IN ('268','269','270') THEN rcresar_comm_code_02 WHEN rcresar_comm_code_03 IN ('268','269','270') THEN rcresar_comm_code_03 WHEN rcresar_comm_code_04 IN ('268','269','270') THEN rcresar_comm_code_04 WHEN rcresar_comm_code_05 IN ('268','269','270') THEN rcresar_comm_code_05 WHEN rcresar_comm_code_06 IN ('268','269','270') THEN rcresar_comm_code_06 WHEN rcresar_comm_code_07 IN ('268','269','270') THEN rcresar_comm_code_07 WHEN rcresar_comm_code_08 IN ('268','269','270') THEN rcresar_comm_code_08 WHEN rcresar_comm_code_09 IN ('268','269','270') THEN rcresar_comm_code_09 WHEN rcresar_comm_code_10 IN ('268','269','270') THEN rcresar_comm_code_10 END acg_elig_comm_code -- Originally posted with , here (error) FROM spriden, rcresar, rcrapp1 WHERE (rcresar_comm_code_01 IN ('268','269','270') OR rcresar_comm_code_02 IN ('268','269','270') OR rcresar_comm_code_03 IN ('268','269','270') OR rcresar_comm_code_04 IN ('268','269','270') OR rcresar_comm_code_05 IN ('268','269','270') OR rcresar_comm_code_06 IN ('268','269','270') OR rcresar_comm_code_07 IN ('268','269','270') OR rcresar_comm_code_08 IN ('268','269','270') OR rcresar_comm_code_09 IN ('268','269','270') OR rcresar_comm_code_10 IN ('268','269','270')) AND rcresar_aidy_code = &aidy_code AND rcrapp1_aidy_code = rcresar_aidy_code AND rcrapp1_curr_rec_ind = 'Y' AND rcrapp1_seq_no = rcresar_seq_no AND spriden_pidm = rcresar_pidm AND rcrapp1_pidm = rcresar_pidm AND spriden_change_ind IS NULL ) SELECT sub_q.*, CASE WHEN acg_elig_comm_code = '268' THEN 'Rigorous HS course' WHEN acg_elig_comm_code = '269' THEN '2 or more AP or IB' WHEN acg_elig_comm_code = '270' THEN 'ACG possible' END comm_code_description FROM sub_q ORDER BY name
Furthermore, you might think to rearrange your table, so that you do not have 10 columns (rcresar_comm_code_01, rcresar_comm_code_02,...) that essentially do the same thing. The usual way to handle this kind of one-to-many relationship is to have all rcresar_comm_codes in a separate table, one per line, with a pointer to the table where you have them now.
Published by: Frank Kulash, January 20, 2009 11:35
Syntax error has been corrected
Maybe you are looking for
-
Can I use my FCPX on more than 1 computer?
I'm about to buy FCPX but I have a Macbook Pro and iMac and I want to use both for FCPX, is it possible?
-
How to remove the automatic password for my outlook accounts?
Recently, I noticed that when I go to access my outlook email accounts password is already filled by something much more of my password (I only see points, as expected). I want those deleted, but don't know how to 'remove '. I can delete them manuall
-
Error-4702, Regular Expressions
In the attached vi, I am trying to accept only numbers and letters. When I hit the SHIFT key, I get error-4702. How can I correct this situation? I have not worked this yet, but I also accept the Enter key so that the vi will end either enter or th
-
I'm unable to download Adobe flash player
Game I like to play online games, but my computer won't let me download adobe flash player, can anyone help me?
-
unwanted RSS in my computer.
I have a sort of coming news feed in my computer with no image, just good. I don't know he's coming, but he's got to go. It's very annoying. It starts just by itself on a min turning on my computer. does anyone know were just this madness?