Group By with Case statement
Hello
I have a question I want to return a different group of list depending on a variable of common past in? It sounds easy, but I can't understand it >
Example 1 if the: P380_COURIER_TYPE = 'All' I want the group by being 'group by tour' - a single column
Example 2 If the: P380_COURIER_TYPE <>'All' I wish that the group by to "group by tour, exp_del_channel '--2 columns
Select
Tour as a 'Tour '.
Exp_del_channel as 'Courier Type' - it can be removed from the select statement because it is not necessary in the report.
, sum (pan_only) "Pan" only
Of
VW_VOLUME_PREADVICE_REPORT APV
where (: P380_COURIER_TYPE = 'All' or exp_del_channel =: P380_COURIER_TYPE)
Touring band, exp_del_channel
not tested
select Tour as "Tour" ,nvl(case when :P380_COURIER_TYPE<>'ALL' then exp_del_channel end,'ALL') as "Courier Type" -- this can be removed from the select statement as it is not required in the report. ,sum(pan_only) as "Pan Only" from VW_VOLUME_PREADVICE_REPORT vpa where (:P380_COURIER_TYPE ='All' or exp_del_channel =:P380_COURIER_TYPE) group by tour, case when :P380_COURIER_TYPE<>'ALL' then exp_del_channel end
Tags: Database
Similar Questions
-
Select with case statement and a formula of the IIR
Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.
[code]
Select *.
Of
(
Select name, month, duration, volume, time_spent, date1, date2,.
-case when 'date' > = 'date1' then '1' other '0' end as departure,.
-case when 'date' < = "date2" then '1' other '0' as end ending
Of
(
Select *.
of call_1 cd
inner join call_2 ON cd.name = cl.queue cl
)
)
;
[/ code]
I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.
[code]
departure: IIf ([date] > = [date1], 1, 0)
[/ code]
[code]
ending: IIf ([date] < = [date2], 1, 0)
[/ code]
Any advice would be appreciated.
Hello
Whenever you have any questions, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
Explain, using specific examples, how you get these results from these data.If the output depends on what anyone outside the application itself (for example, when it is run) and then include a few different examples and the results you want of each given the same sample data. For example, "if I run on November 19, 2013, while the results should be... because... but if I run between November 21 and November 27, then the results should be... because...". »
Always say what version of Oracle you are using (for example, 11.2.0.2.0).See the FAQ forum: https://forums.oracle.com/message/9362002
318f20b8-a3d0-4FB4-bb0f-73785250b7d4 wrote:
Hi, I was looking to get help regarding a statement writing box with a statement select statement all. I tried to reproduce a formula IIf access well that just wanted to check that the query I wrote is correct, any advice would be appreciated.
[code]
Select *.
Of
(
Select name, month, duration, volume, time_spent, date1, date2,.
-case when 'date' > = 'date1' then '1' other '0' end as departure,.
-case when "date."<= 'date2'="" then="" '1'="" else="" '0'="" end="" as="">=>
Of
(
Select *.
of call_1 cd
inner join call_2 ON cd.name = cl.queue cl
)
)
;
[/ code]
I want to know is where I have my ' select name, etc, I would change that to select * to make it easier instead of typing all the field_names outside, but I don't know how to do and also what follows is 2 IIF formulas from an access database for the start of the final case statements so I just wanted check I wrote it correctly.
[code]
departure: IIf([date]>=[date1],1,0)
[/ code]
[code]
ending: IIf ([date]<>
[/ code]
Any advice would be appreciated.
DATE is not a function of Oracle; in fact, it's a terrible name for a column or function, because it blends with the DATE data type.
In Oracle, the function SYSDATE returns the date and time, according to the clock on the database server. So, SYSDATE can return a value as November 19, 2013 06:33:15. If you want to midnight the same day (i.e., November 19, 2013 00:00:00) and then use TRUNC (SYSDATE).
String literals (for example the string which consists 5 characters d, a, t, e, and 1) go inside the single quotes. Numbers and the names of columns only.
If you want to include all the columns, more some calculated values, in a SELECT clause, then you must use a name table or alias before the *. (See select_2 below. To do this, assign the alias j to display online.)
Maybe you wanted to say something like:
Select *-it's select_1
de)
Select j. *-it's select_2
, case when SYSDATE > = date1 then 1 or 0 end as departure
, case when SYSDATE<= date2="" then="" 1="" else="" 0="" end="" as="">=>
de)
Select *-it's select_3
of call_1 cd
inner join call_2 ON cd.name = cl.queue cl
) j
)
;
As mentioned in a previous answer, you should be careful about the use of "SELECT *" in production code.
In select_3, it would be better if you explicitly listed the columns you need. It can improve efficiency and maintenance. In this request, queue and the name will be always the same, so you probably don't want to include both of them in the result set, in any case.
In select_1 and select_2, it is acceptable to use "SELECT *", assuming that select_3 is fixed.
Moreover, there is no point in using subqueries here. You can get the same results simply in a single query, without any subqueries.
-
I try to use a case statement in a report of the Apex, but I get an error message.
It comes to my sql
CASE WHEN EXISTS (select stg.extkey, distsubtrpgrpid
of udm_da da
udm_di di
udm_distsubtrpgrp stg
where di.disttransgrpid = stg.disttransgrpid
and da.daid = di.daid
and da.suid = su.suid)
THEN
Select stg.extkey
of udm_da da.
udm_di di,
udm_distsubtrpgrp stg
where di.disttransgrpid = stg.disttransgrpid
and da.daid = di.daid
and da.suid = ' | Qul SUID. '
Stg.extkey group, distsubtrpgrpid
Union
Select stg.extkey
of stg udm_distsubtrpgrp
where distsubtrpgrpid! = - 1
and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '
order by 1
WHEN su.distsubtrpgrpid IS NOT NULL
THEN
Select stg.extkey, distsubtrpgrpid
of stg udm_distsubtrpgrp
where distsubtrpgrpid! = - 1
and distsubtrpgrpid = ' | NVL (su.distsubtrpgrpid-1). '
order by 1
ON THE OTHER
NULL VALUE
END DISTSUBTRPGRP_1
Any help appreciated
Gus
GusC wrote:
Yes you are right.
My sql must be
CASE WHEN su.distsubtrpgrpid IS NOT NULL
THEN
Select stg.extkey, distsubtrpgrpid
of stg udm_distsubtrpgrp
where distsubtrpgrpid! = - 1
and distsubtrpgrpid = su.distsubtrpgrpid
ON THE OTHER
NULL VALUE
END DISTSUBTRPGRP_1
But the report won't save
Gus
No, your SQL is not that.
The select statement that you put online here, aside from being not not in brackets, is the selection of two values, but you can provide two values for the result column.
Without knowing who the SQL statement, we can highlight only the obvious questions.
-
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 -
WHERE conditional clause with CASE statement
Hi experts,
I have a table:
CREATE THE TABLE HR. TABLE_Y
(
NUMBER (2),
ID NUMBER (2),
NUMBER (5) CALC
)
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(1, 1, 10);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(1, 2, 15);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(1, 3, 17);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(1, 4, 20);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(2, 2, 11);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(1, 2, 14);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(2, 3, 18);
Insert into human resources. TABLE_Y
(TYPE, ID, CALC)
Values
(3, 1, 30);
COMMIT;
SQL > SELECT * FROM TABLE_Y ORDER BY 1.2;
TYPE ID CALC
---------- ---------- ----------
1 1 10
1 2 15
1 3 17
1 4 20
2 1 14
2 2 11
2 3 18
3 1 30
I have the query as below,
I pass a TYPE and an IDENTITY value as a condition for a query.
SELECT THE TYPE, ID, CALC
OF TABLE_Y
WHERE TYPE =: TYPE
AND IN THE CASE OF IDENTITY
WHEN: TYPE = 1
THEN 2
WHEN: TYPE = 2
THEN 1
End
;
When passing a TYPE = 1
Output
ID of TYPE CALC
---------- ---------- ----------
1 2 15
When passing a TYPE = 2
Output
ID of TYPE CALC
---------- ---------- ----------
2 1 14
I want to move a TYPE and ID more values as a condition for a query.
I'm trying
SELECT THE VALUE TYPE, ID,
OF TABLE_Y
WHERE TYPE =: TYPE
AND IN THE CASE OF IDENTITY
WHEN: TYPE = 1
THEN (1,3)
WHEN: TYPE = 2
THEN (1,2)
End
;
But gives me error:
ORA-00907: lack of right parenthesis
During the passage of a desired output TYPE, = 1
TYPE ID CALC
---------- ---------- ----------
1 1 10
1 3 17
During the passage of a desired output TYPE, = 2
TYPE ID CALC
---------- ---------- ----------
2 1 14
2 2 11
Thanks in advance
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0; Production
AMT for Solaris: release 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Hello
Here's one way:
SELECT the type, id, value
OF table_Y
WHERE type =: type
AND ((: type = 1))
AND id IN (1, 3)
)
OR (: type = 2)
AND id IN (1, 2)
)
)
;
-
How to use the Substring function with Case statement.
Hi all
I have a requirement where I have to use the substring function on the ground for the report criteria.
FOR EXAMPLE
I branch domain name where I have all the information of the branch names, now some of the branch names are too large with an extension after the name.
now I want substing it, but the length of characters varies for each branch.
so is it possible where we use a box where we can define if the character of name plug exceed a value then he substing with this length.Try something like this:
LENGTH WHEN CARTER (tablename. Branch_Name) > n THEN SUBSTRING (...) Of ANOTHER tablename. Branch_Name END
where n is the number of characters that you want to start the break.
-
NEED HELP IN THE GROUP BY CLAUSE CASES PLEASE
User1983440 thanks for the help with the script below grouped by product category, can you or someone else help me group by another Case statement as well.
WITH p AS)
Select distinct
partner, region, od. ORDER_AFFILIATE_NAME,
Oh.external_order_number,
Ain.get_g1 (queue_type) offers,
Case
When disp.category = 100 Then 'ORDER'
When disp.category = 200 Then 'ACCESS'
When disp.category = 300 Then 'DSL'
When disp.category = 600 Then 'IPTV'
"When disp.category = 700 Then'INTERNET"
When disp.category = 800 Then 'VOIP'
End up like product_category,
To_char(OH.created_ts, 'yyyy-mm-dd HH24:mi:ss') CREATE_DATE
-Case when ip.offer_type = "IPDSLAM" or instr (ip.display_name, "-d") > 0
THEN 'IP - LAN'
ELSE 'FTTN.
end as product_type
Case
When disp.state = 4 and disp.state = 9 or disp.state = 21 or 22 = disp.state then 'cancelled '.
When disp.state = 17 then "NRFC cancelled.
When disp.state = 7 Then 'incomplete '.
When disp.state = 3 or disp.state = 8, then "Finish".
When disp.state = 6 or disp.state = 10 and disp.state = 13 or disp.state = 14 then 'pending - other ".
When disp.state = 19 disp.state = 20 then "sent".
When disp.state = 21 or disp.state = 22 then "Cancelled - CSUS.
When disp.state = 23 or 24 = disp.state then 'Order Confirmation '.
When disp.state = 1 or disp.state = 2 or disp.state = 15 then "receipt".
Another 'other '.
End as STATE_desc
of AIN.impl_oh_order_header oh,.
Ain.impl_order_data od,
DISP AIN.sncr_order_curr_disp,
AIN.impl_package ip
where oh.created_ts between to_date ('2013-02-04', ' yyyy-mm-dd "")
and to_date ('2013-02-11', ' yyyy-mm-dd "")
and oh.order_type = 'COMMAND '.
and disp.category to (200, 300, 600, 700, 800)
and disp.state! = '5'
and uscs = 0
and disp.transaction_id = oh.transaction_id
and od. transaction_id = oh.transaction_id
and ip.transaction_id = oh.transaction_id
and ip.package_type = 'PACKAGE '.
and ip.parent_package_id is null
and ip.product_type = 'COMMAND '.
)
SELECT product_category,
COUNT (*)
P
GROUP BY product_categoryTo summarize, by category of product, and then by state_desc:
WITH p AS ( Select distinct partner, region, od.ORDER_AFFILIATE_NAME, oh.external_order_number, ain.get_g1(queue_type) offer, Case When disp.category = 100 Then'ORDER' When disp.category = 200 Then'ACCESS' When disp.category = 300 Then'DSL' When disp.category = 600 Then'IPTV' When disp.category = 700 Then'INTERNET' When disp.category = 800 Then'VOIP' End as product_category, TO_CHAR(oh.created_ts, 'yyyy-mm-dd hh24:mi:ss') CREATE_DATE, Case when ip.offer_type = 'IPDSLAM' or instr(ip.display_name, '- D')> 0 THEN 'IP-DSL' ELSE 'FTTN' end as product_type, Case When disp.state = 4 or disp.state = 9 or disp.state= 21 or disp.state= 22 Then 'Canceled' When disp.state = 17 Then 'Canceled NRFC' When disp.state = 7 Then 'Incomplete' When disp.state = 3 or disp.state = 8 Then 'Complete' When disp.state = 6 or disp.state = 10 or disp.state = 13 or disp.state =14 Then 'Pending - Other' When disp.state = 19 or disp.state = 20 Then 'Submitted' when disp.state = 21 or disp.state = 22 then 'Cancelled- CSUS' when disp.state =23 or disp.state = 24 then 'Order Confirmation' when disp.state =1 or disp.state = 2 or disp.state = 15 then 'Received' Else 'Other' End as STATE_desc from AIN.impl_oh_order_header oh, ain.impl_order_data od, AIN.sncr_order_curr_disp disp , AIN.impl_package ip where oh.created_ts between to_date('2013-02-04', 'yyyy-mm-dd') and to_date('2013-02-11', 'yyyy-mm-dd') and oh.order_type = 'ORDER' and disp.category in (200, 300, 600, 700, 800) and disp.state !='5' and uscs= 0 and disp.transaction_id = oh.transaction_id and od. transaction_id = oh.transaction_id and ip.transaction_id = oh.transaction_id and ip.package_type = 'PACKAGE' and ip.parent_package_id is null and ip.product_type = 'ORDER' ) SELECT product_category, STATE_desc, COUNT(*) FROM p GROUP BY product_category, STATE_desc
To summarize by state_desc, just remove "product category", the query above.
-
[8i] Case statement generates ORA-00932: inconsistent data types
Note: I work with a 8i database (Yes, he's old enough) and in this case, I have to deal with the types of data (e.g. TANK), I gives me to work with.
I am trying to calculate the amount of time product expected between manufacturing steps. As I found out though, sometimes I get a negative value then subtract the date the previous step ends the date of that ongoing stage begins. Since it is generally impossible to start a later stage before a previous step (imagine trying to screw a cap on a bottle that has no son Cup yet - it just may not happen), what I found is that sometimes two steps are started the same day and finished the same day (although not necessarily the day they started). This situation CAN occur when a person is two steps and connected to two steps at the same time, rather than log on to one, then the other. So what I need to do in these situations, is to replace the negative number by zero (I'll treat the stage as having no time-out).
Some examples of data:
(Note: the data set real is the result of a query and has about 200 K lines and columns more, but this should be representative enough to find a solution that works on my real application.)
Here's the query I use that sometimes returns negative values:CREATE TABLE steps ( item_id CHAR(25) , ord_nbr CHAR(10) , sub_nbr CHAR(3) , step_nbr CHAR(4) , start_date DATE , finish_date DATE ); INSERT INTO steps VALUES ('A','0000000001','001','0010',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('A','0000000001','001','0020',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('A','0000000001','001','0030',TO_DATE('01/05/2011','mm/dd/yyyy'),TO_DATE('01/06/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('A','0000000001','002','0010',TO_DATE('01/01/2011','mm/dd/yyyy'),TO_DATE('01/02/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('A','0000000001','002','0020',TO_DATE('01/04/2011','mm/dd/yyyy'),TO_DATE('01/04/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('A','0000000001','002','0030',TO_DATE('01/06/2011','mm/dd/yyyy'),TO_DATE('01/07/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('B','0000000002','001','0005',TO_DATE('01/10/2011','mm/dd/yyyy'),TO_DATE('01/12/2011','mm/dd/yyyy')); INSERT INTO steps VALUES ('B','0000000002','001','0025',TO_DATE('01/10/2011','mm/dd/yyyy'),TO_DATE('01/12/2011','mm/dd/yyyy'));
These are the results I want to see:SELECT item_id , ord_nbr , sub_nbr , step_nbr , start_date - last_step_finished FROM ( SELECT s.* , LAG (s.finish_date) OVER ( PARTITION BY s.item_id , s.ord_nbr , s.sub_nbr ORDER BY s.step_nbr ) AS last_step_finished FROM steps s ); Returns: ITEM_ID ORD_NBR SUB STEP START_DATE-LAST_STEP_FINISHED ------------------------- ---------- --- ---- ----------------------------- A 0000000001 001 0010 A 0000000001 001 0020 -1.000 A 0000000001 001 0030 3.000 A 0000000001 002 0010 A 0000000001 002 0020 2.000 A 0000000001 002 0030 2.000 B 0000000002 001 0005 B 0000000002 001 0025 -2.000
And that's what I tried to do to get these results (comment pointed out which line has generated the error):ITEM_ID ORD_NBR SUB STEP START_DATE-LAST_STEP_FINISHED ------------------------- ---------- --- ---- ----------------------------- A 0000000001 001 0010 A 0000000001 001 0020 0.000 A 0000000001 001 0030 3.000 A 0000000001 002 0010 A 0000000001 002 0020 2.000 A 0000000001 002 0030 2.000 B 0000000002 001 0005 B 0000000002 001 0025 0.000
I know that I had errors in data type incompatible before with case statements in this particular 8i database, but I can't seem to understand why I'm getting one this time. I think it has something to do with the NULL value which may occur for last_step_finished. Also, if I change the case statement to:SELECT item_id , ord_nbr , sub_nbr , step_nbr , CASE WHEN start_dt - last_step_finished < 0 THEN 0 ELSE start_dt - last_step_finished -- THIS LINE GENERATES THE ORA-00932 ERROR END AS days_in_queue FROM ( SELECT s.* , LAG (s.finish_date) OVER ( PARTITION BY s.item_id , s.ord_nbr , s.sub_nbr ORDER BY s.step_nbr ) AS last_step_finished FROM steps s );
the query works fine. But, I'm not NULL, I 0. In the level of this request, I'll take the averages by item_id/step_nbr, and I want that 0 s to be included in the average. (NULL values, as far as I know, would be excluded. AVG(null,_1,_2) = AVG (1,2) = 1.5 NOT AVG (0,1,2) = 1)., CASE WHEN start_dt - last_step_finished < 0 THEN NULL ELSE start_dt - last_step_finished -- THIS LINE GENERATES THE ORA-00932 ERROR END
Thanks in advance!CASE requires that all expressions to be of the same type. Numeric data type code is 2, then that date produced different internal datatype 14:
SQL> select dump(1) a,dump(sysdate) b,dump(sysdate - (sysdate - 1)) c from dual; A B C ------------------ ----------------------------------- ------------------------------ Typ=2 Len=2: 193,2 Typ=13 Len=8: 219,7,6,27,13,7,50,0 Typ=14 Len=8: 1,0,0,0,0,0,0,0 SQL>
Case is supposed to process until the number, but it does not--a bug in some versions. Use explicit to_number:
, CASE WHEN start_dt - last_step_finished < 0 THEN 0 ELSE to_number(start_dt - last_step_finished) END AS days_in_queue
SY.
-
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;
-
CASE with MIN statement?
I am trying to write a case statement that says what STAT is select E MIN (TERM)
If there are several TERMS that are both low just choose No. Want a line by ID.
Is this possible using case?
Thank you.
CREATE TABLE DAN_GR2 (ID VARCHAR2(12), TERM VARCHAR2(12), STAT VARCHAR2(12)) INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','5','E') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','2','D') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','9','E') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('1','5','E') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('2','1','E') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('2','10','E') INSERT INTO DAN_GR2 (ID, TERM,STAT) VALUES ('3,'10','D')
Here you areID TERM STAT 1 5 E 1 2 D 1 9 E 1 5 E 2 1 E 2 10 E 3 10 D
Use: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64biID MIN_TERM 1 5 2 1 3 NULL
Edited by: Chloe_19 09/12/2012 18:07Hello
So, you don't want to ignore the whole when line stat! = 'E' (that ignores the single row with id = "3"). Put the condition ' stat = 'E' in the sill of the WHERE clause ignores line, so this isn't what you want.
You want to just ignore the expression column. Put the same condition in a CASE expression that contols only this column.Here's one way:
SELECT id , MIN ( CASE WHEN stat = 'E' THEN term END ) AS min_e_term FROM dan_gr2 GROUP BY id ORDER BY id ;
-
I have problem with value NULL when the use CASE statement please help this question
I have problem with value NULL when the use CASE statement please help this question
Table: digital_val
SNO cl C2
1 San1 11
2 22 San2
Actual result: expected to A B
A B 11 22
11 NULL
22 NULL
query:
Select case when c1 = "san1" then c2,.
case If c1 = "san2" then c2 B
of digital_val
I'm more curious why, when you select 2 rows, you expect a result of row?
WITH digital_val
AS (SELECT 1 AS 'Sno', 'San1"C1, c2 FROM DUAL 11)
UNION ALL
2 SELECT AS 'Sno', 'San2"C1, c2 FROM DUAL 22)
SELECT CASE WHEN c1 is "San1" THEN END AS A c2.
CASE WHEN c1 = "San2" THEN END AS B c2
OF digital_val;
With no other input, if you select 2 rows, you get 2 rows. One of the other solutions use a max function, but is this really what you want, does not specify?
-
How can I get the nested case statements to work with listagg?
I have a statement below that does not work as I want it, here is my code:
Select
cases where o.personal_label is not null
then
listagg (cases where s.subcodevalue like '% MON %')
then s.subcodevalue |' '|| o.personal_label
of other s.subcodevalue
end,': ')
the Group (order of s.subcodevalue) as subcodevalue
on the other
listagg (s.subcodevalue,': ')
the Group (order of s.subcodevalue) as subcodevalue
end
of mtm_styles_new s, order_mtm o
where INSTR (o.extras, s.code) > 0
and o.bodyfitting = s.bodyfitting
and o.division = s.division
and s.subcodevalue is not null
and o.ORDER_MTMID = 'somevalue '.
It should bring the following
* MY TEST: RPP
or the following if the first case is not completed:
* MONDAY: RPP
I get an error message saying 'missing keyword', but cannot make out where, can someone please help me here?
The following works fine as long as it is not a nested case statement, but I need to nest the case statement:
Select listagg (cases where s.subcodevalue like '% MON %')
then s.subcodevalue |' '|| o.personal_label
of other s.subcodevalue
end,': ')
the Group (order of s.subcodevalue) as subcodevalue
of mtm_styles_new s, order_mtm o
where INSTR (o.extras, s.code) > 0
and o.bodyfitting = s.bodyfitting
and o.division = s.division
and s.subcodevalue is not null
and o.ORDER_MTMID = 'somevalue ';
Out alias 'subcodevalue as' the case at the end of the case body.
-
Hi all.
I use a case statement to represent the values for different regions in a command prompt. I'm having some difficulties, with the values in the case statement. Here is an example of a simplified and shortened my code version.
CASE
WHEN 'Region '. "" Region name "="A"THEN"AMERICA ".
WHEN 'Region '. "" Region name "= 'B' THEN 'CHINA '.
WHEN 'Region '. "" Region name "= 'C' THEN 'EUROPE '.
WHEN 'Region '. "' Name of the region ' = 'A' AND 'B' AND 'C' THEN 'GLOBAL '.
END
My error when I use it in my statement. The formula is accepted, however, all the values are displayed in my guest except 'GLOBAL '.
How can I be able to assign a "GLOBAL"value in my guest? ".
Jagadekara I think that you do not get the point. Any given row retrieved from a data source cannot have all three at one point values!
AJ was trying to tell you, is that your CASE logic has no sense at all.
-
Case statement with a select &; date in
Hello everyone,
This is my first discussion, so please bare with me.
I am trying to write a case statement with a select statement that compares the dates.
When I write an instruction box with a select statement in it compares everything but dates it works fine. Once the date is there, he collapses.
Here is a super simple example of what I'm trying to do, "b.in_serv_dt" is a date
Select unit_no,
case when ((sélectionnez b.in_serv_dt de b unit_dept_comp_main où a.unit_id = b.unit_id et b.in_serv_dt) > = 1 January 2012 "") then "everybody wins."
When ((sélectionnez b.in_serv_dt dans l'unit_dept_comp_main b où a.unit_id = b.unit_id et b.in_serv_dt) < = 1 January 2012 "") then "Nobody wins".
end
of ottawa_unitmain_v one
Any help would be greatly appreciated
Thank you very much
Hello
your statement of formatting:
Select unit_no
case when ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
and b.in_serv_dt
") > = JANUARY 1, 2012"
) and then "everybody wins."
When ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
and b.in_serv_dt
)<=>=>
) and then "no winner".
end
of ottawa_unitmain_v oneI see "partial conditions": <... and="" b.in_serv_dt="">> that have no meaning.
I guess it's a kind of "typo" and I ignore them.
If the statement is:
Select unit_no
case when ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
") > = JANUARY 1, 2012"
) and then "everybody wins."
When ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
)<=>=>
) and then "no winner".
end
of ottawa_unitmain_v oneI guess that the "b select" retrieve one line, through the design of database (forced...)
As already written: be careful with data types: avoid "implicit conversions" (when you compare different data types, Oracle has convert the data type of the other side, a side example: "mystring = mynumber" is transformed into 'TO_NUMBER (mystring) = mynumber', or 'mydate = mystring' translates mydate = TO_DATE (mystring,
) ")
Even better: use explicit conversions, use explicit formats when necessary.
Best: use no conversion on columns from the constants, but use the correct data type for the constant.With this in mind:
Select unit_no
case when ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
) > DATE = ' 2012-01-01'
) and then "everybody wins."
When ((select b.in_serv_dt
of unit_dept_comp_main b
where a.unit_id = b.unit_id
)<= date="">=>
) and then "no winner".
end
of ottawa_unitmain_v one
But note that b.in_serv_dt EQUAL to 1 January 2012 (at 00:00:00) is OK for both WHEN the conditions; then the a "win."Now: the subselect statement is identical in the 2 options of the case... You could work differently: (I add the "ELSE" in case one line as NULL in_serv_dt;) I guess that an INNER JOIN is OK, maybe you need a LEFT OUTER JOIN (if some lines of A have no corresponding row in B)
SELECT a.unit_no
, CASE WHEN b.in_serv_dt > = DATE '' 2012-01-01
THEN "everyone wins"
WHEN b.in_serv_dt< date="">
While "Nobody wins".
ELSE ' who knows... ". »
END who_wins
Of ottawa_unitmain_v one
INNER JOIN unit_dept_comp_main b
ON a.unit_id = b.unit_id
;Best regards
Bruno Vroman
-
error with the case statement use what void const query
Hi all.
I have found the error in the result set, and then use subqueries and cases of declaration.
Preparation for example.
2 examples of tables creation.
This statement returns NULL in subqueryCREATE TABLE T1 AS ( SELECT 1 ID , 1 F1 FROM DUAL UNION SELECT 2 ID , 1 F1 FROM DUAL UNION SELECT 3 ID , 1 F1 FROM DUAL ) ; CREATE TABLE T2 AS ( SELECT 2 ID FROM DUAL UNION SELECT 3 ID FROM DUAL ) ;
But this statement (with operator CASE) does NOT return null in subquerySELECT T1.ID, TT2.ID , TT2.ISBUG AS NO_BUG FROM T1 LEFT JOIN ( SELECT ID , -1 ISBUG FROM T2 )TT2 ON T1.ID = TT2.ID WHERE TT2.ID IS NULL
Why?SELECT CASE WHEN 1 = 1 THEN 1 END AS C1, T1.ID, TT2.ID , TT2.ISBUG FROM T1 LEFT JOIN ( SELECT ID , -1 ISBUG FROM T2 )TT2 ON T1.ID = TT2.ID WHERE TT2.ID IS NULL
Oracle 11.2.0.2.0
Published by: 927438 on 13.04.2012 03:27
Published by: 927438 on 13.04.2012 03:31Maybe it is related to this one
outer join of 11g with case - strange results statement.
seems to be a bug
Maybe you are looking for
-
First of all, let me explain my problem completely. I have an Acer Aspire XC - 603 G Desktop running 64-bit Windows 10 Home. It came with Windows 8.1 initially. When I first bought this PC, it has immediately had a sleep problem. He would not go in s
-
Webcam stopped working says insert USB after that reinstalling but being not recognized help pleae
I have a new computer, webcam stopped working, I reinstalled the software screen indicates it has been reinstalled ok but when I try to use the camera it says on screen no device connected to the usb. help please.
-
Back to the top took a lot of time
Hello I tried to start my mobile z2 through pc sony champion, but it took more than 12 hours and the end was not so please suggestions for this problem. Mobile on the latest version of sw
-
Envy 15 J003-tx: Webcam does not work properly
My laptop webcam does not work correctly. It worked correctly and comfortably at the time where I bought the laptop recently. But now, the hd quality missed thanks to a certain period. I have tried several methods until this. I thought the cyberlink
-
Using the IR camera to control C40
I have a C40 which is installed in a place which is not line of site with the room. Is there a way to allow the IR on the camera to move from there to the unit for control? I currently have an HDMI cable connecting the camera to the unit. Thank you J