Help with this query
eid manager
1 111
2 1
3 2
4 2
5 2
6 3
7 5
this is a table and i have to prduce output in the form which employee reports
to which manager
like
row one: 7>5>2>1
row two 6>3>2>1
here in row one 7 reports to 5 which report to 2 which reports to 1.
query should be only in sql no pl/sql.
For a good discussion on this topic, see:
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:2165134263446
Similar Messages
-
Please need help with this query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_datePerhaps something like this...
select id, create_date, loanid, rate, pays, gracetime, emailtosend, first_name, last_name, user_id
from (
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id,
max(create_date) over (partition by a.user_id, a.loadid) as max_create_date
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
where create_date = max_create_date
order by create_date -
Can any one help with this query please
I have a table something as below
Things_t
Things Characteristic Char Value
Item 1 Colour Red
Item 1 Packaging
Item 2 Shape Square
Item 2 Brand Spunk
Now i want to reterive an item with none of its char values as Null. Using the query “ select distinct things from things_t where char value is Null ” will fetch the item 1 also together with item 2. i want to fetch a record from thing for which none of the char values are Null such as Item 2. Can you please help me with this query.Try this:
WITH t AS
(SELECT 1 item_id, 17436 chr_id, 14225034 chr_val_id FROM dual UNION
SELECT 1 item_id, 39 chr_id, 14276173 chr_val_id FROM dual UNION
SELECT 1 item_id, 17774 chr_id, NULL chr_val_id FROM dual UNION
SELECT 1 item_id, 265 chr_id, 20502978 chr_val_id FROM dual UNION
SELECT 1 item_id, 16978 chr_id, 797233 chr_val_id FROM dual UNION
SELECT 1 item_id, 13092 chr_id, 5666917 chr_val_id FROM dual UNION
SELECT 1 item_id, 15228 chr_id, 1209758 chr_val_id FROM dual UNION
SELECT 2 item_id, 112 chr_id, 12705342 chr_val_id FROM dual UNION
SELECT 2 item_id, 6945 chr_id, NULL chr_val_id FROM dual UNION
SELECT 2 item_id, 70 chr_id, 12597376 chr_val_id FROM dual UNION
SELECT 2 item_id, 16832 chr_id, NULL chr_val_id FROM dual UNION
SELECT 2 item_id, 7886 chr_id, 9588619 chr_val_id FROM dual UNION
SELECT 2 item_id, 6986 chr_id, 2659351 chr_val_id FROM dual UNION
SELECT 3 item_id, 9531 chr_id, 8910943 chr_val_id FROM dual UNION
SELECT 3 item_id, 9798 chr_id, 8717531 chr_val_id FROM dual UNION
SELECT 3 item_id, 17446 chr_id, 12266441 chr_val_id FROM dual UNION
SELECT 3 item_id, 4830 chr_id, 13683090 chr_val_id FROM dual UNION
SELECT 3 item_id, 9518 chr_id, 834772 chr_val_id FROM dual UNION
SELECT 3 item_id, 11031 chr_id, 20233753 chr_val_id FROM dual UNION
SELECT 3 item_id, 12564 chr_id, 2282478 chr_val_id FROM dual)
SELECT DISTINCT item_id
FROM t
MINUS
SELECT DISTINCT item_id
FROM t
WHERE chr_val_id IS NULLOr this:
SELECT item_id
FROM (SELECT item_id,
MIN(NVL(chr_val_id, -1)) min_chr_val_id
FROM t
GROUP BY item_id)
WHERE min_chr_val_id != -1Edited by: lee200 on Oct 15, 2012 9:22 AM -
Hello
If I run the following query I correctly get a sum of the invoices for a particular BP for Jan 09
SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] - T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
However I need the value of credit notes to be reflected in the figure so I amended the query to be:
SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] - T2.[VatSum])ELSE 0 END) - SUM(CASE WHEN T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] - T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
When I run this query the output is incorrect ................ Is the fault to do with my JOINS?
ThanksHi Just to clarify
This query, which just totals any invoices for a BP for January, correctly returns a value of £9660.44 :
SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN T2.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T2.[DocTotal] - T2.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN OINV T2 ON T0.CardCode = T2.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
This next query, which just totals any credit notes for a BP in January, correctly returns a value of £567.73 :
SELECT T1.[SlpName], T0.[CardCode], SUM(CASE WHEN T3.[DocDate] BETWEEN '20090101' AND '20090131' THEN (T3.[DocTotal] - T3.[VatSum]) ELSE 0 END) AS 'JAN 09' FROM OCRD T0 INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode INNER JOIN ORIN T3 ON T0.CardCode = T3.CardCode WHERE T0.[CardCode] = 'KEY065' GROUP BY T0.[CardCode], T1.[SlpName]
I want a query which return a value of 9660.44 minus 567.73 (ie £9092.71)
My original effort at this returned -£82,608.65 !!!!!!!!!
and Gordon's amended version returned -£40,281.74 !!!!!
Thanks for your patience
Steve -
Hi,
I am using SQL Server 2008 Enterprise edition 64 bit on Windows serer 2008 enterprise edition 64 bit.
The below query works fine. I am trying to rewrite the code without using the sub query. In the end i should get the same results for both the query which we are going to rewrite and the below mentioned query.
Can any help me please.
SELECT
t1.DOCUMENT_NO,
RTRIM(CAST(t1."ENVIRONMENT_CD" AS VARCHAR(5))) + '*' + RTRIM(CAST(t1."ORDER_NO" AS VARCHAR(25)))
+
CASE WHEN (SELECT COUNT(DISTINCT S1.TEST_REPORTING_MATERIAL_SID)
FROM dbo.TBLTEST S1 ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
WHERE S1.TEST_REPORTING_MATERIAL_SID > 0
AND S1.COMPANY_CD = T1.COMPANY_CD AND S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
AND S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
AND S1.ORDER_NO = T1.ORDER_NO) > 1 THEN
RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS VARCHAR(10)))
ELSE
END as sum_key,
t1.SALES_ANALYSIS_CD
FROM
dbo.TBLTEST_ALL T1
WHERE
t1.TIME_SID >= 20001001 ;
I tried to use a left outer Join , it did not work out. I tried to take the subquery and assign it to a variable and use in the above mentioned query. But i am not getting the same results.
I don't want to use the above query so i am planning to rewrite the code.
Thank You,I doubt that the query you posted works fine, because:
FROM dbo.TBLTEST S1 ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
This is a syntax error. I will have to assume that your inteded query reads:
SELECT t1.DOCUMENT_NO,
rtrim(cast(t1."ENVIRONMENT_CD" AS varchar(5))) + '*' +
rtrim(cast(t1."ORDER_NO" AS varchar(25))) +
CASE WHEN (SELECT COUNT(DISTINCT S1.TEST_REPORTING_MATERIAL_SID)
FROM dbo.TBLTEST S1
WHERE S1.DOCUMENT_NO = T1.DOCUMENT_NO
AND S1.TEST_REPORTING_MATERIAL_SID > 0
AND S1.COMPANY_CD = T1.COMPANY_CD
AND S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
AND S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
AND S1.ORDER_NO = T1.ORDER_NO) > 1 THEN
RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS VARCHAR(10)))
ELSE '*-'
END as sum_key, t1.SALES_ANALYSIS_CD
FROM dbo.TBLTEST_ALL T1
WHERE t1.TIME_SID >= 20001001
Here is a query with a left join. Whether it is actually better than the one you have I am not sure.
SELECT t1.DOCUMENT_NO,
rtrim(cast(t1."ENVIRONMENT_CD" AS varchar(5))) + '*' +
rtrim(cast(t1."ORDER_NO" AS varchar(25))) +
CASE WHEN S1."EXISTS" IS NOT NULL
THEN RTRIM(CAST(t1."TEST_REPORTING_MATERIAL_SID" AS varchar(10)))
ELSE '*-'
END as sum_key, t1.SALES_ANALYSIS_CD
FROM dbo.TBLTEST_ALL T1
LEFT JOIN (SELECT 1 AS "EXISTS"
FROM dbo.TBLTEST
WHERE TEST_REPORTING_MATERIAL_SID > 0
GROUP BY DOCUMENT_NO, COMPANY_CD, INVOICE_SEQ_NO,
DOCUMENT_ITEM_NO, ORDER_NO
HAVING COUNT(DISTINCT TEST_REPORTING_MATERIAL_SID) > 1) AS S1
ON S1.DOCUMENT_NO = T1.DOCUMENT_NO
AND S1.COMPANY_CD = T1.COMPANY_CD
AND S1.INVOICE_SEQ_NO = T1.INVOICE_SEQ_NO
AND S1.DOCUMENT_ITEM_NO = T1.DOCUMENT_ITEM_NO
AND S1.ORDER_NO = T1.ORDER_NO
WHERE t1.TIME_SID >= 20001001
Erland Sommarskog, SQL Server MVP, [email protected] -
SQL I need help with this query Please help
List the names of all the products whose weight unit measure is “Gram”. Order the list by product name. Do not use JOINS, but use the IN clause with a sub-query.
select Name
from UnitMeasure
where Name= 'Gram'
order by Name
I did this, but it seem that the requirement is different.As a guess:
Select Name from Product
where UnitMeasure in (Select Name from unitmeasure where name = 'Gram')
Andy Tauber
Data Architect
The Vancouver Clinic
Website | LinkedIn
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click
"Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread. -
Can anyone help with this query
create table customer(
customer_email varchar(100) not null
,cust_id int not null
,constraint pk__customer primary key clustered(cust_id)
Please help ASAP
create table purchase (
purchase_id int not null identity(1,1)
,cust_id int not null references customer(cust_id)
,product_name varchar(200) not null
,amount int not null
,constraint pk__purchase primary key clustered (purchase_id)
Write a query that returns one row per customer with the following columns. Don't forget about customers with no purchases.
customer_email
qty of purchase
sum of purchase amountHomework deadline looming?
This will work in versions of Oracle from 9 up, and in all versions of Sybase/ SQL Server (which you seem to wnat based on the CREATE TABLE syntax).
SELECT customer_email, COUNT(*) number_purchaes, SUM(amount) total_purchases
FROM customer c
LEFT OUTER JOIN purchases p ON c.cust_id = p.cust_id
GROUP BY customer_emailTTFN
John -
Physician/Researcher needs help with this query
Folks,
Thank you in advance for looking at my problem. You are helping to save lives and make this world a better place.
I am trying to find out the earliest date patients took steroid. I have:
select patientid, visitnumber, steroid_start_date from treatments where steroid_start_date is not null and trim(steroid_start_date) != '/'
order by patientid, substr(steroid_start_date, -4)
I then have:
patientid visitnumber steroid_start_date(this is a string column, not date or time, and it has so many junk in it, like /2003 , /)
4 3 03/2004
4 2 01/01/2005
10 2 08/01/2005
10 1 05/01/2002
What I need is:
4 3 03/2004
10 1 05/01/2002
I am not good with group by... having.... max.... How can I limit to one patient per row and this row is their earliest visit?
Thank you very much in advance. You are helping to save lives.Here it is ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production
Elapsed: 00:00:00.53
satyaki>
satyaki>
satyaki>with t
2 as
3 (
4 select 4 patientid, 3 visitnumber, '03/2004' steroid_start_date from dual
5 union all
6 select 4, 2, '01/01/2005' from dual
7 union all
8 select 10, 2, '08/01/2005' from dual
9 union all
10 select 10, 1, '05/01/2002' from dual
11 )
12 select patientid,
13 visitnumber,
14 steroid_start_date
15 from (
16 select k.*,
17 row_number() over(partition by patientid order by to_date(substr(steroid_start_date,-4),'YYYY')) rn
18 from t k
19 order by patientid
20 )
21 where rn=1;
PATIENTID VISITNUMBER STEROID_ST
4 3 03/2004
10 1 05/01/2002
Elapsed: 00:00:00.11
satyaki>Regards.
Satyaki De. -
Please help with this query!
Hi All,
I have this this table:
Term Grade
term_A A
term_A A
term_A B
term_A B
term_B D
term_B F
term_B F
term_C C
How do I display so that it appears this way?
term_A A 2
term_A B 2
term_B D 1
term_B F 2
term_C C 1
Thank you. Appreciate all the help!Please read about [url http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/functions001.htm#sthref962]aggregate functions in the manual.
Regards,
Rob. -
Hi there,
These are the sample values
declare @table table
Name varchar(50),
flag int
insert into @table
values('Matt', 0),
('George', 0),
('George', 1),
('Lucas', 0),
('Jerome', 0),
('Jerome', 1)
I want to select out where George and Jerome where flag = 0 but leave the records from the same names where flag = 1. All others that only have flag = 0, should stay. So only the names that repeat and have both flag, flag = 0 zero should be selected out.
Thanks for your help.So desired output is ????
George 0
Jerome 0
declare @table table
Name varchar(50),
flag int
insert into @table
values('Matt', 0),
('George', 0),
('George', 1),
('Lucas', 0),
('Jerome', 0),
('Jerome', 1)
select name,min(flag) flag,count(*) from @table
group by name
having count(*)>1
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Please, need help with a query
Hi !
Please need help with this query:
Needs to show (in cases of more than 1 loan offer) the latest create_date one time.
Meaning, In cases the USER_ID, LOAN_ID, CREATE_DATE are the same need to show only the latest, Thanks!!!
select distinct a.id,
create_date,
a.loanid,
a.rate,
a.pays,
a.gracetime,
a.emailtosend,
d.first_name,
d.last_name,
a.user_id
from CLAL_LOANCALC_DET a,
loan_Calculator b,
bv_user_profile c,
bv_mr_user_profile d
where b.loanid = a.loanid
and c.NET_USER_NO = a.resp_id
and d.user_id = c.user_id
and a.is_partner is null
and a.create_date between
TO_DATE('6/3/2008 01:00:00', 'DD/MM/YY HH24:MI:SS') and
TO_DATE('27/3/2008 23:59:00', 'DD/MM/YY HH24:MI:SS')
order by a.create_dateTake a look on the syntax :
max(...) keep (dense_rank last order by ...)
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions056.htm#i1000901
Nicolas. -
I created a blogging tool for my students to use as I teach
them internet safety and cyber citizenship. I am no CF master, but
I dabble a little bit here and there. I need some help with this
query. It is running extremely slow, which means I have probably
created some horrendous loop in this query. If any one out there
has a better solution for this query, I and my middle school
students would be extremely grateful.
Here's what I would like it to do. I have two tables, one for
the blog messages and another for comments. The comments are linked
to their respective blog messages through a common database field.
When someone clicks on a link to read a student's blog, a query
runs which pulls all of the blog messages for that user, the
comments, and it also counts the number of comments entries for
each message so that I can place a total # of comments under each
blog message.Not sure why you have this like this: (Select
count(commentid) from comments where comments.blogid = blog.blogid)
or this twice: blog.blogusersid = #fname#
You need to make sure that the comments.blogid and
blog.blogid fields are indexed. Does this query work any faster?
<cfquery Name="Myblog" datasource="blog">
SELECT b.blogid, b.btitle, b.bcontent, b.bdate,
b.blogusersid, b.fname, b.lname, b.blogpict, b.pictlocation,
b.userid, c.commentid, c.blogid, b.lastupdated, COUNT(c.commentid)
AS cc
FROM blog AS b
INNER JOIN comments AS c ON c.blogid = b.blogid
WHERE b.blogusersid = #fname#
GROUP BY b.blogid, b.btitle, b.bcontent, b.bdate,
b.blogusersid, b.fname, b.lname, b.blogpict, b.pictlocation,
b.userid, c.commentid, c.blogid, b.lastupdated
ORDER BY b.bdate
</cfquery>
..... but I'm not sure that you will be getting the comment
count that you want with either query.
Phil -
I have a macbook Pro i7 mid november 2010. I am wondering if i can exchange my notebook with the latest one. Can anyone help me out with this query please.
You can sell your existing computer using eBay, Craigslist or the venue of your choice. You could then use the proceeds to purchase a new computer.
-
Help with aging query. not sure how to acomplish this???
Guys im a bit stuck usually i have a slight idea how to even start my query but for this particular one I just dont even know where to start to tackle
i have a table like so.
create table TRRACCD (
TRRACCD_GRNT_CODE varchar2(6),
TRRACCD_TRANS_dATE date,
TRRACCD_AMOUNT number(17,2),
trraccd_detail_code varchar2(6))with data like so
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('15-FEB-10', 'DD-MM-RR'),19764.77,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('12-MAR-10', 'DD-MM-RR'),2054.29,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('20-APR-10', 'DD-MM-RR'),4111.46,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('14-MAY-10', 'DD-MM-RR'),2570.1,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('10-JUN-10', 'DD-MM-RR'),2176.16,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('12-JUL-10', 'DD-MM-RR'),4756.29,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('13-AUG-10', 'DD-MM-RR'),28500.62,'GCSH');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('16-AUG-10', 'DD-MM-RR'),3602.3,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('17-SEP-10', 'DD-MM-RR'),3995.79,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('11-OCT-10', 'DD-MM-RR'),3413.6,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('11-NOV-10', 'DD-MM-RR'),2119.86,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('10-DEC-10', 'DD-MM-RR'),5905.52,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('10-DEC-10', 'DD-MM-RR'),12008.21,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('11-JAN-11', 'DD-MM-RR'),-10574.05,'GBIL');
INSERT INTO TRRACCD (TRRACCD_GRNT_CODE, TRRACCD_TRANS_DATE, TRRACCD_AMOUNT, TRRACCD_DETAIL_CODE) VALUES ('400364',to_date('11-JAN-11', 'DD-MM-RR'),20241.9,'GBIL');however what i need to do is end up with an aging for the items summarize so the expected output should be something like
tbrraccd_grnt_Code 30-60 60-90 over90
400364 20,241.90 2119.86 7,370.09
if your confused about how i arrived at the 7370.90 is because i have to subtract entries with GCSH on them and i also need to take
entries that are - and applied them to the oldest period if the balance of the period is positive. im inclined to say that i can do this with sql but the only other way i can think of is using plsql?... any suggestions or solutions would be greatly appreciated.
the closest that i have gotten to acomplishing this query without pl is this. i know is lame but i need help.
SELECT TRRACCD_GRNT_CODE,
TRRACCD_TRANS_dATE,
TRRACCD_AMOUNT,
trraccd_detail_code,
round(sysdate - TRRACCD_TRANS_dATE) "total days"
--sum(case when round(sysdate - TRRACCD_TRANS_dATE)>190 then "greater then 190"end )
FROM TRRACCD
where trraccd_grnt_Code = '400364'
ORDER BY TRRACCD_GRNT_CODE,
TRRACCD_TRANS_dATE,
trraccd_detail_code;Hi, Miguel,
I should have mentioned this before: I'm assuming SYSDATE is some point on January 22, 2011, after 00:00. I don't think this has caused any confusion so far, but if the discussion continues for a day or two, let's be clear about that.
I think I see what you want regarding the rows where trraccd+detailcode='GSCH'. If we completly ignored all the GSCH rows, we would get this output:
TRRACC 0-29 30-59 60-89 90-over
400364 ...
400404 19904.97 6625.27 4757.01 67618.68You want GSCH amounts to cancel other amounts in the last column only until that column reaches 0 ; the remaining GSCH amount will cancel amounts in to "60-89" column, again, only until that amount reaches 0 . If there is anything left over, it will cancel amounts in the "30-59" column, and if there is still some left over, the "0-29" column.
To be specific, trraccd_grnt_code=400404 has a total of 72475.69 from GSCH rows. That counts against the "90-over" total.
67618.68 - 72475.69 = -4757.01; you want to dispaly 0 in the "90-over" column, and apply the remaining 4757.01 to the next column to the left.
That column contains 4757.01, which happens to be the exact maount left over from the previous subtraction, so you want to diesplay 0 in the "60-89" column.
The remaining columns to the left should contain their regular totals.
Here's one way to do that:
WITH got_col_num AS
SELECT trraccd_grnt_code
, CASE
WHEN trraccd_detail_code != 'GCSH' THEN trraccd_amount
END AS net_amount
, SUM ( CASE
WHEN trraccd_detail_code = 'GCSH' THEN trraccd_amount
END
) OVER (PARTITION BY trraccd_grnt_code)
AS gsch_total
, CASE
WHEN trraccd_amount < 0 THEN 4
WHEN trraccd_trans_date > SYSDATE - 30 THEN 1
WHEN trraccd_trans_date > SYSDATE - 60 THEN 2
WHEN trraccd_trans_date > SYSDATE - 90 THEN 3
ELSE 4
END AS col_num
FROM trraccd
WHERE trraccd_trans_date <= SYSDATE
, got_sum AS
SELECT trraccd_grnt_code
, col_num
, LEAST ( SUM (net_amount)
, GREATEST ( 0
, SUM (SUM (net_amount)) OVER ( PARTITION BY trraccd_grnt_code
ORDER BY col_num DESC
- gsch_total
) AS total
FROM got_col_num
GROUP BY trraccd_grnt_code
, col_num
, gsch_total
SELECT trraccd_grnt_code
, SUM (CASE WHEN col_num = 1 THEN total END) AS "0-29"
, SUM (CASE WHEN col_num = 2 THEN total END) AS "30-59"
, SUM (CASE WHEN col_num = 3 THEN total END) AS "60-89"
, SUM (CASE WHEN col_num = 4 THEN total END) AS "90-over"
FROM got_sum
GROUP BY trraccd_grnt_code
ORDER BY trraccd_grnt_code
;Whenever you want to understand a query like this, run just the first sub-query (in this case, got_col_num) and make sure you understand what it's doing. When you do, add one more sub-query, and make sure you understand what it's doing.
Add morte columns to the display to help you understand it better. For example, when you get to the point where you're trying to understand the 2nd sub-query above, got_sum, you might run this query:
WITH got_col_num AS
SELECT trraccd_grnt_code
, CASE
WHEN trraccd_detail_code != 'GCSH' THEN trraccd_amount
END AS net_amount
, SUM ( CASE
WHEN trraccd_detail_code = 'GCSH' THEN trraccd_amount
END
) OVER (PARTITION BY trraccd_grnt_code)
AS gsch_total
, CASE
WHEN trraccd_amount < 0 THEN 4
WHEN trraccd_trans_date > SYSDATE - 30 THEN 1
WHEN trraccd_trans_date > SYSDATE - 60 THEN 2
WHEN trraccd_trans_date > SYSDATE - 90 THEN 3
ELSE 4
END AS col_num
FROM trraccd
WHERE trraccd_trans_date <= SYSDATE
, got_sum AS
SELECT trraccd_grnt_code
, col_num
, gsch_total
, SUM (net_amount) AS period_sum
, SUM (SUM (net_amount)) OVER ( PARTITION BY trraccd_grnt_code
ORDER BY col_num DESC
) AS running_sum
, LEAST ( SUM (net_amount)
, GREATEST ( 0
, SUM (SUM (net_amount)) OVER ( PARTITION BY trraccd_grnt_code
ORDER BY col_num DESC
- gsch_total
) AS total
FROM got_col_num
GROUP BY trraccd_grnt_code
, col_num
, gsch_total
SELECT *
FROM got_sum
ORDER BY trraccd_grnt_code
, col_num
;which produces this output:
TRRACC COL_NUM GSCH_TOTAL PERIOD_SUM RUNNING_SUM TOTAL
400364 1 28500.62 20241.9 76146.2 20241.9
400364 2 28500.62 17913.73 55904.3 17913.73
400364 3 28500.62 2119.86 37990.57 2119.86
400364 4 28500.62 35870.71 35870.71 7370.09
400404 1 72475.69 19904.97 99005.93 19904.97
400404 2 72475.69 6625.27 79100.96 6625.27
400404 3 72475.69 4857.01 72475.69 0
400404 4 72475.69 67618.68 67618.68 0The total column in that sub-query is very complicated, so break it down some. You can see that it is based on an analytic SUM whose argumenet is an aggregate SUM, all wrapped in LEAST and GREATEST. That can be very hard to underrstand, so break it down. Display the aggregate SUM by itself (period_sum) and also display the analytic SUM, without the GREATEST and LEAST.
mlov83 wrote:
where can i get more information about these types of querys?Can you be more specific? What types of queries do you mean? Complicated queries? Pivots? GROUP BY?
... I'm always leary of using a solution that i dont understand Excellent reaction!
would you mind expalining it to me a little.Again, could you be more specific? I don't mind explaining things to you, but I don't want to explain parts you already understand. If something is new, you'll want to read the SQL Language manual, or other sources, anyway, so it's more efficient to do that first, and then post specific questions on this forum.
An example of a more specific question is: "When I looked up "WITH clause" in the SQL Language reference, I found this page:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#sthref9758
but I don't understand where it says ...
The query above says ... Is that what the manual is talking about when it says ...?"
Another example of a good question is
"The query you posted says ... I don't understand what that's doing. I looked for ... and ... but that didn't seem to have anything to do with this query. What should I look for?
The Oracle 10 SQL Language manual is indexed pretty poorly. If you can't find something, it may not be your fault. -
select CASE WHEN TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'DDMMYYYY HH24:MM:SS'),13,2))>0 AND TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)) <14
THEN TO_DATE(CONCAT(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)||'00',SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MI:SS'),16)||'00'),'DD-MM-YYYY HH24:MI:SS') end
from table;
i have written this query.whats wrong with this query..........
the error is "literal does not match format string"
Reegards soumenWhy does your date_format loose, ununify and not fix ?
And what is your exact requirement?
>>
CASE WHEN
TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'DDMMYYYY HH24:MM:SS'),13,2))>0
AND TO_NUMBER(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)) <14
<<
This is
CASE WHEN TO_CHAR(START_TIME_TIMESTAMP,'MM') between '01' and '13'
>>
THEN TO_DATE(CONCAT(SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MM:SS'),13,2)
||'00',
SUBSTR(TO_CHAR(START_TIME_TIMESTAMP,'YYYYMMDD HH24:MI:SS'),16)||'00'),
'DD-MM-YYYY HH24:MI:SS')
<<
This is
TO_DATE(
TO_CHAR(START_TIME_TIMESTAMP,'MM"00"SS"00"),
'DD-MM-YYYY HH24:MI:SS')
Obviously, format is not matching !
SQL> select to_char(sysdate,'MM"00"SS"00') from dual;
TO_CHAR(
06004900
SQL> select to_date('06004900','DD-MM-YYYY HH24:MI:SS') from dual;
select to_date('06004900','DD-MM-YYYY HH24:MI:SS') from dual
ERROR at line 1:
ORA-01861: literal does not match format string
Maybe you are looking for
-
SOLUTION: Windows Vista + iPod Shuffle 2nd Gen
My original question on this topic is closed, but I finally fixed the problem and wanted to share. The problem was: Windows Vista Basic desktop machine and iTunes would recognize a Video iPod, but not recognize an iPod second generation Shuffle The s
-
What earlier version of FCP will work with the intel-3processor?
what earlier version of FCP will work with the intel-3 processor?
-
My new ipod touch goes dim after switching on for about a minute?
my new ipod touch goes dim after switching on for about a minute?
-
Conditional Coloring for Server 2005 Reports
Hey Guys, I'm trying to use the Layout version in my Microsoft Visual Studio for Reports - and have no idea how to get my rows to follow a singular color format depending on a columns' input. For example, I would like the entire row to be yellow if t
-
Removing Prefix of members/alias in report Scripts
I need to remove the pre fixes of all members/alias of a couple of dimensions through report script.( no need to modify the outline) For example Costcenters start with C_ , which needs to be removed when the report is created( I know everybody is goi