Plz help me in case statement
Hi All,
In a table OT_C_S_R_HEAD in this there is a column "CSR_STATUS" having datatype "Number" and i have to apply case on this that if "2 or null is no, 1 is yes".
But apply getting some error "ORA-00932: inconsistent datatypes: expected NUMBER got CHAR"
SELECT
CSR_AOE_NO
, CASE CSR_STATUS
WHEN '2' THEN ('NO')
WHEN '1' THEN ('YES')
WHEN NULL THEN ('NO')
END CSR_STATUS
FROM OT_C_S_R_HEAD
plz reply asap....
Welcome to the forum!
This is NOT the forum for SQL or PL/SQL questions. As the title says this forum is for sql developer questions only.
If you have any additional comments or follow please DO NOT followup in this forum but repost the question in the sql and pl/sql forum.
PL/SQL
>
In a table OT_C_S_R_HEAD in this there is a column "CSR_STATUS" having datatype "Number" and i have to apply case on this that if "2 or null is no, 1 is yes".
But apply getting some error "ORA-00932: inconsistent datatypes: expected NUMBER got CHAR"
>
The reply that you marked CORRECT will not cause the exception you were receiving but it will also NOT give you the results you want.
create table a (col1 number)
insert into a values (null)
insert into a values (1)
insert into a values (2)
insert into a values (3)
SELECT col1, CASE col1
WHEN 2 THEN ('NO')
WHEN 1 THEN ('YES')
WHEN NULL THEN ('NO')
END test
FROM a
COL1,TEST
1,YES
2,NO
3,You said you wanted 'NO' if the column has a NULL value but that query gives you a NULL. The query will also give you NULL for values other than 1 or 2 because there is no ELSE clause. Except in unusual cases there should always be an ELSE clause as a 'catch all'.
If all you want is YES for a 1 and NO for anything else just use
SELECT col1, CASE col1
WHEN 1 THEN ('YES')
ELSE ('NO')
END test
FROM a
COL1,TEST
,NO
1,YES
2,NO
3,NO
Similar Messages
-
Need some help with a case statement implementation
I am having trouble using a CASE statement to compare values and then display the results. The other issue is that i want to put these results in a separate column somehow.
Heres how the code would look:
SELECT "Task"."Code",
"Stat" = CASE WHEN "Task.Code" = 1 THEN string
....and so on
I wanted to make "Stat" the new column for the results, and string represents the string to be assigned if 1 was the value for code. I keep getting syntax error, any help would be nice.This is a lot easier than you might think.
1) First, move another column of "Code" to your workspace.
2) Click on the fx button and then on the BINS tab.
3) Click on "Add BIN" and with the operand on "is equal to/is in," input 1 and then click "OK."
4) Name this what you had for "string."
Repeat for all the different values you want to rename as another "string" value.
5) Finally, check the "custom heading" checkbox, and rename this column "Stat" as you indicated.
That's it. -
hi there, i have my 2.16 core 2 duo mbp for almost 2 weeks and iv noticed when the lid is closed there is a 1 mm gap on one side is this normal? im using a marware protection pack but would like it if i didnt have to tkae it off. what do i do should i take it to an apple centre to ask about it ? will this get any worse than it is? plz help anyone thanks alot.
Are you able to confirm whether the case is warping or whether it's a simple case of the hinge not be quite aligned. There's a big difference. If it concerns you then take it to an Apple Store and have a Genius look at it.
This sort of thing does happen and does not affect the use of your MBP. I personally wouldn't worry about if it doesn't affect your ability to close the lid although everyone is different. -
Please help me with a CASE Statement:
- When ID = 15, 16, 17, 18 then "Bad"
- when ID = 19, then "Average"
- when ID = 21, then "Good"
- else "Null"
Thank you!!Well the 1st thing to do would be to correct my poor spelling... change Delault : to Default :
Don't know why you would get an error stating "The result of selection formula must be a boolean". It's working fine on my machine.
If your ID field is numbers stored text you have a couple different options...
1) Convert the ID to a number...
Select ToNumber({home.noone_ID})
2) Wrap the ID values in double quotes...
Case "15", "16", "17", "18" :
"BAD"
Even if this were your problem... the error should be something other than the boolean thing...
Jason -
Help with multiple case statements
Hello,
I am new to BO. I am on XI 3.0. SQL 2005. In Designer, I am trying to create a measure in a financial universe that would end up being multiple case statements within one select. This is what I tried to do, but it doesn't work with the two case statements. Can I use an ELSE leading into the second CASE WHEN somehow? How can I accomplish this? Sorry for my ignorance!
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 12 THEN dbo.ClientBudgetMonth.Stage1Sales END
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month = 11 THEN dbo.ClientBudgetMonth.Stage1Sales END
Any Suggestions?
Thanks,
HollyHolly,
I don't know enough about your data or requirement to provide a solution, however, the construct that you post will not work because it causes you to build an object with multiple case statements when only one case statement per object is permitted. From what I see in your code I would be inclined to combine the two statements into one as such:
CASE WHEN dbo.ClientBudgetYear.DateStage1Approved > 01/01/1900 AND dbo.ClientBudgetMonth.Month in (11,12) THEN dbo.ClientBudgetMonth.Stage1Sales else null END
Thanks,
John -
Help with Switch-Case Statement
How do you get this in a switch-case statement or work with it?
if (age < 70) {
JOptionPane.showMessageDialog(null, "People that are below the 70s are nothing special.");
else if (age > 69 && age < 80) {
JOptionPane.showMessageDialog(null, "People that are in their 70s are called septuagenarian.");
else if (age > 79 && age < 90) {
JOptionPane.showMessageDialog(null, "People that are in their 80s are called octogenarian.");
else if (age > 89 && age < 100) {
JOptionPane.showMessageDialog(null, "People that are in their 90s are called nonagenarian.");
else (age > 99 && age < 110) {
JOptionPane.showMessageDialog(null, "People that are in their 100s are called centenarian.");
}Thanks~As per Java Specification, swtich case expects an integer and boolean cannot be used as param for switch.
In your case switch can be used like this.
int index = age /10;
switch(index) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
Your First case
break;
case 7:
your second case
break;
case 8:
third case
break;
case 9:
fourth case
break;
default:
fifth case
break;
Take note of the break statements. Its very important. But I wont prefer in this case. Code looks awkward and is not so meaningful..... -
Help With A Case Statement With Multiple Variables
I apologize if this is the incorrect Forum for this type of question, but it was the closest one that I could find. I'm pretty new with SQL and am stuck on this issue. I have roughly 26 dates that I need to compare to one another. Each date is tied to a step code. I also have a Stop value that is tied directly to the "max date" of the step codes. So, I need to compare 30 dates against one another to 1st - ID the max date; 2nd - ID if the Stop value is correct; 3rd - if the stop value is incorrect, identify what the correct value would be.
At first, this seemed like it wouldn't be that hard. I wrote a query that found the max date for each step code. Then I realized that multiple step codes could have the same date. So, I tried using this case statement, but I did not get the expected results. Is there a more efficient way of getting what I need? This code seems like it's not necessary and probably the source of my issue.
CASE
WHEN FS25.ACTUAL_COMPLETION_DATE > FS.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS1.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS2.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS3.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS4.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS5.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS6.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS7.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS8.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS9.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS10.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS11.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS12.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS13.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS14.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS15.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS16.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS17.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS18.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS19.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS20.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS21.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS22.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS23.ACTUAL_COMPLETION_DATE AND FS25.ACTUAL_COMPLETION_DATE > FS24.ACTUAL_COMPLETION_DATE AND L.FORECLOSURE_STOP_CODE <= '8' THEN '9'
ELSE 'UH OH'
END AS "CHANGE FC STOP TO"
Any assistance is appreciated!I think Igor pointed out a working solution before.
Applying it at your examples (you missed the operator after STOP_CODE, I assume it =):
CASE
WHEN FS25 = GREATEST(FS25, FS24, FS23) AND STOP_CODE = '9' THEN '9'
ELSE 'UH OH'
END AS 'CHANGE STOP CODE TO'
{code}
Be careful at the second example. You are checking:
{code:sql}
FS25 > FS24 OR FS25 IS NOT NULL AND FS24 IS NULL AND FS25 > FS23
OR
FS25 IS NOT NULL AND FS23 IS NULL AND STOP_CODE = '9'
{code}
Remember that AND has higher priority among operators than OR so if FS25 is greater than FS24 and FS23 the condition will be true even if STOP_CODE is not equal 9.
Regards.
Al -
Need help to write case statement
Hi Guys,
I have dashboard in which its has pprompt called PROPMT1 in that prompt it has three values A,B,C
but iwant the full abbreviation of those values like A=APPLE B=BOY and C=Cindy
and also in the report i have the coloumn is prompted by PROMPT1 which has again A,B,C values that need to be modified as A=APPLE B=BOY and C=Cindy
how can i do it with case statement.For the prompt, write this in the Show window>SQL Results:
1) CASE WHEN 1=0 THEN char_columnname ELSE 'A = APPLE' END FROM "your subject area" UNION ALL CASE WHEN 1=0 THEN char_columnname ELSE 'B = BOY' END FROM "your subject area" UNION ALL CASE WHEN 1=0 THEN char_columnname ELSE 'C = Cindy' END FROM "your subject area"
2) Save the prompt to a presentation variable, pv_choice
This will give you your three values. Make sure you use a CHAR column, doesnt matter what column, though.
For the report,
1) Create a BINS column on the column that contains the A, B, C values and make the BINS A = APPLE, B = BOY, C = Cindy just like the prompt values.
2) Now put a filter on this BINS column and set it equal to your pv_choice presentation variable.
That's it.
NOTE: Obviously, if you want the prompt values to just say "APPLE," "BOY," and "Cindy," then take off the 'A = ', 'B = ' and 'C = ' appropriately. Same for the BINS in your report.
Edited by: David_T on Jun 8, 2011 10:57 AM -
Need help about switch & case statements
Dear friends
I am new student of Java
Our University gave us project to make a program. Here is the program:
a- Write a java program ,so the user can input the monthly salary then compute the net salary of an employee after deducting the tax ,use the following table to calculate the tax .Note use switch statement .
Annual salary Tax
100- 999 0
1000- 1999 5%
2000- 3999 7%
4000- 7000 10%
7000 13%Please help me how could I use switch statement.
All I know about switch statement is that I have use Integers for selection.
Kindly help me. I have only 2 days to submit my assignment.
Thanks in Advance
Edited by: syedejaz on Aug 13, 2008 8:09 AM
Edited by: syedejaz on Aug 13, 2008 8:10 AMHello every body:
I tried to make my first program using if then else but the problem is that no calcuation is happinig
/* TMA01 ITC250, Part-III, a
Program which gets input monthly salary and shows net salary after deducting given tax.
File: netSalary.java
import javax.swing.*;
import java.text.*;
public class netSalary {
public static void main(String[] args) {
int Salary=0,Tax=0,monthlySalary=0;
monthlySalary= Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Monthly Salary:"));
if ((monthlySalary>=100) && (monthlySalary<=999)) {
Tax = (monthlySalary*(0/100));
Salary = (monthlySalary-Tax);
JOptionPane.showMessageDialog(null, "Net Salary is:" +Salary);
} else if ((monthlySalary>=1000) && (monthlySalary<=1999)) {
Tax = (monthlySalary*(5/100));
Salary = (monthlySalary-Tax);
JOptionPane.showMessageDialog(null, "Net Salary is:" +Salary);
} else if (monthlySalary>=2000 && monthlySalary<=3999) {
Tax = (monthlySalary*(7/100));
Salary = (monthlySalary-Tax);
JOptionPane.showMessageDialog(null, "Net Salary is:" +Salary);
} else if (monthlySalary>=4000 && monthlySalary<=7000) {
Tax = (monthlySalary*(10/100));
Salary = (monthlySalary-Tax);
JOptionPane.showMessageDialog(null, "Net Salary is:" +Salary);
} else {
Tax = (monthlySalary*(13/100));
Salary = (monthlySalary-Tax);
JOptionPane.showMessageDialog(null, "Net Salary is:" +Salary);
} The tax is not detuting. please tell me the mistake
Edited by: syedejaz on Aug 13, 2008 2:49 PM -
Need Help With A Case Statement.
Apex 3.2
I currently have a report with the sql
select oid oid,
APEX_ITEM.MD5_CHECKSUM(oid, jobid, gdc, status)||
apex_item.hidden(2,oid)||
apex_item.display_and_save(3,jobid) jobid,
apex_item.display_and_save(4,gdc) gdc,
apex_item.display_and_save(5,bday) bday,
num_rows num_rows,
status current_status,
APEX_ITEM.SELECT_LIST_FROM_LOV(6,status,'WORKLOAD_STATUS_FULL_LIST',null,'YES',null,'-Select-') new_status,
case
when status in ('TO_TABLE','ERROR_TABLE') then 'PREPARED'
when status in ('TO_FILE','ERROR_FILE') then 'IN_TABLE'
when status like '%SEND%' and status <> 'SEND_RENAME_OK' then 'IN_FILE'
when trim(translate(status,'-1234567890',' ')) is null then to_char(jobid)
else status
end status_if_reset
from scp_workload
where jobid = :P28_JOBIDWhat I want to do is, if the result of status_if_reset column
is not in ('PREPARED','EMPTY_FILE','IN_FILE','SEND_RENAME_OK','DEPRICIATED')
then the new_status status column should be the APEX_ITEM.SELECT_LIST_FROM_LOV.
If not it should just be status (normal report column)
Hope I have explained properly
GusYour function had errors but maybe that was copy paste. The way you wrote your code is way to complicated. This should work for you:
CREATE OR REPLACE FUNCTION fn_28_get_status (p_status IN VARCHAR2)
RETURN NUMBER
AS
v_status NUMBER;
BEGIN
IF p_status IN
('TO_TABLE', 'ERROR_TABLE', 'TO_FILE', 'ERROR_FILE',
'SEND_RENAME_OK')
OR p_status LIKE '%SEND%'
THEN
v_status := 1;
ELSE
v_status := 0;
END IF;
RETURN v_status;
END fn_28_get_status;
SELECT OID OID,
apex_item.md5_checksum (OID, jobid, gdc, status)
|| apex_item.hidden (2, OID)
|| apex_item.display_and_save (3, jobid) jobid,
apex_item.display_and_save (4, gdc) gdc,
apex_item.display_and_save (5, bday) bday, num_rows num_rows,
status current_status,
CASE
WHEN fn_28_get_status (status) = 1
THEN apex_item.select_list_from_lov
(9,
status,
'WORKLOAD_STATUS_FULL_LIST',
NULL,
'YES',
NULL,
'-Select-'
ELSE status
END new_status
FROM scp_workload
WHERE jobid = :p28_jobidYou should not expect the others will debug you code. They don't have your objects to test.
Denes Kubicek
http://deneskubicek.blogspot.com/
http://www.apress.com/9781430235125
https://apex.oracle.com/pls/apex/f?p=31517:1
http://www.amazon.de/Oracle-APEX-XE-Praxis/dp/3826655494
------------------------------------------------------------------- -
Need help with this CASE statement
Hi everyone,
I would like to create a CASE that references another view and uses fields in that view to create a new field in a new view.
The fields I am working with are called LEVEL_CD and AVAILABLE_AMT.
In this case I would like to create a field called AVAILABLE_AMT (in my new view) that inserts the AVAILABLE_AMT from the old view into the new field only if the LEVEL_CD = 1. If the LEVEL_CD is anything else but 1 I would like to insert a 0 into my new
AVAILABLE AMOUNT field.
This is what I have so far and it doesn't seem to work:
CASE WHEN old_view.LEVEL_CD = 1 THEN old_view.AVAILABLE_AMT
WHEN old_view.LEVEL_CD <> 1 THEN 0 END AS AVAILABLE_AMT
This just gives me zeroes in every record. Can anybody spot what I am doing wrong?
Thanks!SELECT <columns>,CASE WHEN old_view.LEVEL_CD
=
1
THEN old_view.AVAILABLE_AMT
ELSE 0 END AS AVAILABLE_AMTFROM old_view.PK JOIN new_view.PK WHERE....PS.PK -Primary Key
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
case
when datepart(mm,[SDate]) between '9' and '11' and datepart(mm,[PDate] ) between '9' and '11'
Then 'Q1_'
when datepart(mm,[SDate]) between '12' and '02' and datepart(mm,[PDate] ) between '12' and '02'
Then 'Q2_'
when datepart(mm,[SDate]) between '03' and '05' and datepart(mm,[PDate] ) between '03' and '05'
Then 'Q3_'
when datepart(mm,[SDate]) between '06' and '08' and datepart(mm,[PDate] ) between '06' and '08'
Then 'Q4_'
else 'N/A' end as QTR
from DDA_2015_FSR
Datatype for SDate and Pdate
2014-09-01 00:00:00
I need help with above case statement, When I run the case statement I get 'N/A FSY_2015 ' When I add the following where clause to validate the results. In my table I do have records for all four quarters, Except Q1, all other rows I get 'N/A
FSY_2015 '
Am i missing something? Can someone help me here?
where
datepart(m,[SDate])
between
'12'
and
'02'
and
datepart(m,[PDate]
between
'12'
and
'02'
FMThere is no CASE statement in SQL; it is a CASE expression. Big difference.
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML.
The report period table gives a name to a range of dates that is common to the entire enterprise.
CREATE TABLE Something_Report_Periods
(something_report_name CHAR(10) NOT NULL PRIMARY KEY
CHECK (something_report_name LIKE <pattern>),
something_report_start_date DATE NOT NULL,
something_report_end_date DATE NOT NULL,
CONSTRAINT date_ordering
CHECK (something_report_start_date <= something_report_end_date),
etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantages are that it will sort with the ISO-8601
data format required by Standard SQL and it is language independent. The pattern for validation is '[12][0-9][0-9][0-9]-00-00' and '[12][0-9][0-9][0-9]-[01][0-9]-00'
--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 -
Hi,
Can anyone help with the case statement in the query below, is it possible to check the date using MIN in a case statement? The MIN date needs to be split by ID.
SELECT id,
name,
MIN(to_date(payment_date, 'YYYYMMDD')) first_date,
MAX(to_date(payment_date, 'YYYYMMDD')) last_date,
CAST(SUM(payment_amount) AS DOUBLE PRECISION) amount,
ABS(CAST(SUM(
CASE
WHEN payment_amount < 0 and to_date(payment_date, 'YYYYMMDD') > MIN(to_date(payment_date, 'YYYYMMDD')) THEN payment_amount
END) AS DOUBLE PRECISION)) credit
FROM test
group by id, name
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"
I am trying to acheive the following in the case statement:
CASE
WHEN payment_amount < 0 and payment_date > first_date THEN payment_amount
ENDHi,
sliderrules wrote:
Below is a test script:
create table test(
id number,
name varchar2(10),
payment_date date,Is payment_date a DATE or a string?
As the name implies <b>TO_</b>DATE converts a string TO a DATE. If payment_date is already a DATE, there's no need to convert it.
payment_amount number
insert into test values(001, 'HK', '20-SEP-10', 69.36);If payment_date is a DATE, don't try to insert a string (such as '20-SEP-10') into that column. Use TO_DATE here, not in your query.
insert into test values(001, 'HK', '23-SEP-10', 235);
insert into test values(001, 'HK', '23-SEP-10', 589.36);
insert into test values(001, 'HK', '14-OCT-10', -743);
insert into test values(001, 'HK', '18-OCT-10', -39.68);
insert into test values(002, 'DU', '18-JUL-11', -56.36);
insert into test values(002, 'DU', '23-SEP-11', 48.96);
insert into test values(002, 'DU', '14-NOV-11', -529.63);
insert into test values(002, 'DU', '14-FEB-12', -15.23);
insert into test values(002, 'DU', '14-FEB-12', 896.21);
insert into test values(002, 'DU', '18-APR-12', -55.14);
insert into test values(003, 'GS', '11-APR-10', 36.69);
insert into test values(003, 'GS', '23-SEP-10', 47.96);
insert into test values(003, 'GS', '14-FEB-11', 14.23);
insert into test values(003, 'GS', '14-JAN-12', -19.23);
insert into test values(003, 'GS', '16-FEB-12', -63.22);
insert into test values(003, 'GS', '18-JUL-12', -78.52);
Expected Results:
ID- 001
NAME- HK
FIRST_DATE- 20-Sep-10
LAST_DATE- 18-Oct-10
AMOUNT- 111.04
CREDIT AMOUNT- -782.68
ID- 002
NAME- DU
FIRST_DATE- 18-Jul-11
LAST_DATE- 18-Apr-12
AMOUNT- 288.81
CREDIT AMOUNT- -656.36Why do you want -656.36 above? Why not -600.00?
ID- 003
NAME- GS
FIRST_DATE- 11-Apr-10
LAST_DATE- 18-Jul-12
AMOUNT- -62.09
CREDIT AMOUNT- -160.97Do you really want the results unpivoted like that, or do you want them in a tabular format, like this?
ID NAME FIRST_DAT LAST_DATE AMOUNT CREDIT
1 HK 20-SEP-10 18-OCT-10 111.04 -782.68
2 DU 18-JUL-11 18-APR-12 288.81 -600
3 GS 11-APR-10 18-JUL-12 -62.09 -160.97If you want the unpivoted output, your Oracle version is even more important.
ABS always returns a positive number whether you pass it a positive or a negative number. If credit is to be less than 0, then don't use ABS.
SELECT id,
name,
MIN(to_date(payment_date, 'YYYYMMDD')) first_date,
MAX(to_date(payment_date, 'YYYYMMDD')) last_date,
CAST(SUM(payment_amount) AS DOUBLE PRECISION) amount,
ABS(CAST(SUM(
CASE
WHEN payment_amount < 0 and to_date(payment_date, 'YYYYMMDD') > MIN(to_date(payment_date, 'YYYYMMDD')) THEN payment_amount
END) AS DOUBLE PRECISION)) credit
FROM test
group by id, name
ORA-00937: not a single-group group function
00937. 00000 - "not a single-group group function"That looks like the same query you started with.
I am trying to acheive the following in the case statement:
CASE
WHEN payment_amount < 0 and payment_date > first_date THEN payment_amount
ENDThen compute first_date in a sub-query, as I showed earlier. -
Substitute Field Values Case Statement
Hey,
I need help with a CASE Statement to display certain field values based on another field value.
This is the data in table DEPT
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTONThis is my select statement.
SELECT DEPTNO, DNAME,
(CASE LOC
WHEN 'NEW YORK' THEN ''
--WHEN LOC = 'NEW YORK' THEN DEPTNO = ''
--WHEN LOC = 'NEW YORK' THEN DNAME = ''
END) AS LOC
FROM DEPTWhen I uncomment out the two lines I get an error message. The results are
DEPTNO DNAME LOC
10 ACCOUNTING -
20 RESEARCH -
30 SALES -
40 OPERATIONS -I do not want to clear all the values for LOC. I would like to display no values for DEPTNO and DNAME if LOC = 'NEW YORK'. Has anyone ever done this before?
Correct Results...
DEPTNO DNAME LOC
- - NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTONLike this?
SQL> ed
Wrote file afiedt.buf
1 SELECT CASE WHEN LOC != 'NEW YORK' THEN DEPTNO END AS DEPTNO
2 ,CASE WHEN LOC != 'NEW YORK' THEN DNAME END AS DEPTNO
3 ,LOC
4* FROM DEPT
SQL> /
DEPTNO DEPTNO LOC
NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
50 IT SUPPORT LONDON
SQL> -
Hello,
I need help implementing multiple Case Statements in a select statement. I can not get this to work in one statement.
Select Statement
select distinct
allt.fk_jobcode_id as Job_Code,
allt.fk_jobcode_descr as Classification,
sum(allt.number_of_positions) as Number_Allotted,
sum(number_of_positions) as Sum_Number_Allotted,
dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
THEN div.division_name
END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,
allt.fund_type as Fund
from tbl_allotment allt, tbl_department dept, tbl_division div
where isdeleted <> 'Y'
and ALLT.FK_DEPARTMENT_ID = dept.department_id
and ALLT.FK_DIVISION_ID = div.division_id
group by fk_jobcode_id, fk_jobcode_descr, fund_type, department_name, division_name
HAVING (count(fk_jobcode_descr ) = 1)
order by Classification asc, Department_Division asc1st CASE Statement combines the Department_Name and Division_Name values but does not display the Division_Name if the value is 'N/A'.
dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
THEN div.division_name
END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,value: Council Administrator's Office/Administration
value: Council Administrator's Office/N/A will display as Council Administrator's Office
I need to extend this now by using this CASE Statement, which displays the text 'Public Works' if the Department_Id = 00072.
(CASE dept.department_id
When '00072' then 'Public Works'
Else dept.Department_Name
End) as Department_Name, incorrect: Road Rehab Program/GLP Transportation Program Admin
Should populate as: Public Works/GLP Transportation Program Admin
select distinct
allt.fk_jobcode_id as Job_Code,
allt.fk_jobcode_descr as Classification,
sum(allt.number_of_positions) as Number_Allotted,
sum(number_of_positions) as Sum_Number_Allotted,
dept.department_name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
THEN div.division_name
END),'/'||DIV.DIVISION_NAME||'',null) as Department_Division,
(CASE dept.department_id
When '00072' then 'Public Works'
ELSE dept.Department_Name|| ''||nvl2((CASE WHEN div.division_name <> 'N/A'
THEN div.division_name
END),'/'||DIV.DIVISION_NAME||'',null)
END) as Department_Name1,
allt.fund_type as Fund
from tbl_allotment allt, tbl_department dept, tbl_division div
where isdeleted <> 'Y'
and ALLT.FK_DEPARTMENT_ID = dept.department_id
and ALLT.FK_DIVISION_ID = div.division_id
group by fk_jobcode_id, fk_jobcode_descr, fund_type, department_name, division_name, department_id
HAVING (count(fk_jobcode_descr ) = 1)
order by Classification asc, Department_Division ascbut once I modify the select statement I am only getting value 'Public Works' for Department_Division value. Does not populate the Division_Name data. Does anyone know how to combine these CASE Statements? Thanks for reading this Thread also.Hi,
I think you want something like this:
SELECT ...
, CASE
WHEN department_id = '00072'
THEN 'Public Works'
ELSE dept.department_name
END
||
CASE
WHEN div.division_name != 'N/A'
THEN '/' || div.division_name
END AS department_name1
...Whenever you have a problem, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) from all tables.
Also post the results you want from that data, and an explanation of how you get those results from that data, with specific examples.
Always say which version of Oracle you're using.
Maybe you are looking for
-
How can I print out specific categories on my outlook calendar
After downloading latest version of Icloud. I am not able to print out specific categories on icloud calendar
-
Lost my contacts, how to transfer from macbook pro to ipad
lost my contacts, how to transfer from macbook pro to ipad
-
Billing value is different in billing due list
Hello Experts , I have one question on contract billing plan. For a contract i have changed service data (i.e contract end date to 10th oct 2008 ) accordingly system generates billing plan item with settlement period (from 1st oct to 10th oct ). This
-
Correct way to export uncompressed to get a product aired on TV
Hello all, just finished editing a 50 minute doc that will be aired on Greek TV I edited with most of my footage beeing DV-pal 16:9 so I edited on a sequence with these kind of settings. I exported a "current settings" version and took it to an AVID
-
HT4759 will Icloud work with windows 8? thanks
will icloud work with windows 8 on my PC ? thanks glenn2222ahotmail.com