Pivot or CASE Statement
Env: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
I am learning pivot functions and looking for your expertise.
I have a list of unique addresses but with different facility types ( residential, business). How do I count the facility type and pivot the results as shown below.
2 new pivot columns to be created is "RES_TYPE" and "BUS_TYPE".
Thanks for your help.
Final result:
STREET,HOUSE_NUMBER,TOTAL_COUNT,RES_TYPE,BUS_TYPE
VENTURE DR SE, 9008,5,3,2
AVENGER DR SW,300,3,1,2
Table create script:
CREATE TABLE TEMP_TABLE
STREET VARCHAR2(30 BYTE),
HOUSE_NUMBER VARCHAR2(6 BYTE),
TYPE_OF_FAC CHAR(1 BYTE)
Insert Script:
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'R');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'R');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'B');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'R');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'B');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('AVENGER DR SW', '300', 'B');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('AVENGER DR SW', '300', 'B');
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('AVENGER DR SW', '300', 'R');
COMMIT;
Hi,
Welcome to the forum!
1005674 wrote:
Env: Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
I am learning pivot functions and looking for your expertise.
I have a list of unique addresses but with different facility types ( residential, business). How do I count the facility type and pivot the results as shown below.
2 new pivot columns to be created is "RES_TYPE" and "BUS_TYPE".
Thanks for your help.
Final result:
STREET,HOUSE_NUMBER,TOTAL_COUNT,RES_TYPE,BUS_TYPE
VENTURE DR SE, 9008,5,3,2
AVENGER DR SW,300,3,1,2
Do you want CSV output, like you posted above, or do you want separate columns, like this:
STREET HOUSE_ TOTAL_COUNT RES_TYPE BUS_TYPE
AVENGER DR SW 300 3 1 2
VENTURE DR SE 9008 5 3 2 Here's one way to get separate columns:
SELECT street, house_number
, res_type + bus_type AS total_count
, res_type
, bus_type
FROM temp_table
PIVOT ( COUNT (*)
FOR type_of_fac IN ( 'R' AS res_type
, 'B' AS bus_type
ORDER BY street, house_number
Table create script:
CREATE TABLE TEMP_TABLE
STREET VARCHAR2(30 BYTE),
HOUSE_NUMBER VARCHAR2(6 BYTE),
TYPE_OF_FAC CHAR(1 BYTE)
Insert Script:
Insert into TEMP_TABLE (STREET, HOUSE_NUMBER, TYPE_OF_FAC) Values ('VENTURE DR SE', '9008', 'R');
...Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful!
Similar Messages
-
Difference beween Case Statement and Pivot Operator in a OWB mapping
Hi ,
Kindly clarify what is the difference between using a PIVOT operator and the CASE statement in a Expression Operator in a particular mapping.
rgds
ArinjitHi
With PIVOT operator you can transform columns to rows. For example if oyu have 12 column (one for every months) than you can create 12 rows with one column of month data.
If you want to do this without PIVOT you can do it with union (12 times) or with
something like this.
In this case you can't use CASE statement I think.
If you want to do the reverse of this, create 12 column from 12 rows, you can use UNPIVOT operator. In this case you can use CASE statement and aggregating.
Ott Karesz
http://www.trendo-kft.hu -
Need help in this sql query to use Case Statement
hi All,
I have the below query -
SELECT DISTINCT OFFC.PROV_ID
,OFFC.WK_DAY
,CASE
WHEN OFFC.WK_DAY ='MONDAY' THEN 1
WHEN OFFC.WK_DAY ='TUESDAY' THEN 2
WHEN OFFC.WK_DAY ='WEDNESDAY' THEN 3
WHEN OFFC.WK_DAY ='THURSDAY' THEN 4
WHEN OFFC.WK_DAY ='FRIDAY' THEN 5
WHEN OFFC.WK_DAY ='SATURDAY' THEN 6
WHEN OFFC.WK_DAY ='SUNDAY' THEN 7
END AS DOW
,OFFC.OFFC_OPENG_TIME
,OFFC.OFFC_CLSNG_TIME
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID='0000600'
WITH UR;
this query is bringing results in 6 differnt rows with opening and closing time for each day separately. I want to generate the data in one row with each day having opening and closing time, so for 7 days, total 14 columns with opening and closing time. But i am not able to do that using case statement.
can somebody help me in achieving that.
thanks,
iamhereHi,
Welcome to the forum!
That's called a Pivot .
Instead of having 1CASE expression, have 14, one for the opening and one for the closing time each day, and do GROUP BY to combine them onto one row.
SELECT OFFC.PROV_ID
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_OPENG_TIME END) AS mon_opn
, MIN (CASE WHEN OFFC.WK_DAY ='MONDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS mon_cls
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_OPENG_TIME END) AS tue_opn
, MIN (CASE WHEN OFFC.WK_DAY ='TUESDAY' THEN OFFC.OFFC_CLSNG_TIME END) AS tue_cls
FROM GGDD.PROV_OFFC_HR OFFC
WHERE OFFC.PROV_ID = '0000600'
GROUP BY offc.prov_id
;This assumes there is (at most) only one row in the table for each distinct prov_id and weekday. If not, what do you want to do? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
The staement above works in Oracle 8.1 and up, but there's a better way (SELECT ... PIVOT) available in Oracle 11. What version are you using? (It's always a good idea to include this when you post a question.)
Edited by: Frank Kulash on Jan 6, 2011 8:22 PM -
Case Statement alternative in answers...
Hi All-
we are using a case statement in one of the row fields in answers..The report is in pivot format.
It is catching only the first statement and ignoring the second statement by printing '0'.
The following is the case statement which we are using:
case when
"Fact"."Location" <> 'X'
then
(" Fact"."Service Revenue" / "Fact"."Hours Budget")
else
"Fact"."Hourly Rate"
end
Hourly rate is the field in the existing table which we are using for the report.. the issue is it is ignoring to print this field when location = 'x'
we have tried the filter function also but it is allowing only one filter statement.
Could you please look into this?
Thanks!
Message was edited by: user13816768
Message was edited by: user13816768Hi
why cant u use the condition in RPD.
In rpd use the case condition and make that as a one logical column. Test=" Fact"."Service Revenue" / "Fact"."Hours Budget". make it as a logical sum.
Then bring this column in to answerside, and use the case condition
case when
"Fact"."Location" <> 'X'
then
(" Fact""Test")
else
"Fact"."Hourly Rate"
end
Regards,
VG -
Case Statement Error - Need help
What's wrong with this case statement
Case When Opportunity."Probability %" = '0' then "- Product Revenue"."Product Revenue (000)" *( Opportunity."Probability %" / 100) else "- Product Revenue"."Product Revenue (000)" *( "Opportunity - Product"."Probability %" / 100) end if
I am getting an error when I place this in a pivot tableDisregard, it is working now
-
Case statements to count Contact numbers
Hi,
I'm trying to use CASE IF or WHEN statements to count simple Contact numbers by owner or region. I need to count the following in separate fields of course:
Number of contact type = Customers
Number of Contact types = Propects
Number of Contacts at each seniority level (6 levels)
Number of contact validated (tick box)
I presume I need to write a CASE statement that I can then use to Sum for each column but I'm struggling with CASE statements to apply an CASE IF Column equals 'Customer' then count 1 for example. Never done these before so any help or advice greatly appreciated.
Contact type field is Contact."Contact Type"
Contact Seniority is Contact.PICK_3
Thanks,
Chrishere is an example:
sum (case when contact type = 'Customers' then 1 else 0 end)
Do the same for the second and forth in your list as above ;
include the Contact.PICK_3 and # of contact fields in step 1;
add region, owner, # of Customers, # of Prospects and # of validated in row section of your pivot view;
add Contact.PICK_3 to column section;
add # of contact to the measurement section. -
SQL Expression in decode function or case statement?
Can I put SQL expressions in decode function or case statement?
For example,
select le.profile, decode( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile, 0, 'N', 'Y')
from element le;
or
select le.profile, case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0 THEN 'N'
ELSE 'Y'
from element le;
None of the above work.
Can anyone tell me how to make it work?
Is there any workaround?
Thanks,
JYou simply needed and END to your CASE statement;
SQL> with profile_data as (
select 'XXXX_AFTER' name, 1 object_id from dual),
element as (
select 1 profile from dual union all
select 2 from dual)
select le.profile,
case WHEN ( select count(1) from profile_data where NAME= 'XXXX_AFTER' and object_id = le.profile) = 0
THEN 'N'
ELSE 'Y'
END new_col
from element le
PROFILE N
1 Y
2 N -
CASE not found while executing CASE statement on Submit Form
Hi to all APEX users and developers.
I have several APEX applications and they are all working well, but recently I got one strange exception when I try to submit page:
Session: Fetch session header information
...metadata, fetch page info
...Validate item page affinity.
...Validate hidden_protected items.
Add error onto error stack
...Error data:
......message: Error processing request.
......additional_info: ORA-06592: CASE not found while executing CASE statement
......display_location: ON_ERROR_PAGE
......is_internal_error: true
......apex_error_code: APEX.UNHANDLED_ERROR
......ora_sqlcode: -6592
......ora_sqlerrm: ORA-06592: CASE not found while executing CASE statement
.....error_backtrace: ORA-06512: at "APEX_040100.WWV_FLOW", line 9273
......component.type: APEX_APPLICATION_AUTH
......component.id: 41350431648668800
......component.name: MNRFR
...Show Error on Error Page
......Performing rollback
Processes - point: AFTER_ERROR_HEADER
Processes - point: BEFORE_ERROR_FOOTER
End Page Processinga
Page has more than 120 items (most of them are hidden), so my first thought is that page has problem with posting so many items, but APEX error message doesn't hel me at all. Any help would be very appreciated :)
AlmirHi Almir,
actually it is the 100 page item limit. See (http://download.oracle.com/docs/cd/E17556_01/doc/user.40/e15517/limits.htm)
I also had a look into the source code and the code at the position where the error gets raised only supports 100 page items.
Can you have a look into the generated HTML code and look if you have a page items which is mapped to p_t101 or a higher number ?
I will file a bug to show a better error message.
Regards
Patrick
My Blog: http://www.inside-oracle-apex.com
APEX Plug-Ins: http://apex.oracle.com/plugins
Twitter: http://www.twitter.com/patrickwolf -
hi.
i have a case statement query. i wonder if in the select statement, can i do computation using different parameter from the main report to subreport?
for example, (qty * parameter)
if parameter = 0, i uses $P{abc} to multiply qty
if parameter > 0, i uses $P{xyz} to multiply qty
in this case, i've writen a sql (as below) but its does not execute.
select ....., (QTY *
case $P{QTY}
when 0 then ' * $P{abc}'
else ' $P{xyz}'
end
from....
hence, what should i do in order to get the right parameter to multiply with? pls guide. thanks.I'm not sure about these parameter placeholders which are specific to whatever report tool you are using, but the structure would be (assuming the parameter value would never be less than zero):
qty * case when $P{QTY} = 0 then $P{abc} else $P{xyz} end -
Case Statement in Answers Filter
Hi,
I'm trying to build a query in Answers that filters the data returned based on the current month number. If the current month is 1 then we want to show all 12 months, otherwise we want to show months less than the current month. If I use the following:
CASE WHEN extract(month from current_date) = 1 THEN "Calculated Values Monthly"."MONTH" >= 1 ELSE "Calculated Values Monthly"."MONTH" < extract (month from current_date) END
I get the error below. All works fine until I add the CASE.
Error Codes: YQCO4T56:OPR4ONWY:U9IM8TAC:OI2DL65P
Location: saw.views.evc.activate, saw.httpserver.processrequest, saw.rpc.server.responder, saw.rpc.server, saw.rpc.server.handleConnection, saw.rpc.server.dispatch, saw.threadpool, saw.threadpool, saw.threads
Odbc driver returned an error (SQLExecDirectW).
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27002] Near <>=>: Syntax error [nQSError: 26012] . (HY000)
Can you use a case statement in a filter? If not, what are the other options, if any?
Thxziekc wrote:
Hi,
I'm trying to build a query in Answers that filters the data returned based on the current month number. If the current month is 1 then we want to show all 12 months, otherwise we want to show months less than the current month. If I use the following:
CASE WHEN extract(month from current_date) = 1 THEN "Calculated Values Monthly"."MONTH" >= 1 ELSE "Calculated Values Monthly"."MONTH" < extract (month from current_date) END
I get the error below. All works fine until I add the CASE.
Error Codes: YQCO4T56:OPR4ONWY:U9IM8TAC:OI2DL65P
Location: saw.views.evc.activate, saw.httpserver.processrequest, saw.rpc.server.responder, saw.rpc.server, saw.rpc.server.handleConnection, saw.rpc.server.dispatch, saw.threadpool, saw.threadpool, saw.threads
Odbc driver returned an error (SQLExecDirectW).
State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 43113] Message returned from OBIS. [nQSError: 27002] Near <>=>: Syntax error [nQSError: 26012] . (HY000)
Can you use a case statement in a filter? If not, what are the other options, if any?
ThxAccording to your requirements, when the month is January, you want all 12 months; any other number and you want months from 1 to "1 less than the current month number." Yes? Okay, here's what to do:
In the filter on your date column, click on the filter button, convert it to SQL and enter this:
MONTH(tablename.date_column) BETWEEN 1 AND CASE WHEN MONTH(CURRENT_DATE) = 1 THEN 12 ELSE MONTH(CURRENT_DATE)-1 END
Here's the logic:
a) When the current month is January, then the case statement will yield 12 and the filter will be:
WHERE MONTH(tablename.date_column) BETWEEN 1 AND 12 ...or all 12 months.
b) When the current month is any other month, say June, then the case statement will yield one less than the what the month number is, or in this case 5.
WHERE MONTH(tablename.date_column BETWEEN 1 AND 5 ...or all the months from 1 to 5 (Jan through May)
This will give you what you want... -
CASE statement in Calculated column
Hi Frzz,
I have below requirement in Calculated column with CASE statement. Could some one help me how to achieve this with case statement.
String = 0Hello
01Hello
012Hello
0123Hello
If 1st Character of the string is '0' then - 0Hello
1st 2 characters of the String is '01' - 22Hello
1st 3 characters of the String is '01' - 333Hello
1st 4 characters of the String is '01' - 4444Hello
Thank you.
Best Regards,
Krishna.Hi Krishna ,
Using IF and MATCH to do that: ( I took one of the conditions you specified )
IF(match("STRING1",'??0??'),'333Hello',"STRING1")
Output:
Regards,
Krishna Tangudu -
Hi PL/SQL experts,
I'm going a bit loopy here, so could someone please point out what I'm doing wrong with this case statement:
Test procedure is:
CREATE OR REPLACE procedure SCOTT.postcode_validate_2 (input_post_code VARCHAR2) as
alphabet_string VARCHAR2(52) := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxwyz';
number_string VARCHAR2(10) := '012345789';
last_part_postcode VARCHAR2(3) := NULL;
BEGIN
IF INSTR(input_post_code,' ') = 0
THEN
DBMS_OUTPUT.PUT_LINE('We need a space in the postcode please');
GOTO exit;
ELSIF LENGTH(SUBSTR(input_post_code,instr(input_post_code,' ')+1)) > 3
THEN
DBMS_OUTPUT.PUT_LINE('Last part of postcode can only be 3 characters');
GOTO exit;
ELSE
last_part_postcode := SUBSTR(input_post_code,instr(input_post_code,' ')+1);
END IF;
CASE input_post_code
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 2
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A9')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA9')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A99')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A99');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0
AND instr(alphabet_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A9A')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9A');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 4
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0
AND instr(number_string,substr(input_post_code,4,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA99')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA99');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 4
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0
AND instr(alphabet_string,substr(input_post_code,4,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA9A')
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9A');
-- GOTO exit;
-- END IF;
END;
-- Check last part of format, should be AA9
IF (instr(number_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(alphabet_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the correct format, 9AA');
ELSE
DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the wrong format!');
END IF;
<<exit>>
DBMS_OUTPUT.PUT_LINE('Please try again');
END;
However, I'm getting the following error:
LINE/COL ERROR
37/6 PLS-00103: Encountered the symbol "WHEN" when expecting one of
the following:
:= . ( % ;
On a second note, can I not have the ELSE structure embedded within the case (currently commented out)?
Thanks very much in advance.
DevFixed code:
CREATE OR REPLACE procedure postcode_validate_2 (input_post_code VARCHAR2) as
alphabet_string VARCHAR2(52) := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvxwyz';
number_string VARCHAR2(10) := '012345789';
last_part_postcode VARCHAR2(3) := NULL;
BEGIN
IF INSTR(input_post_code,' ') = 0
THEN
DBMS_OUTPUT.PUT_LINE('We need a space in the postcode please');
GOTO exit;
ELSIF LENGTH(SUBSTR(input_post_code,instr(input_post_code,' ')+1)) > 3
THEN
DBMS_OUTPUT.PUT_LINE('Last part of postcode can only be 3 characters');
GOTO exit;
ELSE
last_part_postcode := SUBSTR(input_post_code,instr(input_post_code,' ')+1);
END IF;
CASE
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 2
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A9');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA9');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A99');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A99');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 3
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(number_string,substr(input_post_code,2,1)) != 0
AND instr(alphabet_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format A9A');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format A9A');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 4
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0
AND instr(number_string,substr(input_post_code,4,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA99');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA99');
-- GOTO exit;
-- END IF;
WHEN (length(substr(input_post_code,1,instr(input_post_code,' ')-1)) = 4
AND instr(alphabet_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(number_string,substr(input_post_code,3,1)) != 0
AND instr(alphabet_string,substr(input_post_code,4,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('We have a valid postcode in the format AA9A');
-- ELSE
-- DBMS_OUTPUT.PUT_LINE('Sorry but that is an incorrect postcode! Format AA9A');
-- GOTO exit;
-- END IF;
END CASE;
-- Check last part of format, should be AA9
IF (instr(number_string,substr(input_post_code,1,1)) != 0
AND instr(alphabet_string,substr(input_post_code,2,1)) != 0
AND instr(alphabet_string,substr(input_post_code,3,1)) != 0)
THEN
DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the correct format, 9AA');
ELSE
DBMS_OUTPUT.PUT_LINE('End part of the postcode is in the wrong format!');
END IF;
<<exit>>
DBMS_OUTPUT.PUT_LINE('Please try again');
END;
/As VG2 pointed out you were missing some semicolons.
Also, CASE input_post_code was replaced with just CASE since you are doing a searched case.
Also, END at the end of the case statement was replaced with END CASE. -
Case statement in advanced sql
how to use case statement in advanced sql.Example if quarter=1 i need to select jan,fev,and march months from table
this is the case statement i am using :
case when 1=@{quarter} then substring(cast((etxnmis.month_year) as char) from 5 for 2) IN ('03', '04', '05') else null end
and i get the below error
: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 27002] Near <IN>: Syntax error [nQSError: 26012] . (HY000) -
T-sql case statement in a select
When I execute the following t-sql 2012 statement, the "NO Prod' value is not
being displayed from the sql listed below:
SELECT DISTINCT
IsNull(cs.TYPE,'') as type,
CASE IsNull(Course.TYPE,'')
WHEN 'AP' then 'AP Prod'
WHEN 'IB' then 'IB Prod'
WHEN 'HR' then 'HR Prod'
WHEN '' then 'NO Prod'
END AS label
FROM CustSection cs
INNER JOIN dbo.Person p on P.personID = cs.personID
Left join customCustomer cs564 on cs564.personID = p.personID and
cs564.attributeID ='564'
where ( cs.type is null and cs564.attributeID = null)
or
(cs.type IN ('HR','AP') OR
(cs.type='IB' AND SUBSTRING(cs.code,1,1)='3'))
ORDER BY label
What I want is for 'NO Prod' to be displayed when
cs.type is null and cs564.attributeId is null.
Thus can you tell me how to fix query above so the 'NO Prod' value is displayed in the
select statement listed above?There is no CASE statement in SQL; we have a CASE expression. We do not use the old 1970's Sybase*- ISNULL(); we have COALESCE().
There is no such thing as a magic generic “type” in RDBMS. There is no such thing as a generic “code” in RDBMS. They have to to be “<something in particular>_type” and “<something in particular>_code” in a valid data model. How about blood_type
and postal_code??
There is no such thing as a generic “person” table in RDBMS. First of all, do you really have only one person, as you said?? But the important point is that these persons play a role in the data model – customers, students, etc. You are doing the wrong thing
and doing it badly. This table should not exist any more than a table of “Things” such exist.
And the reason you are beyond any real help is “attribute_id” which tell us that your schema is a total disaster of data and meta data mixed together in a non-RDBMS written in awful SQL. Based on cleaning up bad SQL for 30 years, it looks like you are an OO
programmer who never unlearned his prior mindset.
Why did you allow an encoding schema with blanks? Why do you have so many NULL-able columns?
SELECT DISTINCT is very rare in a properly designed schema. The DRI references assure that rows cam be matched. To get you started, look at this skeleton:
CREATE TABLE Products
(product_gtin CHAR(15) NOT NULL PRIMARY KEY,
product_type CHAR(2) DEFAULT 'XX' NOT NULL
CHECK (product_type IN ('AP', 'IB', 'HR', 'XX'))
The table name is a plural noun because it models a set (NOT an OO class).
The GTIN is an industry standard identifiers, and not have to invent our own.
The product_type (not blood_type, not automobile_body_type!) has a constraint that assures it is never NULL and never blank; I invented 'XX' as a default.
You need more help than you can get in a forum, but if you will follow Netiquette and post the DDL, we can get you started.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
I want to do something like this in a SELECT where I set the value of one field based on another. Is this possible. If not are there any alternatives? Thanks
CASE WHEN [var_cpnt_brkpt_ct] = '0' THEN [enrc_cpnt_var_price_rt] = '0'There is no CASE statement in SQL; we are a declarative language. We have a case expression! There is no control flow, like you are trying to write. You do not even know that fields are columns. This is as silly
as asking for the color the feathers on a fish.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in
Sets / Trees and Hierarchies in SQL
Thank you for your support. Good day Sir.
Maybe you are looking for
-
i have had various problems with my 6280 including restriction codes and other common problems but this seems to be a new one not yet discussed. the memory on my 6280 seems to have disappeared. it should have approx 4mb of internal memory but mine is
-
I bought an iMac, but I have a PC full of pictures with 3 External HDD. Now I need transfer all my information like catalogs, license, etc to iMac. I know that 2 of my externals HD isn't compatible with Mac and Mac always ask for format the HD before
-
Sub:when trying to open "adminisration server configuration"
Hi all, when i am trying to open component definitions in admin server configuration, I am getting following error. An error has occurred creating business component 'Enterprise Service Definitions' used by business object 'Server Admin'. Please ask
-
Can't open parent folder by cmd-clicking
I should be able to open parent folders by cmd-clicking a Spotlight search result or the title bar of a Finder or document window. But when I do this, nothing happens, except in Finder windows, where it still works fine. Nothing happening in the Cons
-
Can't set printer as default. Error code 0x0000709
HP3510 Deskjet printer instaled on Hp laptop running windows 7. Cannot set printer as default. Error code 0x0000709. Removed old computer and software This question was solved. View Solution.