SQL - grouping of cases
Hello everyone,I'm trying to get a count of all nulls v/s of the non-null values in a table. I'm trying to to do this, use the CASE statement.
I trained the SQL following so far.
SELECT count(Aj.Asset_Hdr_Id),
CASE Nvl(Aj.Asset_Hdr_Id,
0)
WHEN 0 THEN
'IS NULL'
ELSE
'IS NOT NULL'
END
FROM Asset_Jt Aj
GROUP BY Nvl(Aj.Asset_Hdr_Id, 0);
It only gives me the desired result. My goal will look like2089 IS NULL
12340 IS NOT NULL
I get a result that looks like this1 0 IS NULL
2 1 IS NOT NULL
3 1 IS NOT NULL
4 1 IS NOT NULL
5 1 IS NOT NULL
Try this,
SELECT COUNT (*), result
FROM (SELECT CASE NVL (Aj.Asset_Hdr_Id, 0)
WHEN 0 THEN 'IS NULL'
ELSE 'IS NOT NULL'
END
result
FROM Asset_Jt Aj)
GROUP BY result;
G.
Tags: Database
Similar Questions
-
I am confused regarding the performance of the Sub SQL in 2 cases
In order to obtain the total no.. of the more intimate select timeline.
Once I use the function "COUNT (*) NTC ().
SELECT b.lastname,
b.FirstName,
b.Department,
b.org_relationship,
b.enterprise_name,
b.CNT
DE)
SELECT a.*,
rownum rn
DE)
SELECT P.LastName,
p.FirstName,
pORG. DEPARTMENT,
pORG.org_relationship,
pORG.enterprise_name,
COUNTY () MORE (NTC) - & gt; the function "COUNT (*) (cnt)."
T_person p,
t_contact c1,
porg t_o_person
WHERE p.person_id = c1.ref_id (+)
AND p.person_id = porg.o_person_id
ORDER BY upper (p.lastname), upper (p.firstname)
) a
WHERE rownum & lt; = & lt; & lt; Max asked line & gt; & gt;
) b
WHERE rn & gt; & lt; & lt; min line requested & gt; & gt;
****************************************************
*************************************************
Then, I use a QUERY SELECT INTERNAL to get the total.
SELECT b.lastname,
b.FirstName,
b.Department,
b.org_relationship,
b.enterprise_name,
b.CNT
DE)
SELECT a.*,
rownum rn
DE)
SELECT P.LastName,
p.FirstName,
pORG. DEPARTMENT,
pORG.org_relationship,
pORG.enterprise_name,
* (SELECT count (*)-& gt;) QUERY SELECT INTERNAL for a total count.*
FROM t_person p,
t_contact c1,
porg t_o_person
WHERE p.person_id = c1.ref_id (+)
AND p.person_id = porg.o_person_id
ORDER BY upper (p.lastname), upper (p.firstname)
*) did NTC *.
T_person p,
t_contact c1,
porg t_o_person
WHERE p.person_id = c1.ref_id (+)
AND p.person_id = porg.o_person_id
ORDER BY upper (p.lastname), upper (p.firstname)
) a
WHERE rownum & lt; = & lt; & lt; Max asked line & gt; & gt;
) b
WHERE rn & gt; & lt; & lt; min line requested & gt; & gt;
So, wanted to know which option would be most effective in this case? What the SELECT INTERNAL SEPARATE to get the COUNT or by using the COUNT () at the NTC ()? *
Published by: [email protected] on March 10, 2009 12:41user_7000011 wrote:
Thanks for the inputs. I followed the steps in the link and have gathered some information on the SQLs.However, since I'm new to these topics-i couldnot get
details in its entirety.I've pasted below, data
As shown in the links provided, could you please add that outputs the "Predicate information" section to your PLAN to EXPLAIN next time? It directly follows the exit PLAN and is essential information.
How exactly you generate execution plans? I do not see the optimizer to understand your Top N query, so it does not switch to FIRST_ROWS_n mode. So what have you used for this part of your query execution PLAN of EXPLAIN?
WHERE rownum <= <
> ) b WHERE rn > < > The only difference between the execution of the posted plans that I can spot is that we add a cost for an ORDER BY operation while the other shows only the analytic SORT of WINDOW operation free of charge. I don't know why the ORDER BY operation is absent from the first, probably since the ORDER was already held by the SORT of WINDOW operation (or the declaration was not the same).
The links provided show you step by step instructions, including the information requested by Justin, and much more, for example the scope SQL trace of your statements and in 10 g DBMS_XPLAN scope. Output DISPLAY_CURSOR which is also very useful.
You must simply use the snippets provided in the links and put them in your statement, so if you have trouble following the instructions there that I would be interested to know what was the problem, so that I can improve it.
Kind regards
RandolfOracle related blog stuff:
http://Oracle-Randolf.blogspot.com/SQLTools ++ for Oracle (Open source Oracle GUI for Windows):
http://www.sqltools-plusplus.org:7676 /.
http://sourceforge.NET/projects/SQLT-pp/ -
SQL GROUP BY / HAVING a question
Hello
A few days earlier, I came across an interview question that I can not resolve correctly.
In short, there are two tables: BOOKS and TAGS.
ID TITLE AUTHOR 1 Twilight Stephenie Meyer 2 Catch fire Suzanne Collins 3 Animal farm George Orwell BOOK_ID TAG 1 Best Seller 1 Science Fiction 1 TOP10 2 Best Seller 2 Note. 2 TOP10 3 TOP110 However several tags and the result must be a set of appropriate books, users can enter.
For example, in the case of tags 'Best Seller', 'Science Fiction' and 'Top 10', the result must be "Twilight."
My solution was something like this:
SELECT b.title FROM books b WHERE b.id IN ( SELECT book_id FROM tags WHERE LOWER(tag) = 'best seller' ) AND b.id IN ( SELECT book_id FROM tags WHERE LOWER(tag) = 'science fiction' ) AND b.id IN ( SELECT book_id FROM tags WHERE LOWER(tag) = 'top10' )
But it is definetaly not an elegant and flexible query. In the case of labels of 10 or more incoming, we get a code 'spagetti '.
Somehow, it should be possible to resolve using GROUP BY and HAVING, but I don't really know how.
Could someone help what would be the most elegant solution to this problem?
Thank you so much in advance.
Maybe:
SQL >-generating sample data:
SQL > with books like)
2. select id 1, the title of 'Twilight', author of "Stephenie Meyer" Union double all the
3 select 2, "Taking fire", "Suzanne Collins' Union double all the
4 Select 3, 'Animal Farm', 'George Orwell' of the double
5)
6, tags such as)
7 select 1 book_id, tag "Best Seller" of all the double union
8. Select 1, 'Science Fiction' from dual union all
9. Select 1, 'TOP10' from dual union all
10. Select 2, 'Best Seller' from dual union all
11. Select 2, 'Roman' from dual union all
12. Select 2, 'TOP10' from dual union all
13. Select 3, 'TOP110' from dual
14)
15-
16 - the actual query:
17-
18 select b.title
Books b 19
20, tags t
21 where t.book_id = b.id
22 and t.tag ('Best Seller', 'Science Fiction', 'Top 10')
23 by b.title group
24 after having count (*) = 3
25.
TITLE
-------------
Twilight
1 selected line.
-
error in pl/sql of the CASE clause.
Hello
I m not a sql progremmer. an interior according to the database worked for previous but not mentioned dates and may be for dates too.
Error is given after the following code.
We have 4 store-ID, that ID is to have values rest three IDs have values 0. Gets the ID having missed in FUND and error values. The cursor query works fine when executed alone.
kindly advice!
declare
ip_date varchar2 (30): =' 27-AUG-2011.
cursor dwn_cnt (param_date varchar2, param_hr number), select store b.aw_store_id, count (*) bv1to1.aw_user_dwnld_cnt count_dwn, b bv1to1.bv_user_profile where a.date_download between to_date(param_date||) e '|| param_hr |': 0 0:00 ', "HH24:MI:SS of MON-DD-YYYY") and to_date(param_date||) e '|| param_hr | (": 59: 59 ',' MON-DD-YYYY HH24:MI:SS') and a.user_id = b.user_id group by b.aw_store_id;
dwn_cnt_var dwn_cnt % rowtype;
type dwn_cnt_hr is table of number of pls_integer indexes;
dwn_cnt_hr_var dwn_cnt_hr;
Start
dbms_output.put_line ('US' |) ',' ||' DE'||','||' UK'||','||' FR'||','||' TIME ');
dbms_output.put_line(' ');
I'm in 0.23
loop
dwn_cnt_hr_var (0): = 0;
dwn_cnt_hr_var (103): = 0;
dwn_cnt_hr_var (104): = 0;
dwn_cnt_hr_var (105): = 0;
dwn_cnt_hr_var (200): = 0;
Open dwn_cnt (ip_date, i);
extract the dwn_cnt in dwn_cnt_var;
loop
box (dwn_cnt_var. Store)
When 0 then
dwn_cnt_hr_var (0): = dwn_cnt_var.count_dwn;
When there are 103 then
dwn_cnt_hr_var (103): = dwn_cnt_var.count_dwn;
When there are 104 then
dwn_cnt_hr_var (104): = dwn_cnt_var.count_dwn;
When 105 then
dwn_cnt_hr_var (105): = dwn_cnt_var.count_dwn;
When 200 then
dwn_cnt_hr_var (200): = dwn_cnt_var.count_dwn;
end case;
extract the dwn_cnt in dwn_cnt_var;
When the output dwn_cnt % notfound;
end loop;
dbms_output.put_line (dwn_cnt_hr_var (0) + dwn_cnt_hr_var (200) |) «, » || dwn_cnt_hr_var (103) | «, » || dwn_cnt_hr_var (105) | «, » || dwn_cnt_hr_var (104) | «, » || (i);
close dwn_cnt;
end loop;
end;
/
OUTPUT:-
US, UK, FR, HOUR
declare
*
ERROR on line 1:
ORA-06592: not found CASE when executing CASE statement
ORA-06512: at line 20
Elapsed time: 00:00:00.90
Rgds,
Abdallah.You need an ELSE clause:
... when 200 then dwn_cnt_hr_var(200):=dwn_cnt_var.count_dwn; else /* code here for what to do if none of the WHEN clauses are true */ end case; ...
-
Hi all
I'm writing a query that selects records that are not within a period of 90 days from the other beginning with the first date. Is that records are selected that are more than 90 days, independently of each other. So, for the next game
(Tbl) CREATE TABLE
class VARCHAR2 (3200),.
actiondate DATE);
INSERT INTO tbl
VALUES ("C1",
"(2007-02-12');
INSERT INTO tbl
VALUES ("C1",
"(2007-06-01');
INSERT INTO tbl
VALUES ("C1",
"(01/05/2009 ');
INSERT INTO tbl
VALUES ("C1",
"(01/07/2009 ');
INSERT INTO tbl
VALUES ("C1",
"(01/09/2009 ');
INSERT INTO tbl
VALUES ("C1",
October 1, 2009 ");"
INSERT INTO tbl
VALUES ("C1",
01/01/2010');
INSERT INTO tbl
VALUES ("C1",
"(2010-02-01');
INSERT INTO tbl
VALUES ("C1",
"(2010-03-01');
INSERT INTO tbl
VALUES ("C1",
"(2010-05-01');
INSERT INTO tbl
VALUES ("C1",
"(07/30/2009 ');
INSERT INTO tbl
VALUES ("C1",
"(29/07/2009 ');
INSERT INTO tbl
VALUES ("C2",
"(2008-02-01');
INSERT INTO tbl
VALUES ("C2",
"(2008-05-02');
INSERT INTO tbl
VALUES ("C2",
"(2008-06-01');
INSERT INTO tbl
VALUES ("C2",
October 15, 2008 ');
INSERT INTO tbl
VALUES ("C2",
01/01/2009');
INSERT INTO tbl
VALUES ("C2",
' (15/02/2009 ');
INSERT INTO tbl
VALUES ("C2",
"(30/05/2009 ');
INSERT INTO tbl
VALUES ("C2",
October 1, 2009 ");"
I need to return the following documents
CLASS ACTIONDATE
C1, 12/02/2007
C1, 01/06/2007
C1, 01/05/2009
C1, 01/09/2009
C1, 01/01/2010
C1, 01/05/2010
C2, 01/02/2008
C2, 02/05/2008
C2, 15/10/2008
C2, 15/02/2009
C2, 30/05/2009
C2, 01/10/2009
He tried the following query does not work
SELECT Actiondate,
Min (actiondate)
COURSES (ORDER BY actiondate SCOPE BETWEEN the '90' LINE PREVIOUS AND CURRENT DAY LEVEL) dt
FROM tbl
I also tried the following method, but does not work.
SELECT Ceil ((actiondate-DATE "-4712-01-01') / 90) * 90 + DATE" - 4712-01-01', "
Min (actiondate)
DUNGEON (DENSE_RANK LAST ORDER BY actiondate, rowid)
FROM tbl
GROUP BY Ceil ((actiondate-DATE "-4712-01-01') / 90) * 90 + DATE" - 4712-01-01'"
Is there a SQL to achieve or PL/SQL is the only way to do this? The pointers will be greatly appreciated.
Please consider that the table contains more than 3 million lines.
Rgds
Thank you.Hello
Here's the Dimacit of [the aforementioned thread | http://forums.oracle.com/forums/message.jspa?messageID=2526363#2526363].
I just changed the names of table and column, the length of the period spent under 2 (exclusive) days to 90 days (included) and changed the main query for your needs.WITH rns AS ( SELECT tbl.* , ROW_NUMBER() OVER ( PARTITION BY cls ORDER BY actiondate ) AS rn FROM tbl ) , grp_starts AS ( SELECT cls , actiondate , rn , grp_start FROM rns MODEL PARTITION BY (cls) DIMENSION BY (rn) MEASURES ( actiondate , actiondate grp_start ) RULES ( grp_start [ANY ] ORDER BY rn = CASE WHEN grp_start [CV() - 1] IS PRESENT AND grp_start [CV()] - grp_start [CV() - 1] <= 90 THEN grp_start [CV() - 1] ELSE actiondate [CV()] END ) ) SELECT cls , actiondate FROM grp_starts WHERE actiondate = grp_start ORDER BY cls , actiondate;
Subqueries, up and including grp_starts, assign each line to a group. This is great if you want to GROUP BY this expression. If all what you want is to show the group identifier, then you might be able to shorten the above query a bit.
Class is a keyword, so it's not a good choice for a column name. I called this cls of the column instead.
July 30 is exactly 90 days after May 1. If you want a group to contain all less than 90 days after the date of the first in the group, then it seems that you would want a new group to begin July 30. The output you posted, it's what I expected, if a group is all less than or equal to 90 days after the beginning of the group.
-
SQL in group by
I have a table called test and the data in the table from below
Amount of SNO startdate enddate
10000 10/1 / 2001 9/30/2003 10.34
10000 10/1 / 2003 9/30/2005 15.89
10000 10/1 / 2005 9/30/2007 15.89
10000 10/1 / 2007 9/30/2013 10.34
10000 10/1 / 11/30/2013 2013, 15.89
10000 12/1 / 2013 12/31/4 000 27
I want the results in this way. expected results of this way
Amount of SNO startdate enddate
10000 10/1 / 2001 9/30/2003 10.34
10000 10/1 / 2003 9/30/2007 15.89
10000 10/1 / 2007 9/30/2013 10.34
10000 10/1 / 11/30/2013 2013, 15.89
10000 12/1 / 2013 12/31/4 000 27
Under query not giving good results. Please help any with the query
Select min (startdate), max (endate), sno, amount
of the TEST
Group by sno, amount
Hello
Why do you want 2 separate lines of output for amount = 15.89? Why you want to combine the lines where startdate is in 2033 and in 2005, but not the line where startdate is in 2013? Is this because another line, with a different amount, comes between 2005 and 2013, and but no rank with a different amount comes between 2003 and 2005?
If so, maybe you want something like:
WITH got_diff AS
(
SELECT sno, startdate, enddate, amount
, ROW_NUMBER () OVER (PARTITION BY sno - just guessing
ORDER BY startdate
)
-ROW_NUMBER () OVER (PARTITION BY sno, amount
ORDER BY startdate
) AS diff
OF the test
)
SELECT sno
(MIN) startdate) AS first_startdate
MAX (enddate) AS last_enddate
quantity
OF got_diff
GROUP BY sno, amount, diff
ORDER BY sno, first_startdate
;
For an explanation of the technique used here fixed difference, see
-
I have a request in the form
Select Group, bill, table Qty.
and output as below
Advantage Bill Qty
=== ===== =======
x 1 XXX 50
x 1 XXX 50
x 1 XXX 60
Y1 YYY 23
Y1 YYY 50
but I want to output as below
Advantage Bill Qty
=== ===== =======
x 1 XXX 50
50
60
Y1 YYY 23
50
How can I get this help please
Hello
If you use SQL * more then you simply need to use 'BREAK' (and an ORDER BY.)
WE BREAK bill WE left
SELECT Bill, part Qty.
FROM MyTable
ORDER BY 1, 2, 3
;
Best regards
Bruno Vroman.
-
Oracle PL/SQL using decode/case in the subquery
SELECT DISTINCT E.ACC_ID,
P.EFFECTIVE_DATE,
P.UPDATE_DATE
(CHOOSE abc.def table in case RECON_ACC.partition_id is 1 for this acc_id
abc100.def if RECON_ACC.partition_id 2 table for this acc_id
) p
JOIN IN-HOUSE
(select * from RECON_ACC where id_partition is NOT NULL) e
ON P.ACC_ID = E.ACC_ID
AND TRUNC (P.UPDATE_DATE) > = December 31, 13 '
AND TRUNC (P.UPDATE_DATE) < = 2 January 14 ';Can someone help me to do a SQL query to do this?
Hello
If you had only a table of def, then it would be easy
. You need only a simple join, like this:
SELECT ud.acc_id, ud.effective_date, ud.update_date
Of unified_def ud
JOIN recon_acc r ON ra.partition_id = ud.partition_id
AND ra.acc_id = ud.acc_id - can - be
;
The only problem here is that you have 2 separate tables def. Assistance from the UNION, you can create a result set that makes these 2 tables appear as 1; for example:
WITH date_range AS
(
SELECT TO_DATE (November 1, 2013 00:00:00 ', 'HH24:MI:SS of Mon-DD-YYYY') AS first_date
, TO_DATE (November 20, 2013 23:59:59 ', 'HH24:MI:SS of Mon-DD-YYYY') AS last_date
OF the double
)
unified_def AS
(
SELECT d1.effective_date, d1.update_date, d1.acc_id
1 AS id_partition
To abc_def d1
JOIN date_range r1 WE d1.update_date BETWEEN r1.first_date
AND r1.last_date
UNION ALL
SELECT d2.effective_date, d2.update_date, d2.acc_id
2 AS id_partition,
Abc100_def D2
JOIN date_range r2 WE d2.update_date BETWEEN r2.first_date
AND r2.last_date
)
SELECT ud.acc_id, ud.effective_date, ud.update_date
Of unified_def ud
JOIN recon_acc r ON ra.partition_id = ud.partition_id
AND ra.acc_id = ud.acc_id - can - be
;
I called def small tables abc_def and abc100_def, just so I wouldn't have to create new patterns. Replace your real names when you run this.
I added the subquery date_range as a means of conveying the date settings. You can do something else, bind variable may be.
I guess just in between the recon_acc and other tables join conditions. Do you still need recon_acc at all with this problem?
Once more, this problem only occurs because you have 2 separate tables def. Why you store your data in a way that creates problems? Is there a reason why you can't use only 1 table unified_def?
-
-SQL - GROUP BY clause: fields of nonaggregate mandate
Hello
I study data (especially data recovery) and found something interesting.
When you use an aggregate function in the SELECT clause, it is mandatory to have all fields that are not aggregated in the SELECT clause to be there in the GROUP BY clause.
For example,.
SELECT dept_no, Salary
The EMPLOYEE
GROUP BY dept_no;
The SQL above works fine.
But what happens if the user forgets the dept_no in the GROUP BY clause or the clause GROUP BY itself is missing?
Certainly, it is a mistake.
Why this error is not handled by the database. I mean, the database must be smart/pretty smart to add the GROUP BY clause by itself. So let's assume that, if I miss the GROUP BY clause or miss a field no aggregated from the SELECT clause when I get at least an aggregate function on a field with at least a no aggregated field in the SELECT clause, the database should check the GROUP BY clause at compile time and add mandate missed the fields in the GROUP BY clause.
Example,
SQL1:_
SELECT dept_no, Salary
The EMPLOYEE
GROUP BY dept_no;
SQL2:_
SELECT dept_no, Salary
The EMPLOYEE;
Here, the SQL1 and SQL2, both should give me same output without error.
I can't understand why this is handled?Hello
998478 wrote:
... If we mix the aggregated and non-aggregated values, then there must be a GROUP BY clause that contains all non-aggregated values. Why this is handled by the database/compiler itself?It IS managed by the compiler itself. The compiler manages to trigger an error. The compiler has no way of knowing if you want to remove something from the SELECT clause, or add something to the GROUP BY clause, or not to use the aggregate functions or use several aggregate functions, or a combination of the above. If the compiler re-writes your code and none of these things done automatically, it would be wrong more often that he was right, and you would (rightly) complain about his behavior.
For example, it is clearly wrong:
SELECT deptno , job , SUM (sal) FROM scott.emp GROUP BY deptno ;
What is the right way to fix it?
1. remove something from the SELECT clause
SELECT deptno , SUM (sal) FROM scott.emp GROUP BY deptno ;
2. add something to the GROUP BY clause
SELECT deptno , job , SUM (sal) FROM scott.emp GROUP BY deptno , job ;
3. do not use aggregate functions
SELECT deptno , job , sal FROM scott.emp ;
4. use several aggregate functions
SELECT deptno , MIN (job) , SUM (sal) FROM scott.emp GROUP BY deptno ;
What are all the options, either. For example, the correct solution would be to use analytical functions instead of aggregate functions.
How can anyone tell which of them is right? They all have the right answer for some problem.Moreover, by saying that everying in the SELECT clause must be an aggregate or in the GROUP BY clause is a bit oversimplified.
Fuller, here's the ABC of GROUP BY:
When you use a GROUP BY clause or in an aggregate function, then all in the SELECT clause must be:
(A) a ggregate function,
(B) one of the expressions "group By."
(C) adding to C, or
(D) something that Depends on the foregoing. (For example, if you "GROUP BY TRUNC (dt)", you can SELECT "TO_CHAR (TRUNC (dt), 'Mon - DD')").Published by: Frank Kulash on April 13, 2013 13:44
Additional code examples. -
Need a query sql for obtaining the planned op, please notify--General Employee table -- Version: oracle 11g Rel 2 create table employee (ID varchar2(100),first_name varcha2(200),last_name varchar2(200),start_date date,end_date date,city varchar2(100)) select listagg(First_name,',') within group (order by first_name) from employee group by city ; op: 1. XXX,YYy,ZZZ 2. AAA,SSS Excepted OP: 1. 1.XXX,2.YYY,3.ZZZ 2. 1.AAA,2.SSS
Thanks in advance
Carole Kumar
Edited by: 876377 may 1, 2012 22:04876377 wrote:
--General Employee table -- Version: oracle 11g Rel 2 select listagg(First_name,',') within group (order by first_name) from employee; op: 1. XXX,YYy,ZZZ 2. AAA,SSS Excepted OP: 1. 1.XXX,2.YYY,3.ZZZ 2. 1.AAA,2.SSS
Need a query sql for obtaining the planned op, please notify
Thanks in advance
Carole KumarTry to use
row_number over (partition by ID_COLUMN order by first_name) as RN
And then
listagg( rn || first_name)
Since you don't validate your description of table we do not know what are the columns, etc... I guessed that the ID column is named ID_COLUMN, adjust accordingly.
-
SQL grouping and summing impossible?
I want to create a sql query to summarize some data, but I'm starting to think that it is impossible with sql only. The data I have is of the following form:
The data are classified by TRAN_DT and by TRAN_RS. THA grouping and summing data based on tran_rs, but only when it changes. If in the table above, I don't want to see all the first 3 records but only one value DEBT the sum of these 3 i.e. 100 + 150 + 250 = 500. If the table above after grouping would be similar to that below:TRAN_DT TRAN_RS DEBT CRED 10-Jan 701 100 0 20-Jan 701 150 0 21-Jan 701 250 0 22-Jan 705 0 500 23-Jan 571 100 0 24-Jan 571 50 0 25-Jan 701 50 0 26-Jan 701 20 0 27-Jan 705 0 300
The TRAN_DT is the last value of the record sum. I undestand that the tran_dt are not selectable. What I've tried so far is the following query:TRAN_DT TRAN_RS DEBT CRED 21-Jan 701 500 0 22-Jan 705 0 500 24-Jan 571 150 0 26-Jan 701 70 0 27-Jan 705 0 300
Is it still possible with only sql, any thoughts?select tran_dt, tran_rs, sum(debt)over(partition by tran_rs order by tran_dt rows unbounded preceding), sum(cred)over(partition by tran_rs order by tran_dt rows unbounded preceding) from that_table
The report that I am creating in BI Publisher.Maybe it is possible to group the data in the model and my question here?The Re: tutorial method Tabibitosan by Aketi Jyuuzou me would be very useful here:
with sample_data as (select to_date('10/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 100 debt, 0 cred from dual union all select to_date('20/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 150 debt, 0 cred from dual union all select to_date('21/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 250 debt, 0 cred from dual union all select to_date('22/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 500 cred from dual union all select to_date('23/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 100 debt, 0 cred from dual union all select to_date('24/01/2012', 'dd/mm/yyyy') tran_dt, 571 tran_rs, 50 debt, 0 cred from dual union all select to_date('25/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 50 debt, 0 cred from dual union all select to_date('26/01/2012', 'dd/mm/yyyy') tran_dt, 701 tran_rs, 20 debt, 0 cred from dual union all select to_date('27/01/2012', 'dd/mm/yyyy') tran_dt, 705 tran_rs, 0 debt, 300 cred from dual), tabibitosan as (select tran_dt, tran_rs, debt, cred, dense_rank() over (order by tran_dt, tran_rs, rownum) - dense_rank() over (partition by tran_rs order by tran_dt, rownum) grp from sample_data) select max(tran_dt), tran_rs, sum(debt) debt, sum(cred) cred from tabibitosan group by tran_rs, grp order by 1, 2; TRAN_DT TRAN_RS DEBT CRED ---------- ---------- ---------- ---------- 21/01/2012 701 500 0 22/01/2012 705 0 500 24/01/2012 571 150 0 26/01/2012 701 70 0 27/01/2012 705 0 300
ETA: I added the rownum in analytical functions to take account of the "encounters" where a tran_rs has multiple entries for the same tran_dt. If you have some other unique key to use (for example, the primary key of the table) then I would use instead.
Published by: Boneist on February 17, 2012 11:41
-
Hi friends
I have a table like this
I want to make a report asname birth_date john 01.01.1990 sue 02.05.2000 abraham 04.07.1998 ..........
How can I do this?age sum 0-17 30 18-24 25 15-34 32 35-44 14 45-54 8 55+ 4
The geometrico elsewhere perhaps is just an internal query where you define groups of age, and then outside the Group:
with dummydata as ( select 'AAA' name, to_date('01.01.1995','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.01.1990','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.11.1975','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.11.1980','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.01.1991','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.01.1969','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.01.1970','dd.mm.yyyy') birth_date from dual union all select 'AAA' name, to_date('01.01.1960','dd.mm.yyyy') birth_date from dual ) select age_group , count(*) from( select case when trunc( months_between(sysdate, birth_date) / 12 ) <= 17 then '0-17' when trunc( months_between(sysdate, birth_date) / 12 ) <= 24 then '18-24' when trunc( months_between(sysdate, birth_date) / 12 ) <= 34 then '25-34' when trunc( months_between(sysdate, birth_date) / 12 ) <= 44 then '35-44' when trunc( months_between(sysdate, birth_date) / 12 ) <= 54 then '45-54' -- and so on... else 'n/a' end age_group from dummydata ) group by age_group order by age_group ; AGE_GROUP COUNT(*) --------- ---------- 0-17 1 18-24 2 25-34 1 35-44 3 45-54 1 5 rows selected.
-
Hello
I'm using LabVIEW 8.0 and I'm trying to create a subgroup of experts which shows the façade of a subvi according to the choice made by the user via the menu drop down Enum. I wired the Enum to a case structure and apply a coding identical to each structure of individual cases with the exception of course change the source of sub - vi. However, you receive an error "The VI isn't in a State compatible with this operation." I have managed to make a single sub - vi work within a subgroup of experts using a true/false case structure and the same exact coding. However, now that I use an Enum box connected to a structure of the case, I get this error. I enclose my diagram as well. Could someone help me as to where it is that I am wrong?
Thanks in advance!
If your VI runs already, when you try to run it? Which returns the error you get.
Check the State of VI before trying to run or make an error handling.
-
DECLARE
V_TOT_MARKS NUMBER CONSTANT: = 500;
V_MKS_OBT NUMBER: = 0;
NUMBER of V_FPAPER: = NVL (& FIRST_PAPER, 0);
NUMBER of V_SPAPER: = NVL (& SECOND_PAPER, 0);
NUMBER of V_TPAPER: = NVL (& THIRD_PAPER, 0);
V_4PAPER NUMBER: = NVL (& FOURTH_PAPER, 0);
V_5PAPER NUMBER: = NVL (& FIFTH_PAPER, 0);
NUMBER OF V_PER (5.2): = 0;
V_GRADE VARCHAR2 (05);
V_RES VARCHAR2 (30);
BEGIN
V_RES: = V_FPAPER > = 40 AND V_SPAPER > = 40 AND V_TPAPER > = 40 AND V_4PAPER > = 40 AND V_5PAPER > = 40;
V_MKS_OBT: = V_FPAPER + V_SPAPER + V_TPAPER + V_4PAPER + V_5PAPER;
V_PER: = TRUNC ((V_MKS_OBT / V_TOT_MARKS) * 100, 2);
V_RES: = CASE WHEN V_PER BETWEEN 95 AND 100 THEN V_GRADE 'A-1 '.
WHEN V_PER BETWEEN 85.00 AND 94.99 THEN V_GRADE "A".
WHEN V_PER BETWEEN 75.00 AND 84.99 CAN V_GRADE 'B '.
WHEN V_PER BETWEEN 40.00 AND 74.99 V_GRADE THEN 'C '.
WHEN V_PER BETWEEN 30,00 AND 39.99 V_GRADE CAN "FAIL".
END OF ANOTHER "FAILED."
DBMS_OUTPUT. PUT_LINE (' FIRST BRANDS OF PAPER: = ' |) V_FPAPER | (' / 100');
DBMS_OUTPUT. PUT_LINE ("SECOND BRANDS PAPER: = ' |") V_SPAPER | (' / 100');
DBMS_OUTPUT. PUT_LINE (' THIRD PAPER BRANDS: = ' |) V_TPAPER | (' / 100');
DBMS_OUTPUT. PUT_LINE (' FOURTH BOOK BRANDS: = ' |) V_4PAPER | (' / 100');
DBMS_OUTPUT. PUT_LINE ("BOOK MARKS FIFTH: = ' |") V_5PAPER | (' / 100');
DBMS_OUTPUT. PUT_LINE ('TOTAL POINTS: = ' |) V_MKS_OBT | » /'|| V_TOT_MARKS);
DBMS_OUTPUT. PUT_LINE (' % / GRADE: = ' |) V_PER: ' / ' | V_GRADE);
END;
/
A lot of mistakes in your block, I think that you are looking for something like this:
DECLARE V_TOT_MARKS CONSTANT NUMBER := 500; V_MKS_OBT NUMBER := 0; V_FPAPER NUMBER := NVL ( &FIRST_PAPER, 0 ) ; V_SPAPER NUMBER := NVL ( &SECOND_PAPER, 0 ) ; V_TPAPER NUMBER := NVL ( &THIRD_PAPER, 0 ) ; V_4PAPER NUMBER := NVL ( &FOURTH_PAPER, 0 ) ; V_5PAPER NUMBER := NVL ( &FIFTH_PAPER, 0 ) ; V_PER NUMBER ( 5, 2 ) := 0; V_GRADE VARCHAR2 ( 05 ) ; V_RES VARCHAR2 ( 30 ) ; BEGIN V_MKS_OBT := V_FPAPER + V_SPAPER + V_TPAPER + V_4PAPER + V_5PAPER; V_PER := TRUNC ( ( V_MKS_OBT / V_TOT_MARKS ) * 100, 2 ) ; select case WHEN V_PER BETWEEN 95 AND 100 THEN 'A-1' WHEN V_PER BETWEEN 85.00 AND 94.99 THEN 'A' WHEN V_PER BETWEEN 75.00 AND 84.99 THEN 'B' WHEN V_PER BETWEEN 40.00 AND 74.99 THEN 'C' WHEN V_PER BETWEEN 30.00 AND 39.99 THEN 'FAIL' ELSE 'FAIL' end INTO V_GRADE from dual; DBMS_OUTPUT.PUT_LINE ( 'FIRST PAPER MARKS := '||V_FPAPER||'/100' ) ; DBMS_OUTPUT.PUT_LINE ( 'SECOND PAPER MARKS := '||V_SPAPER||'/100' ) ; DBMS_OUTPUT.PUT_LINE ( 'THIRD PAPER MARKS := '||V_TPAPER||'/100' ) ; DBMS_OUTPUT.PUT_LINE ( 'FOURTH PAPER MARKS := '||V_4PAPER||'/100' ) ; DBMS_OUTPUT.PUT_LINE ( 'FIFTH PAPER MARKS := '||V_5PAPER||'/100' ) ; DBMS_OUTPUT.PUT_LINE ( 'TOTAL MARKS := '||V_MKS_OBT||'/'||V_TOT_MARKS ) ; DBMS_OUTPUT.PUT_LINE ( 'PER% / GRADE := '||V_PER||' / '||V_GRADE ) ; END; /
-
Hi all
Thanks for your time in advance. I have the situation where I need quantities of different domain group but one of the fields must be reorganized. Not sure how best to explain this in the text. But, please see the example below. With the help of 11g
Expected result:SELECT 80 AMOUNT, 111 ACCOUNT_NUM, 123 CHECK_NUM FROM DUAL UNION ALL SELECT 100 AMOUNT, 111 ACCOUNT_NUM, 124 CHECK_NUM FROM DUAL UNION ALL SELECT 120 AMOUNT, 111 ACCOUNT_NUM, 125 CHECK_NUM FROM DUAL
Thank youAMOUNT ACCOUNT_NUM CHECK_NUM 200 111 123,124,125
I guess you wanted to say 300 was the amount in your example? (80 + 100 + 120)?
I have 10g, but here's one way:
with t as ( SELECT 80 AMOUNT, 111 ACCOUNT_NUM, 123 CHECK_NUM FROM DUAL UNION ALL SELECT 100 AMOUNT, 111 ACCOUNT_NUM, 124 CHECK_NUM FROM DUAL UNION ALL SELECT 120 AMOUNT, 111 ACCOUNT_NUM, 125 CHECK_NUM FROM DUAL ) select sum(amount), account_num, RTRIM(XMLAGG(XMLELEMENT(c, check_num||',') order by check_num).EXTRACT ('//text()'), ',') check_nums from t group by account_num SUM(AMOUNT) ACCOUNT_NUM CHECK_NUMS 300 111 123,124,125
Maybe you are looking for
-
Fingerprints does not work in Google Chrome - solution?
My fingerprint sensor does not work in a browser other than IE. I want to use it as well with Google Chrome (some Applications work better there). No work around?
-
HP Stream 13: Beware of the upgrade to Windows 10
I have just told me that in Hp Support the HP 13 Stream is not recommended for the upgrade to Windows 10 due to the lack of space and memory for this new operating system. I thought it would be interesting as well as alarming to soemwhat. This occurr
-
Improved HP software and now get the error scanning
IMac bought 3 months ago (Mountain Lion 10.8.2). Operations to scan and print on my Officejet Pro 8600 (N911A) worked well. Also knitted for laptop with Windows XP. I had never installed any software for this printer on either computer graphics si
-
Convert SVGImage in JPEGEncodedImage
Hello Is there a way to convert SVGImage to JPEGEncodedImage. Please provide some piece of code. Thanks in advance. Sridhar
-
BlackBerry Smartphones task icon
I tried to move my task icon when he suddenly disappeared and I can't get back. It seems that my tasks disappeared, even when I select Show tasks in calendar. Any ideas? See you soon