How to use decode to calculate sum for different date range
I'm stuck with decode() function:
I have a table like this:
(project_id, approve_date, value, builder_code)
I want to write a SQL query to get sum of values for different month of the approve_date, and group by builder_code)
The result is like this:
builder_code Sum(value)_Sep-03 Sum(value)_Oct-03 Sum(value)_Nov03
1001 1,299 1,322 990
1002 3,332 1,222 333
I tried to use decode for this question but could not get the answer.
Thanks a lot
I don't think you need a DECODE() here. I'd do something like this-
create table builder (
project_id number,
approve_date date,
value number,
builder_code number
insert into builder values( 1, to_date('09-01-2003', 'MM-DD-YYYY'), 100, 990 )
insert into builder values( 2, to_date('09-03-2003', 'MM-DD-YYYY'), 150, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 990 )
insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 333)
SELECT sept.builder_code, sept.sept_sum, oct.oct_sum
FROM (SELECT builder_code, sum(value) sept_sum
FROM builder
WHERE approve_date >= to_date('09-01-2003','MM-DD-YYYY')
AND approve_date < to_date('10-01-2003','MM-DD-YYYY')
GROUP BY builder_code) sept,
(SELECT builder_code, sum(value) oct_sum
FROM builder
WHERE approve_date >= to_date('10-01-2003','MM-DD-YYYY')
AND approve_date < to_date('11-01-2003','MM-DD-YYYY')
GROUP BY builder_code) oct
WHERE oct.builder_code(+) = sept.builder_code
BUILDER_CODE SEPT_SUM OCT_SUM
333 250
990 500Justin
Distributed Database Consulting, Inc.
www.ddbcinc.com/askDDBC
Similar Messages
-
How to use dbo procedure to execute for different schema?
I am using SQL Server 2008 R2, I have a schema [dbo],
and in that schema, I have created a stored procedure dbo.GetAccount:
CREATE PROCEDURE dbo.GetAccount
AS
BEGIN
select * from tblAccountNet
END
GO
Then, I have created a schema [ABC] with
user named UserABC.
Now, I would like to login with UserABC and
execute dbo.GetAccount for
schema [ABC] to
get all user of it and don't want to change code of dbo.GetAccount.
So, how can I do?
Thanks for your help.Thank you everyone.
But, I don't want to change procedure dbo.GetAccount because in face, I have more 100 procedures. I think of two solutions:
1. Create another stored procedure in
[dbo] schema, and use it to read all other procedure to list all table used in procedure and assign them with schema
[ABC]
2. Create
a stored procedure to change schema of all table in every procedure
But I really don't know how to do it. Please help me.
Thanks so much. -
How to use Purch. Info Record, for different Valuation Types ?
Hi All,
While doing Split Valuation,
I have created three valuation types - say, M1,M2,M3. for material M.
Now, I want to create Info record for material M and Vendor V1.
when i go to me11, system is allowing me to create info record for M and V1 once only,
in Purchasing Organization View, I'm getting the Valuation type field,
but can't entering 3 prices for 3 valuation types,
such as in Material Master.
Can any one help me regarding this....
Thanks,
Anand.if u create po with info update indicator then u can use your valuatiion type in po for whihc there is no ifo recrod existing then system willl createt one
but actually the price will get stored in the price oder history not exactly in the pir -
How to use sql tag of jstl for inserting data
hello
I want to insert the data in to oracle through jsp using standard sql tags of jstl please help me1) Go to www.google.com.
2) Enter in that input field "jstl sql tag tutorial" (assuming you want to learn the JSTL SQL tag library).
3) Hit that search button.
4) Explore the results. One of them is http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSTL7.html -
How can I make CONTAINS query work for a date range
In either 9i or 10g (eventual). I have a CONTEXT index that contains multiple columns from multiple tables and using a USER_DATASTORE. E.g., I have names that come from 3 different table locations and dates that come from 4. I can index them fine but how can I perform a single consolidated CONTAINS query against the single CONTEXT index to do the following:
smith WITHIN lname AND john WITHIN fname AND dob BETWEEN '19870315' and '19970315'
I know that I can use a mixed query but this is inefficient (esp since I have birth dates in multiple tables). Is there any algorithm for a range operator (>, <, between?) within the CONTAINS operator?
CTXCAT index is not an option, as I have many text columns I am searching.
Thanks!When you run the cdstore.sql, in addition to creating the ctx_cd package, it also creates the friedman package that contains the algorithm that the ctx_cd package uses. You could use the functions from that friedman package in your procedure for your user_datastore and in the creation of your query string, as demonstrated below.
SCOTT@orcl_11g> CREATE OR REPLACE PROCEDURE my_proc
2 (p_rowid IN ROWID,
3 p_clob IN OUT NOCOPY CLOB)
4 AS
5 BEGIN
6 FOR r IN
7 (SELECT emp.ename, emp.job, emp.hiredate, dept.dname
8 FROM emp, dept
9 WHERE emp.deptno = dept.deptno
10 AND emp.ROWID = p_rowid)
11 LOOP
12 DBMS_LOB.WRITEAPPEND (p_clob, 7, '<ename>');
13 DBMS_LOB.WRITEAPPEND (p_clob, LENGTH (r.ename), r.ename);
14 DBMS_LOB.WRITEAPPEND (p_clob, 8, '</ename>');
15 DBMS_LOB.WRITEAPPEND (p_clob, 5, '<job>');
16 DBMS_LOB.WRITEAPPEND (p_clob, LENGTH (r.job), r.job);
17 DBMS_LOB.WRITEAPPEND (p_clob, 6, '</job>');
18 DBMS_LOB.WRITEAPPEND (p_clob, 7, '<dname>');
19 DBMS_LOB.WRITEAPPEND (p_clob, LENGTH (r.dname), r.dname);
20 DBMS_LOB.WRITEAPPEND (p_clob, 8, '</dname>');
21 DBMS_LOB.WRITEAPPEND (p_clob, 10, '<hiredate>');
22 -- apply friedman algorithm to date column ------------------
23 friedman.init
24 (TO_NUMBER (TO_CHAR (TO_DATE (19000101, 'YYYYMMDD'), 'J')),
25 TO_NUMBER (TO_CHAR (TO_DATE (21001231, 'YYYYMMDD'), 'J')));
26 DBMS_LOB.WRITEAPPEND
27 (p_clob,
28 LENGTH (friedman.encodedate (r.hiredate)),
29 friedman.encodedate (r.hiredate));
30 --------------------------------------------------------------
31 DBMS_LOB.WRITEAPPEND (p_clob, 11, '</hiredate>');
32 END LOOP;
33 END my_proc;
34 /
Procedure created.
SCOTT@orcl_11g> SHOW ERRORS
No errors.
SCOTT@orcl_11g> BEGIN
2 CTX_DDL.CREATE_PREFERENCE ('my_datastore', 'USER_DATASTORE');
3 CTX_DDL.SET_ATTRIBUTE ('my_datastore', 'PROCEDURE', 'my_proc');
4 END;
5 /
PL/SQL procedure successfully completed.
SCOTT@orcl_11g> CREATE INDEX my_index ON emp (ename)
2 INDEXTYPE IS CTXSYS.CONTEXT
3 PARAMETERS
4 ('DATASTORE my_datastore
5 SECTION GROUP CTXSYS.AUTO_SECTION_GROUP')
6 /
Index created.
SCOTT@orcl_11g> EXEC DBMS_STATS.GATHER_TABLE_STATS (USER, 'DEPT')
PL/SQL procedure successfully completed.
SCOTT@orcl_11g> EXEC DBMS_STATS.GATHER_TABLE_STATS (USER, 'EMP')
PL/SQL procedure successfully completed.
SCOTT@orcl_11g> VARIABLE cstring VARCHAR2(4000)
SCOTT@orcl_11g> BEGIN
2 :cstring := 'smith WITHIN ename';
3 :cstring := :cstring || ' AND ' || 'clerk WITHIN job';
4 :cstring := :cstring || ' AND ' || 'research WITHIN dname';
5 -- apply friedman algorithm to search criteria ---------------------------
6 friedman.init
7 (TO_NUMBER (TO_CHAR (TO_DATE (19000101, 'YYYYMMDD'), 'J')),
8 TO_NUMBER (TO_CHAR (TO_DATE (21001231, 'YYYYMMDD'), 'J')));
9 :cstring := :cstring || ' AND ((' ||
10 friedman.integercontainscriteria
11 (TO_NUMBER (TO_CHAR (TO_DATE ('19800315', 'YYYYMMDD'), 'J')),
12 TO_NUMBER (TO_CHAR (TO_DATE ('19810315', 'YYYYMMDD'), 'J')),
13 'B')
14 || ') WITHIN hiredate)';
15 ---------------------------------------------------------------------------
16 END;
17 /
PL/SQL procedure successfully completed.
SCOTT@orcl_11g> SET AUTOTRACE ON EXPLAIN
SCOTT@orcl_11g> SELECT *
2 FROM emp
3 WHERE CONTAINS (ename, :cstring) > 0
4 /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
Execution Plan
Plan hash value: 1887222286
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 1 | 37 | 4 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 1 | 37 | 4 (0)| 00:00:01 |
|* 2 | DOMAIN INDEX | MY_INDEX | | | 4 (0)| 00:00:01 |
Predicate Information (identified by operation id):
2 - access("CTXSYS"."CONTAINS"("ENAME",:CSTRING)>0)
SCOTT@orcl_11g> -
Column wise output for different date range
I m having a query which gives the output like
DISTRIBUTOR_CODE APR MAY JUN JUL AUG
R127 01/04/2005
R127 02/04/2005
R127 03/04/2005
R127 01/05/2005
R127 02/05/2005
R127 03/05/2005
R127 01/06/2005
while the output needed by me is
DISTRIBUTOR_CODE APR MAY JUN JUL AUG
R127 01/04/2005 01/05/2005 01/06/2005
R127 02/04/2005 02/05/2005
R127 03/04/2005 03/05/2005
R127
The query is
The query used by me is as below
select distributor_code,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),0),'MM'),param_date) apr,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),1),'MM'),param_date) may,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),2),'MM'),param_date) jun,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),3),'MM'),param_date) jul
,decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),4),'MM'), param_date) aug
,decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),5),'MM'), param_date) sep,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),6),'MM'),param_date) oct,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),7),'MM'),param_date) nov,
decode(to_char(param_date,'MM'),to_char(add_months(min(mindt),8),'MM'),param_date) dec
from ppbsnew.dms_audit_trail t,(select distributor_code dcode ,min(param_date) mindt
from ppbsnew.dms_audit_trail
where trunc(param_Date) between '01-APR-05' and '31-DEC-05' group by distributor_code) t2
where t.distributor_code=t2.dcode
and trunc(param_Date) between '01-APR-05' and '31-DEC-05'
and t.distributor_name is not null
group by distributor_code,param_Datehi
SQL> select job,
2 decode(to_char(hiredate,'MON'),'JAN',hiredate, NULL ) JAN,
3 decode(to_char(hiredate,'MON'),'FEB',hiredate, NULL ) FEB,
4 decode(to_char(hiredate,'MON'),'MAR',hiredate, NULL ) MAR,
5 decode(to_char(hiredate,'MON'),'APR',hiredate, NULL ) APR,
6 decode(to_char(hiredate,'MON'),'MAY',hiredate, NULL ) MAY,
7 decode(to_char(hiredate,'MON'),'JUN',hiredate, NULL ) JUN,
8 decode(to_char(hiredate,'MON'),'JUL',hiredate, NULL ) JUL,
9 decode(to_char(hiredate,'MON'),'AUG',hiredate, NULL ) AUG,
10 decode(to_char(hiredate,'MON'),'SEP',hiredate, NULL ) SEP,
11 decode(to_char(hiredate,'MON'),'OCT',hiredate, NULL ) OCT,
12 decode(to_char(hiredate,'MON'),'NOV',hiredate, NULL ) NOV,
13 decode(to_char(hiredate,'MON'),'DEC',hiredate, NULL ) DEC
14 from emp where to_date(hiredate,'DD-MON-YY') between '01-JAN-81' and '31-DEC-81';
JOB JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
SALESMAN 20-FEB-81
SALESMAN 22-FEB-81
MANAGER 02-APR-81
SALESMAN 28-SEP-81
MANAGER 01-MAY-81
MANAGER 09-JUN-81
PRESIDENT 17-NOV-81
SALESMAN 08-SEP-81
CLERK 03-DEC-81
ANALYST 03-DEC-81
10 rows selected.Is that you wana achieve??
Khurram Siddiqui
[email protected] -
Workday calculation for specified date range
Hi All,
How to count number of workdays available for given date range. And I tried below statement which is
not working.
SUM(CASE [Calendar].[Calendar Date]
WHEN [Calendar].[Calendar Date].[Year] = '2005' AND [Calendar].[Calendar Date].[Month]
= 1
THEN [Calendar].[Calendar Date].CURRENTMEMBER.CHILDREN
END
, [Measures].[Work Day Flag])Hi bpbhhaskar,
According to your description, you want to count the weekdays in MDX. Right?
In this scenario, we can just aggregation days from Monday to Friday. The key should be 2 to 6. Please use the expression below:
with
MEMBER [Measures].[DayCount] AS
Count
Descendants
[Date].[Calender].CurrentMember
,[Date].[Calender].[Date]
* {[Date].[Day Of Week].&[2]:[Date].[Day Of Week].&[6]}
Best Regards,
Simon Hou
TechNet Community Support -
Get table partition name dynamically for given date range
Dear All,
Could you please tell me how to get the partition name dynamicaly for given date range ?
Thank you.SQL> select table_name,
partition_name,
to_date (
trim (
'''' from regexp_substr (
extractvalue (
dbms_xmlgen.
getxmltype (
'select high_value from all_tab_partitions where table_name='''
|| table_name
|| ''' and table_owner = '''
|| table_owner
|| ''' and partition_name = '''
|| partition_name
|| ''''),
'//text()'),
'syyyy-mm-dd hh24:mi:ss')
high_value_in_date_format
from all_tab_partitions
where table_name = 'SALES' and table_owner = 'SH'
TABLE_NAME PARTITION_NAME HIGH_VALUE_IN_DATE_FORMAT
SALES SALES_1995 01-JAN-96
SALES SALES_1996 01-JAN-97
SALES SALES_H1_1997 01-JUL-97
SALES SALES_H2_1997 01-JAN-98
SALES SALES_Q1_1998 01-APR-98
SALES SALES_Q2_1998 01-JUL-98
SALES SALES_Q3_1998 01-OKT-98
SALES SALES_Q4_1998 01-JAN-99
SALES SALES_Q1_1999 01-APR-99
SALES SALES_Q2_1999 01-JUL-99
SALES SALES_Q3_1999 01-OKT-99
SALES SALES_Q4_1999 01-JAN-00
SALES SALES_Q1_2000 01-APR-00
SALES SALES_Q2_2000 01-JUL-00
SALES SALES_Q3_2000 01-OKT-00
SALES SALES_Q4_2000 01-JAN-01
SALES SALES_Q1_2001 01-APR-01
SALES SALES_Q2_2001 01-JUL-01
SALES SALES_Q3_2001 01-OKT-01
SALES SALES_Q4_2001 01-JAN-02
SALES SALES_Q1_2002 01-APR-02
SALES SALES_Q2_2002 01-JUL-02
SALES SALES_Q3_2002 01-OKT-02
SALES SALES_Q4_2002 01-JAN-03
SALES SALES_Q1_2003 01-APR-03
SALES SALES_Q2_2003 01-JUL-03
SALES SALES_Q3_2003 01-OKT-03
SALES SALES_Q4_2003 01-JAN-04
28 rows selected. -
How to use decode function in oracle apex 3.2.1
Hello Friends,
how to use decode
var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
appreciate your help.
regards/kumar
Edited by: kumar73 on Apr 23, 2010 12:25 PM
Edited by: kumar73 on Apr 23, 2010 12:27 PMHey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
For example, if I had a string:
I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
note: I used the caret (^) character by convention but any valid character will do
...all of which leaves your original string more or less unadulterated.
I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
Give it a go! -
How to use the same POWL query for multiple users
Hello,
I have defined a POWL query which executes properly. But if I map the same POWL query to 2 portal users and the 2 portal users try to access the same page simultaneously then it gives an error message to one of the users that
"Query 'ABC' is already open in another session."
where 'ABC' is the query name.
Can you please tell me how to use the same POWL query for multiple users ?
A fast reply would be highly appreciated.
Thanks and Regards,
SandhyaBatch processing usually involves using actions you have recorded. In Action you can insert Path that can be used during processing documents. Path have some size so you may want to only process document that have the same size. Look in the Actions Palette fly-out menu for insert path. It inserts|records the current document work path into the action being worked on and when the action is played it inserts the path into the document as the current work path..
-
How to use the same services-config for the local and remote servers.
My flex project works fine using the below but when I upload my flash file to the server I doesn't work, all the relative paths and files are the same execpt the remote one is a linux server.
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="amfphp-flashremoting-service"
class="flex.messaging.services.RemotingService"
messageTypes="flex.messaging.messages.RemotingMessage">
<destination id="amfphp">
<channels>
<channel ref="my-amfphp"/>
</channels>
<properties>
<source>*</source>
</properties>
</destination>
</service>
</services>
<channels>
<channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
<endpoint uri="http://localhost/domainn.org/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
I think the problem is the line
<endpoint uri="http://localhost/domainn.org/amfphp/gateway.php" class="flex.messaging.endpoints.AMFEndpoint"/>
but I'm not sure how to use the same services-config for the local and remote servers.paul.williams wrote:
You are confusing "served from a web-server" with "compiled on a web-server". Served from a web-server means you are downloading a file from the web-server, it does not necessarily mean that the files has been generated / compiled on the server.
The server.name and server.port tokens are replaced at runtime (ie. on the client when the swf has been downloaded and is running) not compile time (ie. while mxmlc / ant / wet-tier compiler is running). You do not need to compile on the server to take advantage of this.
Hi Paul,
In Flex, there is feature that lets developer to put all service-config.xml file configuration information into swf file. with
-services=path/to/services-config.xml
IF
services-config.xml
have tokens in it and user have not specified additional
-context-root
and this swf file is not served from web-app-server (like tomcat for example) than it will not work,
Flash player have no possible way to replace token values of service-config.xml file durring runtime if that service-config.xml file have been baked into swf file during compilation,
for example during development you can launch your swf file from your browser with file// protocol and still be able to access blazeDS services if
-services=path/to/services-config.xml
have been specified durring compilation.
I dont know any better way to exmplain this, but in summary there is two places that you can tell swf about service confogiration,
1) pass -services=path/to/services-config.xml parameter to compiler this way you tell swf file up front about all that good stuff,
or 2) you put that file on the webserver( in this case, yes you should have replacement tokens in that file) and they will be repaced at runtime . -
How to use the same email address for outgoing email on two iPads?
How to use the same email address for outgoing email on two iPads?. My wife and I both use the same email address. We receive the same incoming email on both iPads but are unable to send email from my wife's iPad. She is using an iPad 2 and is usng IOS 6.0.1
Hi Creaturekeeper,
If you've added the account into the iPad and you are unable to send email from it, then please follow the troubleshooting in the article below.
Get help with Mail on iPhone, iPad, and iPod touch - Apple Support
Take care,
Alex H. -
How to use the field of 'FM for Determining Address' in report RSWUWFML2?
Dear gurus,
In the selection screen of report RSWUWFML2, there is a field named 'FM for Determining Address' . How to use it? Is it for sending e-mail to specific e-mail addresses?
Regards
DavidI will test soon. Thanks to all involved.
-
WSUS 3.2 work on Win2008 R2, how to use it deploy MS patch for Win 2012 ?
WSUS 3.2 work on Win2008 R2, how to use it deploy MS patch for Win 2012 ?
I have installed KB2734608, but when I search MS13-101 , no patch for Win 2012.
Can you help me resolve this problem ?
ThanksOn Thu, 10 Apr 2014 06:01:48 +0000, fujitsu08 wrote:
WSUS 3.2 work on Win2008 R2, how to use it deploy MS patch for Win 2012 ?
WSUS has its own, dedicated forum where the WSUS experts answer questions.
You should repost your question here:
http://social.technet.microsoft.com/Forums/windowsserver/en-US/home?forum=winserverwsus
Paul Adare - FIM CM MVP
Aibohphobia: n. Fear of Palindromes -
How to use decode concept in htp.p?
Hi All,
How to use decode function in htp.p syntax?
Thanks,
Anoo..Hi,
Could you please give more details on what you like to achieve? Decode function works only in sql query.
You can first do sql query, and then do htp.p.
Or you can use PL/SQL IF or CASE statements.
You can also create custom decode function if you need.
Regards,
Oleg
Maybe you are looking for
-
Sony BRAVIA Sound bar home theatre system
I just bought one of those Sony BRAVIA sound bar home theatre systems & I am trying to hook up my old model vcr/dvd combo so I can get surround sound on the dvd player. My husband & I have almost tried everything & it still won't work. It doesn't hav
-
Data Guard - MRP stuck issues on a physical standby database
Hi, Oracle 11.2.0.3 DG running. When i do a switchover the physical standby database has error with following error ARC0: LGWR is actively archiving destination LOG_ARCHIVE_DEST_2 ARCH: Archival stopped, error occurred. Will continue retrying ORACLE
-
Export Gif? Save for web?
Do either of these options exist in InDesign, anywhere, with a plugin or whatever? Thanks!
-
I really do not want to start a controversy, but I'm wondering if Apple has a problem that they are not telling us about. Did they come up with the "Time Machine" program because there is a problem with their computers losing files? Twice now I have
-
My iphone 4 drop down after that it start showing apple logo i have restored it after that it shows searching on carrier signal and shows that Connect to itune... when i connect it to itune, message came that there is some problem with your iphone.Pl