Decode help in sql
i m writing an sql query to print records
select role_name,date_created,date_modified,createdbyuser_id,modifiedbyuser_id from t_role;
This returns me the the rows in table t_role.
But what i want is instead of the id of the person being printed in createdbyuser_id and modifiedbyuser_id, it shud provide me the username from another table user by checking the user_id field of table user.For this i used the fuction decode .But it oesnt work
select role_name,date_created,date_modified,(select user_name from user where user_id=createdbyuser_id),(select user_name from user where user_id=modifiededbyuser_id) from role;
kindly point out the mistake.
Shefali
<<
i m writing an sql query to print records
>>
and
<<
1) tried this
select a.role_name,a.date_created, a.date_modified, b.user_name, c.user_name from role a, user b, user c where b.user_Id = a.createdbyuser_Id and c.user_Id = a.modifiedbyuser_Id;
But getting this error::
ORA-20001: Your query contains duplicate alias names, queries used for reports require unique alias names.
Error saving column settings
>>
so you are working with report writer ?
the error message is telling you all you need.
just change :
select a.role_name ,a.date_created, a.date_modified
, b.user_name, c.user_name
from role a, user b, user c
where b.user_Id = a.createdbyuser_Id
and c.user_Id = a.modifiedbyuser_Id
;to
select a.role_name ,a.date_created, a.date_modified
, b.user_name_creator, c.user_name_modifier
from role a, user b, user c
where b.user_Id = a.createdbyuser_Id
and c.user_Id = a.modifiedbyuser_Id
;
Similar Messages
-
Hi,
I'm writing an HTMLDB application that we will be using to track the number of hours that we put into a project (all projects), and I've got a routine that returns the number of hours that are possible within a given month M-F 8 hours a day. I use this value returned to indicate how many hours have been enterered.
SELECT null link, 'Still Unrecorded' itsrdescr,trueworkdays(:P80_START,:P80_END)*8-(select sum(hoursworked) ihoursworked from workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END) hoursworked
from dual
UNION
SELECT null link, wd.itsrdescr,sum(b.hoursworked) hoursworked
FROM workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END
GROUP by wd.itsrdescr
ORDER by hoursworked desc
What I'm wanting is something along the lines of this:
SELECT null link, 'Still Unrecorded' itsrdescr,decode((trueworkdays(:P80_START,:P80_END)*8-(select sum(hoursworked) ihoursworked from workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END)),<0,0,?????????) hoursworked
from dual
UNION
SELECT null link, wd.itsrdescr,sum(b.hoursworked) hoursworked
FROM workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END
GROUP by wd.itsrdescr
ORDER by hoursworked desc
which will include a decode statement to let me know when the value getting returned into hoursworked on that first column is less than zero which throws off the chart. In this particular case, it has a value of -6 which makes all my percentages go haywire. How would one code this statement to include a decode statement to find out if that sum is returning a negative number?
Hope this made sense,
TIA,
cliffHey Tyler,
Actually.... It kinda killed the server. I'm trying to figure out a way to get the following functionality in a different way. Here is the way that the query turned out using the case statement.
SELECT null link, '<b><font color="red">Still Unrecorded</font></b>' itsrdescr,
(case
when (trueworkdays(:P80_START,:P80_END)*8-(select sum(hoursworked) ihoursworked from workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END)) < 0 then 0
else
(trueworkdays(:P80_START,:P80_END)*8-(select sum(hoursworked) ihoursworked from workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END))
end) hoursworked
from dual
UNION
SELECT null link, wd.itsrdescr,sum(b.hoursworked) hoursworked
FROM workdetail wd, breakdowns b
WHERE wd.workdetailid = b.itsrnum and b.oid = upper(:P80_DEVELOPER) and dateworked between :P80_START and :P80_END
GROUP by wd.itsrdescr
ORDER by hoursworked desc
Any tips on what I could do to get the above functionality without killing my server.
Thx for the input
cliff -
Need help in SQL (DENSE_RANK) function
Hello All,
I need the help in SQL.
We have a table called status and the column are
status_id number
account_id number
status_cd varchar2(10)
created_id varchar2(10)
created_by date
and data is as follows
insert into status values (1,101,'ENTER','ABC',to_date('21-JAN-2007 11:15:14','DD-MON-YYYY HH:MI:SS'));
insert into status values (2,101,'REVIEW','DEF',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (3,101,'APPROVE','GHI',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (4,102,'ENTER','ABC',to_date('21-JAN-2007 11:18:14','DD-MON-YYYY HH:MI:SS'));
insert into status values (5,102,'REVIEW','DEF',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (6,102,'CANCEL','GHI',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (7,103,'ENTER','ABC',to_date('21-JAN-2007 11:21:14','DD-MON-YYYY HH:MI:SS'));We have different status as follows
1. ENTER
2. REVIEW
3. APPROVE
4. CANCEL
5. REJECT
My requirement ..
I need the max of created_id column for the status in ('APPROVE','CANCEL') and if there is no status in ('APPROVE','REVIEW') than it should be NULL.
I wrote an SQL as
select account_id,max(created_id) keep (dense_rank first order by decode(status_cd,'APPROVE',created_dt,'REVIEW',created_dt,NULL) DESC NULLS LAST,
decode(status_cd,'APPROVE',status_id,'REVIEW',status_id,NULL) DESC NULLS LAST) last_app_rev_user
from status
group by account_id and gives me the output like
ACCOUNT_ID LAST_APP_R
101 GHI
102 DEF
103 ABCBut I want the Output like
ACCOUNT_ID LAST_APP_R
101 GHI
102 DEF
103 NULLAs the account 103 has no status called 'REVIEW' and 'APPROVE'
My DB Version in 10.2.0.3.0.
Hope I explain it properly. And if you have any other option without dense_rank still i will be happy.
Thanks in advance for your help.
AB
null
Message was edited by:
ABinstead of max(created_id) keep... use
smth like max(case when status_cd in ('APPROVE','REVIEW') then created_id end) keep... -
Is there any context-sensitive help in SQL Developer. I'm looking for a way to link SQL syntax with some sort of a Help function. Searching the SQL Reference pdf just doesn't cut it. For example, when I search the SQL Reference pdf for Round, I get 134 hits. I would look for a page or 2 on how to use Round, with examples, but accessible directly from SQL Developer.
No there isn't.
There's a rejected feature request for this at the Exchange (http://htmldb.oracle.com/pls/otn/f?p=42626:39:947182819474243::NO::P39_ID:5662), so chances are very small it ever makes it in.
Nevertheless, you can still vote and add comments there; if there's enough weight on an item it will eventually make it for the TODO list.
Including the doc isn't necessary, but just linking to the correct page would save a lot of time. In the case of ROUND, on pressing F1 inside the code editor or worksheet, sqldev could link directly to the SQL reference (http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14200/functions135.htm).
If the team doesn't want to put it in, maybe someone else does. It would make a nice extension... if you feel up for it?
K. -
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De. -
Accessing online help in Sql developer
For some reason I have lost the ability to access Help in Sql Developer. Nothing happenx when I click on the "Table of Contents" in the Help menu. I un and re installed Sql Developer, but thie problem remains.
My OS is vista 32 bit.
Thanks
MewbieThere's a +\sqldeveloper\system1.5.1.54.40\o.ide.11.1.1.0.22.49.42\windowinglayout.xml+ under your user profile. Deleting that should revert illegal positions (as it may have got pushed off screen).
You can also delete the whole +\sqldeveloper+ folder (or selective subfolders) to reset everything to factory defaults...
Hope that helps,
K. -
Can u give an detail examples or help on SQL- loader
Can u give an detail examples or help on SQL- loader
I need full details on this how to run and what are the pre requirements for this and what
Can u give an example with unix+sql loaderThe Oracle Utilities manual has a chapter on SQL*Loader Case Studies
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96652/ch10.htm#1656
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
I have the following 3 tables:
Table A
Id (Number)
Id#2 (Number)
Type (Number)
Table B
Id#2 (Number)
Decription (Varchar2)
Table C
Id#2 (Number)
Decription (Varchar2)
I want to write a select statement that would return:
Id from Table A,
Description (If type in Table A = 1 then get description from Table B, if type in Table A= 2 then get description from Table C)
Can anyone please help?Well, one of the things I've learned is "It always depends, you always need to test the defferent scenario's.."
But still I have to agree on this one with you, see the results I got:
SQL> select banner from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - Production
Elapsed: 00:00:00.03
SQL> set autotrace traceonly
SQL> drop table a;
Table dropped.
Elapsed: 00:00:00.37
SQL> drop table b;
Table dropped.
Elapsed: 00:00:00.34
SQL> drop table c;
Table dropped.
Elapsed: 00:00:00.32
SQL> create table a as
2 select level id,
3 level id2,
4 case when rownum <= 500000 then 1 else 2 end type
5 from dual
6 connect by level <= 100000;
Table created.
Elapsed: 00:00:08.18
SQL> create table b as
2 select level id2, 'description table b' description
3 from dual
4 connect by level <= 50000;
Table created.
Elapsed: 00:00:01.06
SQL> create table c as
2 select 50000+level id2, 'description table c' description
3 from dual
4 connect by level <= 50000;
Table created.
Elapsed: 00:00:01.67
SQL> alter table a modify id primary key;
Table altered.
Elapsed: 00:00:01.06
SQL> alter table b modify id2 primary key;
Table altered.
Elapsed: 00:00:00.37
SQL> alter table c modify id2 primary key;
Table altered.
Elapsed: 00:00:00.37
SQL> begin
2 dbms_stats.gather_table_stats(user, 'A');
3 dbms_stats.gather_table_stats(user, 'B');
4 dbms_stats.gather_table_stats(user, 'C');
5 end;
6 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.68
SQL> SELECT a.id,
2 CASE
3 WHEN a.TYPE = 1 THEN
4 b.description
5 WHEN a.TYPE = 2 THEN
6 c.description
7 END description
8 FROM a,
9 b,
10 c
11 WHERE a.id2 = b.id2(+)
12 AND a.id2 = c.id2(+);
100000 rows selected.
Elapsed: 00:00:11.06
Execution Plan
Plan hash value: 1054766904
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 100K| 5859K| | 676 (1)| 00:00:09 |
|* 1 | HASH JOIN RIGHT OUTER | | 100K| 5859K| 1728K| 676 (1)| 00:00:09 |
| 2 | TABLE ACCESS FULL | C | 49082 | 1150K| | 50 (0)| 00:00:01 |
|* 3 | HASH JOIN RIGHT OUTER| | 100K| 3515K| 1800K| 313 (1)| 00:00:04 |
| 4 | TABLE ACCESS FULL | B | 51064 | 1196K| | 50 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | A | 100K| 1171K| | 60 (2)| 00:00:01 |
Predicate Information (identified by operation id):
1 - access("A"."ID2"="C"."ID2"(+))
3 - access("A"."ID2"="B"."ID2"(+))
Statistics
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
0 bytes sent via SQL*Net to client
0 bytes received via SQL*Net from client
0 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100000 rows processed
SQL> select a.id,
2 case
3 when a.type = 1 then
4 (select description from b b where b.id2 = a.id2)
5 when a.type = 2 then
6 (select description from c c where c.id2 = a.id2)
7 end description
8 from a a;
100000 rows selected.
Elapsed: 00:00:11.73
Execution Plan
Plan hash value: 3680219842
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 100K| 1171K| 60 (2)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID | B | 1 | 24 | 1 (0)| 00:00:01 |
|* 2 | INDEX UNIQUE SCAN | SYS_C0022091 | 1 | | 1 (0)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID| C | 1 | 24 | 1 (0)| 00:00:01 |
|* 4 | INDEX UNIQUE SCAN | SYS_C0022092 | 1 | | 1 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL | A | 100K| 1171K| 60 (2)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("B"."ID2"=:B1)
4 - access("C"."ID2"=:B1)
Statistics
0 recursive calls
0 db block gets
0 consistent gets
0 physical reads
0 redo size
0 bytes sent via SQL*Net to client
0 bytes received via SQL*Net from client
0 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
100000 rows processedMy testcase differs from yours, since I added Primary Keys to the tables.
I thought that to be more realistic, I hope you'll agree on that.
That way the plan and time for the second query changes, ofcourse ;-) .
It would be nice to know from Dausboy if our testcases were more or less representative for his situation anyway. -
I need to know if there is any way to use a range of values from
database and decode to certain text. I am able to do with one
value.
for example:
DECODE(column_name,'216767111','Unlimited',column_name)
above argument works with one value only. How about a range,
ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
Kinldly need some body's help
Thanks
MunisWhich version of the database? If it's 8i+ then you can use
the CASE function
for example:
(CASE WHEN column_name between 216767000 and 216767111
THEN 'Unlimited' ELSE column_name END)
This won't work in PL/SQL because they're introducing a CASE
statement does soemthing different.
rgds, APCHello Andrew
Thank you for response. I am using 8i. 8.1.6. However using
CASE, I get inconsistent data type, ORA-00932: inconsistent
datatypes. I able to work it out with other response using
DECODE(sign(. Do you have any idea why i am getting this error.
If time permits, let me know -
NEED HELP IN SQL HOMEWORK PROBLEMS
I NEED HELP IN MY SQL HOMEWORK PROBLEMS....
I CAN SEND IT VIA EMAIL ATTACHMENT IN MSWORD....Try this:
SELECT SUBSTR( TN,
DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1) + 1),
DECODE( INSTR(TN, '#', 1, LEVEL) , 0 ,
LENGTH(TN) + 1, INSTR(TN, '#', 1, LEVEL) )
- DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1 ) + 1)
) xxx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XXX
234123
1254343
909823
908232
12345
SELECT regexp_substr(tn, '[^#]+', 1, level) xx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XX
234123
1254343
909823
908232
12345 -
Hi all
I read that in order to use the DECODE function in PL/SQL block you will have to use the system table dual.
my query is why we cannot use user table like emp while using decode function???thois might give some help,
in general use dual, if the function used in the select list is independent of any data of a particular table, like emp in your case, check this out,
select decode('',null,'is null','is not null') from dual
will return is null
but
select decode('',null,'is null',' is not null') from emp
will work and will give the same result but will give many records as of emp table, so either a distinct can be used or row_num = 1 can be used, but the whole point to to get things done with little cost, so better use dual for such tables...
answering this query, i have got few doubts, decode() is a fuunction which does the selection operation, but
1) how to implement a custom function with variable number of parameters
2) as decode is a function which returns one of the input parameter on execution, why can't we call
x := decode('',null,'is null','is not null');
kindly reply to the above two queries???
cheere -
Hi
I am executing a query:
select vetno,category from vetno where vetno.category IN (select decode (REP_VET_CATG,NULL,'('A','B')','REP_VET_CATG') from dual);
The REP_VET_CATG variable takes the value from the GUI.
I want the functionality such that if it is NULL then the query should be
select vetno,category from vetno where vetno.category IN ('A','B');
else
Take the values that is passed i.e the query should be like this
select vetno,category from vetno where vetno.category = 'REP_VET_CATG';
I tried the above query but it gives me a error missing right paranthesis.
Can anyone help ......with this OR suggest a better alternative..Do you mean this?
SQL> create table vetno
2 as
3 select 1 vetno, 'A' category from dual union all
4 select 2, 'B' from dual union all
5 select 3, 'C' from dual
6 /
Tabel is aangemaakt.
SQL> var REP_VET_CATG varchar2(1)
SQL> exec :REP_VET_CATG := null
PL/SQL-procedure is geslaagd.
SQL> select vetno
2 , category
3 from vetno
4 where ( :REP_VET_CATG is null
5 and vetno.category in ('A','B')
6 )
7 or ( :REP_VET_CATG is not null
8 and vetno.category = :REP_VET_CATG
9 )
10 /
VETNO C
1 A
2 B
2 rijen zijn geselecteerd.
SQL> exec :REP_VET_CATG := 'C'
PL/SQL-procedure is geslaagd.
SQL> select vetno
2 , category
3 from vetno
4 where ( :REP_VET_CATG is null
5 and vetno.category in ('A','B')
6 )
7 or ( :REP_VET_CATG is not null
8 and vetno.category = :REP_VET_CATG
9 )
10 /
VETNO C
3 C
1 rij is geselecteerd.Regards,
Rob. -
Please help with SQL amount calulation
-- Results
with t as (
select 'P11877' Mstr_Program, 1 Year_of_study, 'BUSI1490' program_module, 20 no_of_stud, 1 rank, 30 program_credits, 30 cumm_credits from dual union all
select 'P11877', 1, 'COMP1365', 20, 2, 30, 60 from dual union all
select 'P11877', 1, 'BUSI1375', 20, 3, 30, 90 from dual union all
select 'P11877', 1, 'COMP1363', 20, 4, 30, 120 from dual union all
select 'P11877', 2, 'MARK1174', 8, 1, 30, 30 from dual union all
select 'P11877', 2, 'FINA1068', 8, 2, 15, 45 from dual union all
select 'P11877', 2, 'INDU1062', 8, 3, 30, 75 from dual union all
select 'P11877', 2, 'BUSI1329', 8, 4, 15, 90 from dual union all
select 'P11877', 2, 'MARK1138', 8, 5, 30, 120 from dual)
select * from t;-- Each MSTR_PROGRAM can have 1 or many program_module
-- MSTR_PROGRAM's can run for 1 or 2 years (case above is two years) so some modules run in year 1 and some in year 2
-- NO_OF_STUD is the number of students on the module
-- RANK basically ranks the modules by the number of students on them grouped by program and year
-- e.g.row_number() OVER (PARTITION BY Mstr_Program, Year_of_study) ORDER BY COUNT(STUDENT_ID) DESC) rank
-- PROGRAM_CREDITS: each module has a fixed number of credits
-- CUMM_CREDITS: Increments the credit count of modules
-- SUM(program_credits * 10) OVER (PARTITION BY Mstr_Program, Year_of_study
-- ORDER BY count(STUDENT_ID) desc ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumm_credits
-- I want to trim of any modules once the CUM_CREDITS hits 120. As seen above. I achieve this by wrapping the main query is another SELECT then LIMIT
-- that WHERE cum_credit <=120.
-- But what I need is:
-- In some cases the the cumm_credit maybe on lets say 90credits then the next module is worth 40 credits. This next module will not show as it
-- will be greater than 120 credits, so i need to pro-rata it:
-- So if credit_count > 120, then the last module is counted pro-rata as follows: 1- ((credit count - 120) / credits from last module
-- Can anyone help with how I can incorporate this into my current code: The SELECT portion of the Original SQL is below: I simplified column names
-- e.t.c in the above so they wont be the same
SELECT * FROM (
SELECT
,SR_PROGRAM Mstr_Program
,DECODE (SORLCUR_YEAR, 1, 1,
2, 2,
3, 3,
4, 3, SR_YEAR) year_of_study
,SCT_SUBJ_CODE||SCT_CRSE_NUMB program_module
,COUNT(student_ID) no_of_stud
,row_number() OVER (PARTITION BY sr_program,
DECODE (sr_year, 1, 1,
2, 2,
3, 3,
4, 3, SR_YEAR) ORDER BY COUNT(student_id) DESC, scbcrse_title asc) rank
,(SCT_CREDIT_HRS * 10) program_credits
,SUM(SCT_CREDIT_HRS * 10) OVER (PARTITION BY sr_program, DECODE (sorlcur_year, 1, 1,
2, 2,
3, 3,
4, 3, SR_YEAR)
ORDER BY count(student_id) desc ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumm_credits
WHERE cumm_credit <=120
ORDER BY Mstr_Program, YEAR_OF_STUDY, RANK asc;Maybe
SELECT Mstr_Program,year_of_study,program_module,no_of_stud,rank,program_credits old_program_credits,cumm_credits old_cumm_credits,
case when cumm_credits > 120
then program_credits - cumm_credits + 120
else program_credits
end new_program_credits,
case when cumm_credits > 120
then 120
else cumm_credits
end new_cumm_credits
FROM (SELECT SR_PROGRAM Mstr_Program,
DECODE(SORLCUR_YEAR,1,1,2,2,3,3,4,3,SR_YEAR) year_of_study,
SCT_SUBJ_CODE||SCT_CRSE_NUMB program_module,
COUNT(student_ID) no_of_stud,
row_number() OVER (PARTITION BY sr_program,DECODE(sr_year,1,1,2,2,3,3,4,3,SR_YEAR)
ORDER BY COUNT(student_id) DESC,scbcrse_title) rank,
10 * SCT_CREDIT_HRS program_credits,
10 * SUM(SCT_CREDIT_HRS) OVER (PARTITION BY sr_program,DECODE(sorlcur_year,1,1,2,2,3,3,4,3,SR_YEAR)
ORDER BY count(student_id) desc
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumm_credits
WHERE 0 <= case when cumm_credits > 120
then program_credits - cumm_credits + 120
else program_credits
end
ORDER BY Mstr_Program,YEAR_OF_STUDY,RANKRegards
Etbin
Edited by: Etbin on 16.12.2011 8:50
with
t as /* simulating the result achieved */
(select 'P11877' Mstr_Program,1 Year_of_study, 'BUSI1490' program_module,20 no_of_stud,1 rank,30 program_credits,30 cumm_credits from dual union all
select 'P11877', 1, 'COMP1365', 20, 2, 40, 70 from dual union all
select 'P11877', 1, 'BUSI1375', 20, 3, 30, 100 from dual union all
select 'P11877', 1, 'COMP1363', 20, 4, 40, 140 from dual union all
select 'P11877', 2, 'MARK1174', 8, 1, 30, 30 from dual union all
select 'P11877', 2, 'FINA1068', 8, 2, 50, 80 from dual union all
select 'P11877', 2, 'INDU1062', 8, 3, 30, 110 from dual union all
select 'P11877', 2, 'BUSI1329', 8, 4, 50, 160 from dual union all
select 'P11877', 2, 'MARK1138', 8, 5, 30, 190 from dual
select Mstr_Program,Year_of_study,program_module,no_of_stud,rank,program_credits old_credits,cumm_credits old_cumm,
case when cumm_credits > 120
then program_credits - cumm_credits + 120
else program_credits
end new_program_credits,
case when cumm_credits > 120
then 120
else cumm_credits
end new_cumm_credits
from t
where 0 <= case when cumm_credits > 120
then program_credits - cumm_credits + 120
else program_credits
end -
Hi All,
I need help to get a sql query for included dates
I have this scenario
create table test (names varchar2(100),start_date date,end_date date,exc_flg number);
insert into test values('John',to_date('01-jan-2009','dd-mon-yyyy'),to_date('31-dec-2011','dd-mon-yyyy'),0);
insert into test values('John',to_date('01-jan-2009','dd-mon-yyyy'),to_date('31-dec-2010','dd-mon-yyyy'),1);
insert into test values('Joseph',to_date('01-jan-2009','dd-mon-yyyy'),to_date('31-dec-2011','dd-mon-yyyy'),0);
insert into test values('Jack',to_date('01-jan-2009','dd-mon-yyyy'),to_date('31-dec-2010','dd-mon-yyyy'),0);
In the above case john is included for this duration start_date is '01-jan-2009' and end_date is '31-dec-2011' and is excluded for this duration '01-jan-2009','31-dec-2010'
Now i want to dispay John's incuded duration as ''01-jan-2011','31-dec-2011'
My sql output should be
Names start_date end_date
John 01-jan-2011 31-dec-2011
Joseph 01-jan-2009 31-dec-2011
Jack 01-jan-2009 31-dec-2010
Thanks in AdvanceHi there,
seems like the old "find those time gaps" problem. I tried to solve it in pure SQL, and since it's Monday I didn't want to wrap my mind around the MODEL clause,
therefore my solution looks a bit cluttered - feel free to improve.
create table test (names varchar2(100),start_date date,end_date date,exc_flg number);
INSERT INTO TEST VALUES('John',to_date('01-01-2009','dd-mm-yyyy'),to_date('31-12-2011','dd-mm-yyyy'),0);
INSERT INTO TEST VALUES('John',to_date('01-01-2009','dd-mm-yyyy'),to_date('31-12-2010','dd-mm-yyyy'),1);
INSERT INTO TEST VALUES('John',to_date('01-04-2011','dd-mm-yyyy'),to_date('01-07-2011','dd-mm-yyyy'),1);
INSERT INTO TEST VALUES('Joseph',to_date('01-01-2009','dd-mm-yyyy'),to_date('31-12-2011','dd-mm-yyyy'),0);
INSERT INTO TEST VALUES('Jack',to_date('01-01-2009','dd-mm-yyyy'),to_date('31-12-2010','dd-mm-yyyy'),0);
INSERT INTO TEST VALUES('Joe',to_date('01-01-2009','dd-mm-yyyy'),to_date('31-12-2011','dd-mm-yyyy'),0);
INSERT INTO TEST VALUES('Joe',to_date('01-04-2010','dd-mm-yyyy'),to_date('31-12-2011','dd-mm-yyyy'),0);
INSERT INTO TEST VALUES('Joe',to_date('01-01-2008','dd-mm-yyyy'),to_date('31-12-2009','dd-mm-yyyy'),1);
select * from test;
WITH t AS (SELECT names
, 's' flg_date
, start_date dat
FROM TEST
WHERE exc_flg = 0
UNION ALL
SELECT names
, 'e'
, end_date
FROM TEST
WHERE exc_flg = 0
UNION ALL
SELECT names
, 's'
, end_date + 1
FROM TEST
WHERE exc_flg = 1
UNION ALL
SELECT names
, 'e'
, start_date - 1
FROM TEST
WHERE exc_flg = 1
, t2 AS (SELECT t.*
, DECODE(LAG(t.flg_date) OVER (PARTITION BY t.names ORDER BY t.dat), flg_date, 0, NULL, 1, 1) grp_date
FROM t
WHERE NOT EXISTS (SELECT 1
FROM TEST t2
WHERE t2.names = t.names
AND exc_flg = 1
AND t.dat BETWEEN t2.start_date AND t2.end_date)
AND t.dat BETWEEN (SELECT MIN(start_date)
FROM TEST t2
WHERE t2.names = t.names
AND exc_flg = 0)
AND (SELECT MAX(end_date)
FROM TEST t2
WHERE t2.names = t.names
AND exc_flg = 0)
, t3 AS (SELECT t2.names
, t2.flg_date
, t2.dat
, SUM(grp_date) OVER (PARTITION BY t2.names ORDER BY t2.dat) grp_date
FROM t2
, t4 AS (SELECT t3.*
, row_number() OVER (PARTITION BY t3.names, t3.grp_date ORDER BY t3.dat) rn_start_date
, row_number() OVER (PARTITION BY t3.names, t3.grp_date ORDER BY t3.dat DESC) rn_end_date
FROM t3
SELECT names
, dat
, nxt_dat
FROM (SELECT t4.names
, dat
, CASE WHEN t4.flg_date = 's' THEN LEAD(dat) OVER (PARTITION BY t4.names ORDER BY t4.dat) END nxt_dat
FROM t4
WHERE (t4.flg_date = 's' AND rn_start_date = 1)
OR (t4.flg_date = 'e' AND rn_end_date = 1)
WHERE nxt_dat IS NOT NULL
ORDER BY names, dat
NAMES DAT NXT_DAT
Jack 2009.01.01 00:00:00 2010.12.31 00:00:00
Joe 2010.01.01 00:00:00 2011.12.31 00:00:00
John 2011.01.01 00:00:00 2011.03.31 00:00:00
John 2011.07.02 00:00:00 2011.12.31 00:00:00
Joseph 2009.01.01 00:00:00 2011.12.31 00:00:00C. -
Hi having a slight problem with some SQL code -
We store a number of actions they get given a date_done, a Y if we Met_Customer, a Y if we had an Upsell_Apps , a Y if we Upsell_Other, a Y if there was a reference and a Y if there was an issue. The action must have a date, but can have none to any of the Y's
We want SQL to report back the number of actions (so the count) broken down into the months of the date_done - so Aug 07, Sep 07 etc etc. And this again broken down for Met_customer, Upsell_apps, upsell_other, reference and issue.
We want it to look something like this...
Action Type-------------------- Oct06 | Nov06 | Dec06 | Jan07
Met Customer--------------------12--------8-----------4----------2
Upsell App-------------------------21--------0-----------1----------3
Upsell Other-----------------------0---------1-----------3----------1
Reference---------------------------2---------6----------7----------3
Issue---------------------------------0----------0----------1----------0
So if an action on 12-Oc-06 had a Y in Met_customer and Y in Upsell_app it would be counted twice in the above - whereas below it does not work like that...
At the moment the code is producing something like the following...
MET_CUST | USPELL_APP | UPSELL_OTHER | REFERENCE | ISSUE | Oct06 | Nov06
------Y------------------------------------------------------------------------------------------------------12--------2
------Y-------------------Y---------------------------------------------------------------------------------11--------0
---------------------------Y----------------------------------------------------------------------------------1---------3
------------------------------------------------------Y-------------------------------------------Y----------6---------0
--------------------------------------------------------------------------------Y-----------------------------2---------1
---------------------------------------------------------------------------------------------------Y----------0----------1
---------------------------------------------------------------------------------------------------------------1---------1
This is the code we're using
SELECT
ACTION_MET_CUSTOMER,
ACTION_UPSELL_APPS,
ACTION_UPSELL_OTHER,
ACTION_REFERENCE,
ACTION_ISSUE,
SUM ( decode( substr(DATE_DONE,-3,1), '-', cnt, null ) ) TOTAL,
SUM ( decode( substr(DATE_DONE,-6,6), 'OCT-06', cnt, null ) ) OCT06,
SUM ( decode( substr(DATE_DONE,-6,6), 'NOV-06', cnt, null ) ) NOV06,
SUM ( decode( substr(DATE_DONE,-6,6), 'DEC-06', cnt, null ) ) DEC06,
SUM ( decode( substr(DATE_DONE,-6,6), 'JAN-07', cnt, null ) ) JAN07,
SUM ( decode( substr(DATE_DONE,-6,6), 'FEB-07', cnt, null ) ) FEB07,
SUM ( decode( substr(DATE_DONE,-6,6), 'MAR-07', cnt, null ) ) MAR07,
SUM ( decode( substr(DATE_DONE,-6,6), 'APR-07', cnt, null ) ) APR07,
SUM ( decode( substr(DATE_DONE,-6,6), 'MAY-07', cnt, null ) ) MAY07,
SUM ( decode( substr(DATE_DONE,-6,6), 'JUN-07', cnt, null ) ) JUN07,
SUM ( decode( substr(DATE_DONE,-6,6), 'JUL-07', cnt, null ) ) JUL07,
SUM ( decode( substr(DATE_DONE,-6,6), 'AUG-07', cnt, null ) ) AUG07,
SUM ( decode( substr(DATE_DONE,-6,6), 'SEP-07', cnt, null ) ) SEP07,
SUM ( decode( substr(DATE_DONE,-6,6), 'OCT-07', cnt, null ) ) OCT07,
SUM ( decode( substr(DATE_DONE,-6,6), 'NOV-07', cnt, null ) ) NOV07,
SUM ( decode( substr(DATE_DONE,-6,6), 'DEC-07', cnt, null ) ) DEC07,
SUM ( decode( substr(DATE_DONE,-6,6), 'JAN-08', cnt, null ) ) JAN08,
SUM ( decode( substr(DATE_DONE,-6,6), 'FEB-08', cnt, null ) ) FEB08,
SUM ( decode( substr(DATE_DONE,-6,6), 'MAR-08', cnt, null ) ) MAR08,
SUM ( decode( substr(DATE_DONE,-6,6), 'APR-08', cnt, null ) ) APR08,
SUM ( decode( substr(DATE_DONE,-6,6), 'MAY-08', cnt, null ) ) MAY08
FROM
(SELECT ACTION_MET_CUSTOMER, ACTION_UPSELL_APPS, ACTION_UPSELL_OTHER, ACTION_REFERENCE, ACTION_ISSUE, DATE_DONE, COUNT(*) CNT FROM ACTIONS, EMPLOYEE
WHERE ACTION_OWNER = NAME
GROUP BY ACTION_MET_CUSTOMER, ACTION_UPSELL_APPS, ACTION_UPSELL_OTHER, ACTION_REFERENCE, ACTION_ISSUE, DATE_DONE)
GROUP BY
ACTION_MET_CUSTOMER, ACTION_UPSELL_APPS, ACTION_UPSELL_OTHER, ACTION_REFERENCE, ACTION_ISSUE
Any ideas to get it looking how id ideally like it?
Cheers, would be a great help
Sselect 'Met Customer' action_type,
to_char(date_done,'MON-YY') date_done,
action_owner,
COUNT(1)
from ff_actions
where action_met_customer = 'Y'
group by action_met_customer
UNION
select 'Upsell Apps' action_type,
to_char(date_done,'MON-YY') date_done,
action_owner,
COUNT(1)
from ff_actions
where action_upsell_apps = 'Y'
group by action_upsell_apps
UNION
select 'Upsell Other' action_type,
to_char(date_done,'MON-YY') date_done,
action_owner,
COUNT(1)
from ff_actions
where action_upsell_other = 'Y'
group by action_upsell_other
UNION
select 'Reference' action_type,
to_char(date_done,'MON-YY') date_done,
action_owner,
COUNT(1)
from ff_actions
where action_reference = 'Y'
group by action_reference
UNION
select 'Issue' action_type,
to_char(date_done,'MON-YY') date_done,
action_owner,
COUNT(1)
from ff_actions
where action_issue = 'Y'
group by action_issue
now error...
ORA-00979: not a GROUP BY expression
Maybe you are looking for
-
Just another thread to document the major issues I'm having with Lightroom 4. System: Dell Studio XPS running Windows 7 Home Premium, Intel i7-720QM, 8 GB RAM 1333MHz, 500 GB HD, ATI Radeon 4670 1GB with Wacom Intuos 5 Working in the Develop module h
-
Daily stats required from statspack for graph of month activity
Hi All, I am trying to get a graph of index io on a daily basis, the trouble is the stats are cumulative since database startup and the database is rarely bounced. So what I actually get is a growing value for each day. I expect there is a simple way
-
Adobe X Standard not recognising scanner
I've just installed AcrobatX Standard and in the installation it will not recognise my network scanner and has stopped all other programmes recognising the scanner too! Can anyone help?
-
Exclude Baggage Files From RH8 Search
Hi... I have noticed that you can exclude pages from appearing in the RH8 search (Topic Properties > Exclude From Search), but are you also able to do this with baggage files? (now that RH8 indexes those too). I have a large number of PDF baggage fil
-
About to purchase new Mac Pro - Need Advice
I am about to purchase a new mac pro and have some questions I am planning to use final cut pro as well as Edius on Boot Camp. I intend to edit footage in HDV from a sony Z1 and also from the new sony - pmw -ex3 which i will be planning to edit in HD