case of subquery in part then
HelloI want to write a case statement that returns multiple values.
Currently, I use this, but it does not work
SELECTION * PROJECT WHERE
PROJECT IN
(CASE
WHEN the CONDITION < 7 THEN condition
WHEN THE CONDITION = 7 THEN (SELECT PROJECT PROJECT WHERE PROJECT_STATUS LIKE '% OPEN')
OTHER (SELECT THE PROJECT PROJECT WHERE PROJECT_STATUS LIKE '% CLOSED')
END)
When I run the present I get error:
Met with the symbol "CLOSED" when expecting one of the following values: * & = - ; <>/ at is mod remains not rem <>or! = or ~ = > = < = <>and or as like2 like4 likec between | Member of multiset submultiset +.
Please help me... What do to correct the code
I don't think it is possible for an instruction BOX be used throughout to made this way. You can rewrite the query, however, to be something like
SELECT *
FROM project
WHERE ( (condition < 7) and (condition = project_id) )
OR ( (condition = 7) and (project_status like 'OPEN%') )
OR ( (condition > 7) and (project_status like 'CLOSED%') )
Justin
Tags: Database
Similar Questions
-
Update col value based on an another tbl - update, case stmt, subquery
I need to
(1) adds a column col1 existing table tbl1;
(2) the value of col1 will be updated to the value of column col2 from table tbl2.
(3) if the tbl2 returns the number of records or no records, the value of col1 will default to 0. otherwise, it will be the unique value of col2 of tbl2
I'm stuck.
Thank you.Hello
new2Oracle wrote:
I need to
(1) adds a column col1 existing table tbl1;Use ALTER TABLE.
(2) the value of col1 will be updated to the value of column col2 from table tbl2.
(3) if the tbl2 returns the number of records or no records, the value of col1 will default to 0. otherwise, it will be the unique value of col2 of tbl2Use an expression BOX to test if there is a line matching or not and return a value on this basis.
For example:UPDATE tbl1 t1 SET col1 = ( SELECT CASE WHEN COUNT (*) = 1 THEN MIN (col2) ELSE 0 END FROM tbl2 WHERE common_column_1 = t1.common_column_1 AND common_column_2 = t1.common_column_2 ) ;
Because the subquery uses an aggregate function (COUNTY), the returned value must be as an aggregate (such as MIN) or a constant (such as 0). In cases where there is only 1 row, we could use MAX, AVG or SUM instead of MIN to get the value of this one line.
-
How to make a sum of a subquery which first determine the time period and then the total sum.
Hello
I know how to solve it in two of the query, but I want to merge this in a single query.
I have records in a table with the status 1. I also have records in the table with the State 2 and 3. But the 2 and 3 folders can have a period different kind of these must be determined in advance when this is, mind a deviding through 3 or 12 months.
Double nesting is not allowed with Oracle, I detected. There may be more than 1 card whose status is 2 or 3 and I want to collect these and return as a sum value.
select clk.clickid ,clk.uidclickstatus ,clk.period ,clk.STARTTIME ,clk.stoptime ,clk.CREATETIME ,clk.VOLUME "Open click volume" ,sum(tot_volume) --This doesn't work and is my problem! ,(select case when ec.period = 'M' then ec.volume when ec.period = 'Q' then ec.volume / 3 when ec.period = 'Y' then ec.volume / 12 else 0 end as tot_volume from click ec where ec.uidcontract = clk.uidcontract --807430 and ec.starttime = clk.starttime --Maybe the good reader will be saying why are you deviding, this is now directly related to keep it simple for now and ec.stoptime = clk.stoptime --Maybe the good reader will be saying why are you deviding, this is now directly related to keep it simple for now and ec.uidclickstatus in (2, 3) ) sourced_volume from click clk ,lscmcontract ctr where clk.UIDCONTRACT = ctr.UIDCONTRACT and ctr.UIDCONTRACT = 807430 and clk.uidclickstatus = 1 order by clk.uidclickstatus asc, clk.period, clk.starttime ;
Can someone help me how to solve this problem?
I'm not a guru on SQL. Straight motion is not a problem but what advanced...
Thank you
Nico
OK, I've already said I'm not a guru, but I solved my problem. I didn't know that it is possible to put a sum around a case. In the end, it is the solution:
select field ,field ,(select sum(case when clk.period = 'M' and ec.period = 'M' Then (ec.volume) when clk.period = 'M' and ec.period = 'Q' Then (ec.volume / 3) when clk.period = 'M' and ec.period = 'Y' Then (ec.volume / 12) when ((clk.period = 'Q' and ec.period = 'M') or (clk.period = 'Q' and ec.period = 'Q')) Then (ec.volume) when clk.period = 'Q' and ec.period = 'Y' Then (ec.volume / 4) when clk.period = 'Y' Then ec.volume end) as Month_Vol from ec.table where ... and ... ) volume from clk.talble where ...
Nico
-
Case statement with multiple queries to the THEN clause
Y at - it a syntax something like:
Case
When A = 1
Then B: = 2 and C: = 3
When A = 2
Then B: = 4 and C: = 5
Else B: = 6 and C: = 7
End case;
where clause can then have multiple assignments?
Also, how to mark entries as code on this forum?
TIA,
Extreme FarleyYou can simplify a bit:
SQL> declare 2 a number := 0; 3 b number := 0; 4 c number := 0; 5 begin 6 7 Case A 8 WHEN 1 Then 9 B := 2; 10 C := 3; 11 WHEN 2 Then 12 B := 4; 13 C := 5; 14 Else 15 B := 6; 16 C := 7; 17 End Case; 18 dbms_output.put_line('A=' || a); 19 dbms_output.put_line('B=' || b); 20 dbms_output.put_line('C=' || c); 21 end; 22 / A=0 B=6 C=7 PL/SQL procedure successfully completed.
-
A query containing a subquery and Group By all-in-one
Hi people,
Any interesting fact but nerve racking situation I have here. I have a question that I have to do counts and amounts and I also need to have a Sub inside query. When I run my application, I get an error like this:
Error in the command line column: 4:36
Error report:
SQL error: ORA-00979: not a GROUP BY expression
00979 00000 - "not a GROUP BY expression"
* Cause:
* Action:
Here's my query:
If I replace it with the acc.account_num (subquery) with a number that is in the database, then my request works well.select distinct to_char(c.start_date,'YYYY') start_date, substr(acc.account_num,1,12) account_number, ( select acc2.account_desc from student_account acc2 where acc2.account_num = acc.account_num and acc2.account_type = 'A' ), COUNT(distinct(s.student_id)) from student s, student_account acc, course c where s.course_id = c.course_id and s.account_num = acc.account_num and to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31' group by to_char(c.start_date,'YYYY'), substr(acc.account_num,1,12) order by 1;
Published by: Roxyrollers on March 29, 2011 16:45Hello
We don't know what you want. It would really help if you posted some examples of data (CREATE TABLE and INSERT statements) and the results desired from these data.
Are you sure that the scalar subquery will return ever more than one line? If so, you can wrap in a MIN or MAX aggregate function, as I've suggested before:
select distinct to_char(c.start_date,'YYYY') start_date, substr(acc.account_num,1,12) account_number, MIN ( ( select acc2.account_desc from student_account acc2 -- Changed where acc2.account_num = acc.account_num and acc2.account_type = 'A' ) ), -- Changed COUNT(distinct(s.student_id)) from student s, student_account acc, course c where s.course_id = c.course_id and s.account_num = acc.account_num and to_char(c.start_date, 'YYYY/MM/DD') between '2004/01/01' and '2009/12/31' group by to_char(c.start_date,'YYYY'), substr(acc.acct_acc_num,1,12) order by 1;
It seems that you could also simplify the query and make more fast, eliminating the scalar subquery. The table there is already part of the main query, you can do something like this:
SELECT TO_CHAR (c.start_date, 'YYYY') AS start_date , SUBSTR (acc.account_num, 1, 12) AS account_number , MIN ( CASE WHEN acc.account_type = 'A' THEN account_desc END ) AS a_desc , COUNT (DISTINCT s.student_id) AS student_id_cnt FROM student s , student_account acc , course c WHERE s.course_id = c.course_id AND s.account_num = acc.account_num AND c.start_date BETWEEN DATE '2004-01-01' AND DATE '2009-12-31' GROUP BY TO_CHAR (c.start_date, 'YYYY') , SUBSTR (acc.acct_acc_num, 1, 12) ORDER BY start_date;
I guess that course.start_date is a DATE. Instead of converting each start_date to string (so you can see if it is in the right range), it is more efficient and less prone to error, compared to other DATEs, as shown above.
It is very strange that you
substr(acc.account_num,1,12)
in the select clause, but
substr(acc.acct_acc_num,1,12)
in the GROUP BY clause. Did you mean having the same column in both places?
Yet once, without seeing some examples of data (CREATE TABLE and INSERT statements) and the results desired from this data, I can't do much.
-
A subquery within the instruction BOX
I try to use a subquery in a Case statement with a condition 'where' that binds at the request of the mother. Like this:
It works for me in SQL Server, but in ORACLE the subquery does not return line, I guess that it is binding to the parent query part. All thoughts in another way, I could get the result I want?Select T.idperson , CASE WHEN T.type = 'C' THEN (SELECT name from Customers where C.idcustomer = T.idperson) ELSE (SELECT name from Providers where idprovider = T.idperson) END Name from myTable T
Thank you
JuanDiegoHi, Juandiego,
If it's not returns all rows, you must have all the lines in the table.
If you have lines in the table, you either get
(a) an error or
(b) some lines (even if the columns are set to NULL).There is another way to get the desired results.
I probably wouldn't use scalar subqueries. I'd do it outer join all the tables, like this:Select T.idperson , CASE WHEN T.type = 'C' THEN c.name ELSE p.name END AS Name from myTable t LEFT OUTER JOIN Customers c ON c.idcustomer = T.idperson LEFT OUTER JOIN Providers p ON p.idprovider = T.idperson ;
-
I tried this software ad/malware and tried the launchdaemon corrections responsible extensions suggested and still can't find where they are hiding on my computer. One of them adds unwanted links with green circular arrows on other Web pages and advertising windows with offers for similar products pop - up on Safari and Chrome. They are driving me crazy, I even paid for the software adblocking, which serves to block the sites I want to open and still allows popups!
You may have installed ad-injecting malicious software ("adware").
Do not use any type of product, "anti-virus" or "anti-malware" on a Mac. It is never necessary for her, and relying on it for protection makes you more vulnerable to attacks, not less.
Save all data first.
Some of the most common types of adware can be removed by following the instructions from Apple. But before you follow these instructions, you can try an automatic removal.
If you are not already running the latest version of Mac OS X ("El Capitan"), update or upgrade in the App Store you could adware to automatically remove. If you are already using the latest version of El Capitan, you can still download the current update of the Apple Support downloads page and run it. Still, some types of malware will be deleted, not all. There is no such thing as the automatic removal of all possible malware, either by OS X third party software. That's why you can't rely on software to protect you.
If the malware is deleted in your case, you will still need to make changes to the way you use your computer to protect you from new attacks. Ask if you need advice.
If the malware is not automatically deleted, and you cannot remove yourself by following the instructions from Apple, see below.
This simple procedure to detect any type of adware that I know. Disabling is a procedure distinct and better still.
Some legitimate software is funded by advertising and may display advertisements in its own windows or in a web browser while it is running. It's not malware and it may not appear. In addition, some Web sites display advertising intrusive popup that can be confused with adware.
If none of your web browsers work well enough to carry out these instructions, restart the computer in safe mode. Allows to temporarily disable the malware.
Step 1
Please triple - click on the line below on this page to select it, and then copy the text to the Clipboard by pressing Control-C key combination:
~/Library/LaunchAgents
In the Finder, select
Go ▹ go to the folder...
from the menu bar and paste it into the box that opens by pressing command + v press return. Open a folder named "LaunchAgents", or you will get a notice stating that the file cannot be found. If the file is not found, proceed to the next step.
If the folder opens, press the combination of keys command-2 to select the display of the list, if it is not already selected. Please don't skip this step.
There should be a column in the update Finder window. Click this title two times to sort the content by date with the most recent at the top. If necessary, enlarge the window so that all the content show.
Follow the instructions in this support article under the heading "take a screenshot of a window." An image file with a name starting in 'Screenshot' should be saved to the desktop. Open the capture screen and make sure it is readable. If this isn't the case, capture a small part of the screen indicating that what needs to be shown.
Start a reply to this message. Drag the image file in the editing window downloading. Alternatively, you can include text in the response.
Leave the case open for now.
Step 2
Do as in step 1 with this line:
/Library/LaunchAgents
The record which can open up will have the same name but is not the same as in step 1. In this step, the folder does not exist.
Step 3
Repeat with this line:
/Library/LaunchDaemons
This time the file will be called "LaunchDaemons."
Step 4
Open Safari preferences window and select the tab 'Extensions'. If the extensions are listed, post a screenshot. If there are no extensions, or if you cannot launch Safari, skip this step.
Step 5
If you use Firefox or Chrome browser, open the list of extensions and do as in step 4.
-
Satellite 1800-514: need parts such as keyboard, screen, inverter
I have a portable Satellite 1800-514, I need to get some parts of second hand for this that I have a few internet size for parts, but I need to know what other laptop I can take parts to repair it.
These are the parts that I need of keyboard, screen, inverter and the front of the case where the screen is.Hello
If you are looking for second hand parts then I would recommend the search on Ebay for a Satellite 1800-514 of second-hand sites.
Especially a full section of second hand is much less expensive than individual parts and devices!In my opinion, you can use the entire Satellite 1800 series parts.
The screen, keyboard, FL inverter and the screen frame must be the same.In addition, if you want you can contact Toshiba service provider in your country.
Guys might provide a list of compatible parts and devicesGreetings
-
Case statement in the process Page
Request Express 3.2.1.00.11
I put the following code in an anonymous block (will return no error) for a process Page;
BEGIN INSERT INTO DAD_ASSESSMENT_REQUEST ( tenure_id , client_id ) VALUES ( :P10101_TENURE_ID , :P10101_CLIENT_ID ); END;
But when I put a statement case it throws the following error;
1 error has occurred ORA-06550: line 7, column 3: PL/SQL: ORA-01747: invalid user.table.column, table.column, or column specification ORA-06550: line 3, column 1: PL/SQL: SQL Statement ignored
BEGIN INSERT INTO DAD_ASSESSMENT_REQUEST ( tenure_id , client_id , (CASE WHEN (:P10101_MEASURE_OPTION = 'AREA') THEN estimated_area_ha WHEN (:P10101_MEASURE_OPTION = 'LINE') THEN estimated_length_km END) ) VALUES ( :P10101_TENURE_ID , :P10101_CLIENT_ID , :P10101_X ); END;
Why is this code not work and it is possible to use a case statement in a Page Apex process
Concerning
Ben
Benton says:
Request Express 3.2.1.00.11
I put the following code in an anonymous block (will return no error) for a process Page;
- BEGIN
- INSERT
- IN DAD_ASSESSMENT_REQUEST
- (tenure_id
- client_id
- )
- VALUES
- (: P10101_TENURE_ID)
- ,: P10101_CLIENT_ID
- );
- END;
But when I put a statement case it throws the following error;
- 1 error has occurred
- ORA-06550: line 7, column 3: PL/SQL: ORA-01747: specification user.table.column, table.column or invalid column ORA-06550: line 3, column 1: PL/SQL: statement ignored
- BEGIN
- INSERT
- IN DAD_ASSESSMENT_REQUEST
- (tenure_id
- client_id
- (CASE
- WHEN (: P10101_MEASURE_OPTION = 'SPACE') THEN estimated_area_ha
- WHEN (: P10101_MEASURE_OPTION = 'LINE') THEN estimated_length_km
- END)
- )
- VALUES
- (: P10101_TENURE_ID)
- ,: P10101_CLIENT_ID
- ,: P10101_X
- );
- END;
Why is this code not work and it is possible to use a case statement in a Page Apex process
Of course, it is possible to use a CASE statement or expression (it is the latter) in a page APEX process. However, it is not possible to use an invalid syntax while doing so. Part of a clause INSERT INTO can contain only static column names.
In general the SQL expressions (and also the bind variable, another cause frequent errors of syntax) can be used only in
- the projections of the SELECT queries and subqueries
- the right side of the disposals in the SET update clause
- the lists of VALUES of INSERTs
- WHERE clause predicates
ReemaPuri wrote:
Try this
INSERT
IN DAD_ASSESSMENT_REQUEST
(tenure_id
client_id
estimated_area_ha
estimated_length_km
)
VALUES
(: P10101_TENURE_ID)
,: P10101_CLIENT_ID
, (SELECT CASE WHEN: P10101_MEASURE_OPTION = 'SPACE' AND THEN: P10101_X)
OTHERWISE, NULL END OF DOUBLE)
, (SELECT CASE WHEN: P10101_MEASURE_OPTION = 'LINE' THEN: P10101_X)
OTHERWISE, NULL END OF DOUBLE)
);
It's a good idea, but it can be simplified, eliminating unnecessary subqueries:
insert into dad_assessment_request ( tenure_id , client_id , estimated_area_ha , estimated_length_km) values ( :p10101_tenure_id , :p10101_client_id , case when :p10101_measure_option = 'AREA' then :p10101_x end , case when :p10101_measure_option = 'LINE' then :p10101_x end);
-
Hi all
I want to find the check when sdate > ACE or sdate > date of the day or sdate > edate.
It works by using the Union operator. When I replace it with the case when then statement, correct record are not seized.
Can someone help me to part, I fact the error (or any other means other than the Union)
with t as)
"SELECT id 101, November 23, 2022" as sdate, 24 October 2012 'as', 22/11/2014 like edate Union double
SELECT id 102, 27 November 2012 as sdate, 25 October 2012 'as', 29/11/2014 as edate Union double
SELECT id 103, 11 November 2012 as sdate, 18 October 2012 'as', 22/11/2014 as edate Union double
"SELECT id 104, 11 April 2012 ' as sdate, 10 September 2012 ' as, ' 02 /' 11/2014 as edate double Union
SELECT id 105, 14 November 2012 ' as sdate, 14 October 2012 'as', 14/11/2014 as double edate
)
SELECT id, sdate, stdate, trunc (sysdate) such as current_date,
case when to_date (sdate, 'dd-mm-yyyy') > to_date (, ' dd-mm-yyyy "") then 'sdate is greater than the ACE.
When to_date (sdate, ' dd-mm-yyyy "") > trunc (sysdate) then "sdate is greater than the current date.
When to_date (sdate, 'dd-mm-yyyy') > to_date (edate, ' dd-mm-yyyy "") then 'sdate is superior to edate.
finish as spd
t
Hello
We don't know what you want for the output. Always show exactly the results you want from the data of the given sample.
You do 3 different comparisons between the dates. If the dates are independent, then you have 3 different CASE expressions, as the results of which you can unpivot on 3 different lines of output, like this
WITH got_dates AS
(
SELECT id
, TO_DATE (sdate, ' DD/MM/YYYY') AS sdate
, TO_DATE (ACE, ' DD/MM/YYYY') AS
, TO_DATE (edate, ' DD/MM/YYYY') AS edate
T
)
got_spds AS
(
SELECT id, As
, CASE WHEN sdate > ACE "sdate IS superior to ACEs ' END as spd_1
, CASE WHEN sdate > TRUNC (SYSDATE) THEN "sdate is greater than the current date' END as spd_2
, CASE WHEN sdate > edate THEN "sdate is greater than edate' END as spd_3
OF got_dates
)
SELECT id
As
TRUNC (SYSDATE) AS current_date
spd
OF got_spds
UNPIVOT (spd
FOR n (spd_1 AS 1
spd_2 AS 2
spd_3 AS 3
)
)
;
Or you could UNPIVOT 3 dates on 3 different ranks first and then use only 1 CASE expression. I find the above path.
-
the same should be able to dynamically with a Case expression, until I create the string asselect count(*) from vwsr_all_merged_data s where sr_tio_priority in ('Level 0','CEO','TER','Priority Assistance','Enquiry') and sr_status = 'Open' and SR_BUS_UNIT IN ('Business Support and Improvement','Finance and Administration','Cust Sat Simplification and Productivity','Project New and Customer Experience','Corp Strategy and Customer Experience','Other') COUNT(*) ---------- 126
the same query string, I put here within the subquery to make higher up the chain, it doesn't work.SELECT CASE WHEN :name = 'BS&I' THEN ('('||''''||'Business Support and Improvement'||''''||','||''''||'Finance and Administration'||','||''''||'Cust Sat Simplification and Productivity' ||''''||','||''''||'Project New and Customer Experience'||''''||','||''''||'Corp Strategy and Customer Experience'||''''||','||''''||'Other'||''''||')') ELSE null END FROM dual / CASEWHEN:NAME='BS&I'THEN('('||''''||'BUSINESSSUPPORTANDIMPROVEMENT'||''''||','|| ----------------------------------------------------------------------------------------------------------- ('Business Support and Improvement','Finance and Administration,'Cust Sat Simplification and Productivity','Project New and Customer Experience','Corp Strategy and Customer Experience','Other') SQL> var name varchar2(10) SQL> exec :name:='BS&I' PL/SQL procedure successfully completed.
Published by: user13653962 on 24/01/2013 17:09select count(*)--sr_bus_unit,count(trunc(sr_open_date)) from vwsr_all_merged_data s where sr_tio_priority in ('Level 0','CEO','TER','Priority Assistance','Enquiry') and sr_status = 'Open' and SR_BUS_UNIT IN ( SELECT CASE WHEN :name = 'BS&I' THEN ('('||''''||'Business Support and Improvement'||''''||','||''''||'Finance and Administration'||','||''''||'Cust Sat Simplification and Productivity' ||''''||','||''''||'Project New and Customer Experience'||''''||','||''''||'Corp Strategy and Customer Experience'||''''||','||''''||'Other'||''''||')') ELSE null END FROM dual ) / COUNT(*)--SR_BUS_UNIT,COUNT(TRUNC(SR_OPEN_DATE)) ------------------------------------------------ 0
Published by: user13653962 on 24/01/2013 17:10Isn't it the same
SELECT COUNT (*) FROM vwsr_all_merged_data s WHERE sr_tio_priority IN ( 'Level 0' , 'CEO' , 'TER' , 'Priority Assistance' , 'Enquiry' ) AND sr_status = 'Open' AND SR_BUS_UNIT IN ( 'Business Support and Improvement' , 'Finance and Administration' , 'Cust Sat Simplification and Productivity' , 'Project New and Customer Experience' , 'Corp Strategy and Customer Experience' , 'Other' ) AND :NAME = 'BS&I'
-
Hi all
I have a calculation in a report of Discoverer Desktop with 29 instructions BOX and still 2 case statements more to add. The problem is that my report freezes and fails to run due to the excessive number of case statements. I need to put the 31 statemnts cases in the report, how do I do this? Is there another way to do it without causing the report to freeze?
Below is part of my instructions box just to show you what I'm doing:
CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
CASES WHERE Ex = "PP" AND result IN (40,35,30,25) THEN NULL OTHERWISE
CASE WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
-CASE WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
-CASE WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...
The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END END END END...
Help, please. Thank you.Hello
You have reached the limit of characters in a calculation.Try to set into a single statement as follows:
CASE WHEN Ex = 'MM' AND THEN of OTHER NULL result (', ')
WHEN Ex = "PP" AND as a result (40,35,30,25) THEN NULL OTHERWISE
WHEN Ex = he's ' AND result IN (' a ',' B', 'C', d ') THEN NULL OTHERWISE
WHEN Ex = 'IO' AND THEN of OTHER NULL result ('Distinction', 'Merit')
WHEN Ex IN ('ZZ', 'WW', 'SS', 'KK') AND ('PASS', 'P') THEN to ANOTHER NULL result...The last statement should be BOX WHEN... THEN NULL OTHERWISE 1 END
Another option is to combine all the statements of NULL in a series of gold options like this:
BOX WHEN
(Ex = 'MM' AND the result in (', am')) OR
(Ex = "PP" AND result IN (40,35,30,25)) OR
(Ex = he's ' AND translated BY (' a ',' B', 'C', ')) OR
(Ex = 'IO' AND as a result ('Distinction', "according to merit")) OR
(Ex IN ("ZZ", "WW", "SS", "KK") AND result IN ('PASS', 'P')) THEN ANOTHER NULL...Best wishes
Michael -
Hi all
I have a table:
where the columns VALE_1 to VALE_12 represent the months... VALE_1 VARCHAR2(1) DEFAULT 'S', VALE_2 VARCHAR2(1) DEFAULT 'S', VALE_3 VARCHAR2(1) DEFAULT 'S', VALE_4 VARCHAR2(1) DEFAULT 'S', VALE_5 VARCHAR2(1) DEFAULT 'S', VALE_6 VARCHAR2(1) DEFAULT 'S', VALE_7 VARCHAR2(1) DEFAULT 'S', VALE_8 VARCHAR2(1) DEFAULT 'S', VALE_9 VARCHAR2(1) DEFAULT 'S', VALE_10 VARCHAR2(1) DEFAULT 'S', VALE_11 VARCHAR2(1) DEFAULT 'S', VALE_12 VARCHAR2(1) DEFAULT 'S',
How can I create a case in one where, according to the month that I give I can check the column for this month (VALE_month) and thus get or not to get these lines?
I'll try to explain better:
"for example, for a given DATE = 10/01/2012 (10 months, that means October), I create a simple query to only check the VALE_10 column and can have the line if its value is of."
Select * from
"Check if the VALE_10 column = s"
I hope that I was clear
Thanks in advanceHello
Vigilant Rosario wrote:
Thanks for your time.Very true, but she, the table, or up to 12 lines, because each row (transaction) can have 12 values (VALE_XX): has / for each month
(number of rows * 12)
It's true, then create a new table, fill it with 12 times the number of lines that are in your original table, and then move the columns from the original table.
If you cannot change the original table, you can create a view, which standardizes the columns:CREATE OR REPLACE VIEW notmalized_x AS WITH cntr AS ( SELECT LEVEL AS month_num FROM dual CONNECT BY LEVEL <= 12 ) SELECT x.primary_key , c.month_num , CASE c.month_num WHEN 1 THEN x.vale_1 WHEN 2 THEN x.vale_2 ... WHEN 12 THEN x.vale_12 END AS vale FROM table_x x CROSS JOIN cntr c ;
You will notice that the CASE expression in the view is very similar to the CASE expression that Solomon has suggested.
Then, you will be able to do ion of simple queries of life, like this:SELECT primary_key FROM normalized_x WHERE month_num = EXTRACT (MONTH FROM SYSDATE) AND vale = 'S' ;
If you don't want to create a view, you can do normalized_x a subquery.
-
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:50user610131 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;
-
Poor implementation of "box, when then else"?
It seems to me that my expression < next > is executed even if < WHEN > is false. I have a queury with LISTAGG, and if there are too many people, the string exceeds the limit of 4000 characters. I tried to use "BOX WHEN" to give a message instead of the list of names, but I always get an error.
I have reproduced this with the HR schema. (A simple example ;))
This was my first expression:
throw this error:SELECT department_id ,LISTAGG('Employee_ID: '||EMPLOYEE_ID ||' Name: '||FIRST_NAME ||' '||LAST_NAME ||' Email: '||EMAIL||' Phone number: '||PHONE_NUMBER||'Hiredate: '||HIRE_DATE, ',') WITHIN GROUP (ORDER BY hire_date) AS Employees FROM employees e GROUP BY department_id;
With this expression I do not exceed the limit of 4000 characters and the message "too many employees" are correctly displayed.ORA-01489: resultatet av strengsammenkjeding er for langt 01489. 00000 - "result of string concatenation is too long" *Cause: String concatenation result is more than the maximum size. *Action: Make sure that the result is less than the maximum size.
For this Department 50 expression exceeds the limit of 4000 characters, and I get the above error. But the 50 Department has 45 employees and should not be the same in the part 'THEN'SELECT department_id ,CASE WHEN ((select count(*) from employees where department_id=e.department_id) < 30) THEN (LISTAGG('Employee_ID: '||EMPLOYEE_ID ||' Name: '||FIRST_NAME ||' '||LAST_NAME ||' Email: '||EMAIL||' Phone number: '||PHONE_NUMBER, ',') WITHIN GROUP (ORDER BY hire_date)) ELSE 'Too many employees' END AS Employees FROM employees e GROUP BY department_id;
Oracle is a very silly implementation of ' BOX WHEN.. "or I do something wrong?SELECT department_id ,CASE WHEN ((select count(*) from employees where department_id=e.department_id) < 30) THEN (LISTAGG('Employee_ID: '||EMPLOYEE_ID ||' Name: '||FIRST_NAME ||' '||LAST_NAME ||' Email: '||EMAIL||' Phone number: '||PHONE_NUMBER||'Hiredate: '||HIRE_DATE, ',') WITHIN GROUP (ORDER BY hire_date)) ELSE 'Too many employees' END AS Employees FROM employees e GROUP BY department_id;
I can solve this another way?
Kind regards
ThomasA way out could be
SQL> SELECT department_id, SUBSTR (NVL (listagg (str, ',') WITHIN GROUP (ORDER BY hire_date), 'Too many employees'), 1, 20) || ' ... ' employee FROM (SELECT department_id, hire_date, CASE WHEN COUNT (*) OVER (PARTITION BY department_id) <= 30 THEN 'Employee_ID: ' || employee_id || ' Name: ' || first_name || ' ' || last_name || ' Email: ' || email || ' Phone number: ' || phone_number || 'Hiredate: ' || hire_date END str FROM employees e -- WHERE department_id != 50 OR department_id IS NULL ) GROUP BY department_id / DEPARTMENT_ID EMPLOYEE ------------- ------------------------- 10 Employee_ID: 200 Nam ... 20 Employee_ID: 201 Nam ... 30 Employee_ID: 114 Nam ... 40 Employee_ID: 203 Nam ... 50 Too many employees ... 60 Employee_ID: 103 Nam ... 70 Employee_ID: 204 Nam ... 80 Too many employees ... 90 Employee_ID: 100 Nam ... 100 Employee_ID: 109 Nam ... 110 Employee_ID: 205 Nam ... Employee_ID: 178 Nam ... 12 rows selected.
Maybe you are looking for
-
YouTube slow to change the size of the screen
Just updated to FF42 and noticed a problem with the change of YouTube screen size. More specifically, when I change the screen full-size and vice versa full to view normal change it is significantly more slow then before. I don't see this problem on
-
Satellite A80 - need help for recovery
Hello I have a laptop Toshiba A80 and I'm trying to re - install the default operating system. I inserted the recovery disk and I click on continue to start the process, yes ok to wipe and then I get an error message. Error 1851 "file entered does no
-
Double monitors the Macbook Pro 15 "
Hello! I'm looking at the Samsung 29 "LED curve S29E790C, I have a 2015 Macbook Pro 15". My goal would be to run 2 of these monitors chained to the stroke on this laptop, closing the lid. Is this possible?
-
compatibility of windows 8 Microsoft with the sidewinder gamepad
Just replaced my worn on slimline. Really unhappy with the new windows operating system. Now I buy the new office and worse still, my microsoft sidewinder force feedback2 gamepad does not work for my flight sims. Guess they want to get rid of everyth
-
I ended the trial of recovery vista window and the credit card was not credited back
I ended the trial of window vista recovery program has been removed from the computer and my credit below was not credited back