Want to select query based on sample data.
My Oracle Version
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
I am creating inventory valuation report using FIFO method . here sample data .
create table tranx(
TRXTYPE varchar2(10) ,ITEM_CODE varchar2(10), RATE number,qty number
insert into tranx values('IN' ,'14042014', 457.2 ,10);
insert into tranx values('OUT','14042014', 0, 10);
insert into tranx values('IN','14042014', 458.1, 35);
insert into tranx values('OUT','14042014', 0, 11);
insert into tranx values('OUT','14042014', 0, 6);
insert into tranx values('IN',' 14042014', 457.2 ,10);
insert into tranx values('OUT',' 14042014', 0, 3);
insert into tranx values('OUT',' 14042014', 0, 4);
insert into tranx values('IN',' 14042014', 457.2, 20);
insert into tranx values('OUT',' 14042014', 0, 5);
insert into tranx values('OUT',' 14042014', 0, 9);
insert into tranx values('OUT',' 14042014', 0, 8);
current output
TRXTYPE ITEM_CODE RATE QTY
IN 14042014 457.2 10
OUT 14042014 0 10
IN 14042014 458.1 35
OUT 14042014 0 11
OUT 14042014 0 6
IN 14042014 457.2 10
OUT 14042014 0 3
OUT 14042014 0 4
IN 14042014 457.2 20
OUT 14042014 0 5
OUT 14042014 0 9
OUT 14042014 0 8above data populate based on first in first out . but out rate is not comes from that query. suppose fist 10 qty are OUT its rate same as IN. but when qty start out from 35 rate will be 458.1 till all 35 qty will not out. like out qty 11,6,3,4,5,9 now total are 38 .
when qty 9 will out the rate are 6 qty rate 458.1 and other 3 out rate of 457.2 means total value of 9 out qty value is 4120.20 .
Now 35 qty is completed and after that rate will continue with 457.2 till 10 qty not completed.
I think you understand my detail if not please tell me .
thanks
i am waiting your reply.
As SomeoneElse mentioned, there is no row order in relational tables, so you can't tell which row is first and which is next unless ORDER BY is used. So I added column SEQ to your table:
SQL> select *
2 from tranx
3 /
TRXTYPE ITEM_CODE RATE QTY SEQ
IN 14042014 457.2 10 1
OUT 14042014 0 10 2
IN 14042014 458.1 35 3
OUT 14042014 0 11 4
OUT 14042014 0 6 5
IN 14042014 457.2 10 6
OUT 14042014 0 3 7
OUT 14042014 0 4 8
IN 14042014 457.2 20 9
OUT 14042014 0 5 10
OUT 14042014 0 9 11
TRXTYPE ITEM_CODE RATE QTY SEQ
OUT 14042014 0 8 12
12 rows selected.
SQL> Now it can be solved. Your task requires either hierarchical or recursive solution. Below is recursive solution using MODEL:
with t as (
select tranx.*,
case trxtype
when 'IN' then row_number() over(partition by item_code,trxtype order by seq)
else 0
end rn_in,
case trxtype
when 'OUT' then row_number() over(partition by item_code,trxtype order by seq)
else 0
end rn_out,
count(case trxtype when 'OUT' then 1 end) over(partition by item_code) cnt_out
from tranx
select trxtype,
item_code,
rate,
qty
from t
model
partition by(item_code)
dimension by(rn_in,rn_out)
measures(trxtype,rate,qty,qty qty_remainder,cnt_out,1 current_in,seq)
rules iterate(10) until(iteration_number + 1 = cnt_out[0,1])
rate[0,iteration_number + 1] = rate[current_in[1,0],0],
qty_remainder[0,iteration_number + 1] = case sign(qty_remainder[0,cv() - 1])
when 1 then qty_remainder[0,cv() - 1] - qty[0,cv()]
else qty[current_in[1,0],0] - qty[0,cv()] + nvl(qty_remainder[0,cv() - 1],0)
end,
current_in[1,0] = case sign(qty_remainder[0,iteration_number + 1])
when 1 then current_in[1,0]
else current_in[1,0] + 1
end
order by seq
TRXTYPE ITEM_CODE RATE QTY
IN 14042014 457.2 10
OUT 14042014 457.2 10
IN 14042014 458.1 35
OUT 14042014 458.1 11
OUT 14042014 458.1 6
IN 14042014 457.2 10
OUT 14042014 458.1 3
OUT 14042014 458.1 4
IN 14042014 457.2 20
OUT 14042014 458.1 5
OUT 14042014 458.1 9
TRXTYPE ITEM_CODE RATE QTY
OUT 14042014 457.2 8
12 rows selected.
SQL> SY.
Similar Messages
-
Select Query Based on date condition
Hi ,
Is it Possible.
i want to run select query based on date condition.
Eg...
if the date between 01-jan-01 and 01-jan-05 then
select * from table1;
if the date between 02-jan-05 and 01-jan-08 then
select * from table2;
Becaz i have data in 2 diffrent tables , based on the date condition i wnt to run the select statement to diffrent tables.
i dont want plsql here Just SQL needed.
thanks,
-R
Edited by: infant_raj on May 5, 2009 11:48 PMHelo Kanish,
this is not the one i was asking..
wht i mean was .
i use bind variable to get date while running the select statement , once i get the date then i want to choose any one of the table to run select query.
EG..
select col1,col2 from table1 where date between only if 01-jan-01 and 01-jan-05;
select col1,col2 from table2 where date between only if 02-jan-05 and 01-jan-08;
Run any one of the two . not all
thanks,
_raj -
I want to query based on a date range. I wrote the following EJB QL:
"select object(a) from ActionItems as a where a.dueDate
between ?1 and ?2"
But when I deployed the application to Oracle's EJB server, I got the following error:
Auto-deploying biogen.jar (No previous deployment found)... Invalid type for expression (a.dueDate BETWEEN ?1 AND ?2)
EJB QL statement : 'select object(a) from ActionItems as a where a.dueDate between ?1 and ?2'
EJB QL method : public abstract java.util.Collection ActionItemsLocalHome.findByDueDateRange(java.sql.Date,java.sql.Date) throws javax.ejb.FinderException
at com.sun.ejb.ejbql.parser.EjbQLParser.parse EjbQLParser.java:218)
at com.sun.ejb.ejbql.EjbQLDriver.parse(EjbQLDriver.java:86)
at com.sun.ejb.sqlgen.SQLGenerator.generateSQLForEjbQLQueries(SQLGenerator.java:704)
It works fine for a number range, but does not work for a date range.
Any one has an idea on how to write a correct EJB QL for a query based on date range?
Thanks.
JingzhiRe-posted. Please help!
-
SELECT QUERY BASED ON SECONDARY INDEX
Hi all,
CAN ANYONE TELL ME HOW TO WRITE SELECT QUERY BASED ON SECONDARY INDEX.
IN WHAT WAY DOES IT IMPROVE PERFORMANCE.
i KNOW WHEN CREATING SECONDARY INDEX I NEED TO GIVE AN INDEX NO -iT SHOULD BE ANY NUMBER RIGHT?
I HAVE TO LIST ALL PRIMARY KEYS FIRST AND THEN THE FIELD FOR WHICH I AM CREATING SECONDARY INDEX RIGHT?
LETS SAY I HAVE 2 PRIMARY KEYS AND I WANT TO CREATE SEONDARY INDEX FOR 2 FIELDS THEN
I NEED TO CREATE A SEPERTE SECONDARY INDEX FOR EACH ONE OF THOSE FIELDS OR ONE SHOULD BE ENOUGH
pLS LET ME KNOW IF IAM WRONGHI,
If you cannot use the primary index to determine the result set because, for example, none of the primary index fields occur in the WHERE or HAVINGclauses, the system searches through the entire table (full table scan). For this case, you can create secondary indexes, which can restrict the number of table entries searched to form the result set.
You create secondary indexes using the ABAP Dictionary. There you can create its columns and define it as UNIQUE. However, you should not create secondary indexes to cover all possible combinations of fields.
Only create one if you select data by fields that are not contained in another index, and the performance is very poor. Furthermore, you should only create secondary indexes for database tables from which you mainly read, since indexes have to be updated each time the database table is changed. <b>As a rule, secondary indexes should not contain more than four fields</b>, <b>and you should not have more than five indexes for a single database table</b>.
<b>What to Keep in Mind for Secondary Indexes:</b>
http://help.sap.com/saphelp_nw04s/helpdata/en/cf/21eb2d446011d189700000e8322d00/content.htm
http://www.sap-img.com/abap/quick-note-on-design-of-secondary-database-indexes-and-logical-databases.htm
Regards
Sudheer -
Select query based LOV for tabular form attributes
Hi HTMLDB Team,
Congrats u all for the new release of HTMLDB in htmldb.oracle.com.
I badly need a solution of the below problem.
Say , i have table called user_col_comments now i want to display the table_name ,column_name and comments in a tabular form.I displayed only one row for the tabular form.Now i create a select query based LOV for attribute table_name where i got all table_name in drop down list.
Point is that after selecting any data from drop down table list i want to get the corresponding fields to be populated in column_name attributes.How can i wrote the select query based LOV for the attibute column_name.
I wrote it as 'select column_name d,column_name r from user_col_comments where table_name=:TABLE_NAME Its not working?
Similarly i want same thing to display for comments after matching both table_name and column_name.
Any solution to get rid of those problem will be highly appreciable...
If u need my htmldb.oracle.com userid and password to solve the problem i will sure let u email it.
Cheers,
Emanhi rchalton,
can u plz little bit more clearer .I know hopefully u can imagine the problem and may u guide me thru proper way.....One think i understand that there must be multiple process and submit but "only when..." that u have said i cant understand that part........
U are welcome to give me proper solution.....
Thanks for the reply ....atleast one can pay hid to me.....
Cheers,
Eman -
In oracle rac, If user query a select query and in processing data is fetched but in the duration of fetching the particular node is evicted then how failover to another node internally?
The query is re-issued as a flashback query and the client process can continue to fetch from the cursor. This is described in the Net Services Administrators Guide, the section on Transparent Application Failover.
-
Select query based on userinput
Hi Folks...
I posted this in another forum, but no reply,so I am posting it here too. I am trying to make a select query based on a user input. Earlier I was having probelms making a select query and printing out the result in the stack trace,with the advice given, I managed to solve that. I am now modifying that code to make a query based on user input, however it's not working, following is the code:
String userId = request.getRemoteUser(); String sql = "SELECT hoursused FROM sysuser WHERE iduser = ?"; try { Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, userId); ResultSet srs = preparedStatement.executeQuery(sql); while(srs.next()) { String hoursused = srs.getString("hoursused"); System.out.println("The hours used are " + hoursused); } } catch (SQLException e) { e.printStackTrace(); } }
stack trace seems to suggest I may have a probelm with this statement:
"SELECT hoursused FROM sysuser WHERE iduser = ?";
I am not sure how to rectify this, I hope someone can advise, thanks.Hi jschell..
Thanks for responding, I appreciate it. The problem has been solved. No 'sysuser' is not a reserved word in Mysql. Based on the advice given in another forum and also from this site, I managed to solve the problem. I only made one change. This:
ResultSet srs = preparedStatement.executeQuery(sql); was changed to this:
ResultSet srs = preparedStatement.executeQuery(); Thanks. -
Select query based on joining of tables from different database is taking too long
Hi Team,
Select query on table with millions of records is taking very long time. It took 50mins, the below query is joining on multiple table from two databases DB1.dbo.Table1 contains 100 million records and also Table3 and Table4 (of different database) might
contain close to 1million records.
Select T1.*
From DB1.dbo.Table1 T1
Join DB1.dbo.Table2 T2 on RTRIM(T1.Col3) = RTRIM(T2.Col3)
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
Join Table3 U on CA.Col2 = U.Col2 AND RTRIM(T2.Col2) = U.Col3
Where U.Col4 NOT IN ('A1', 'A2', 'A3', 'A4', 'A5', 'A6','A7','A8','A9')
And (T1.flg IS NULL OR T1.flg = 'N')
And LTRIM(RTRIM(T2.Col2)) NOT IN ('B1','B2')
How can i improve the performance of this query. Actual thing is update the data in Db1.dbo.table1 based on the conditions but if the select is taking close to 1hr then update will take hours together. Indexes already implemented on all the tables.
Thanks,
Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users.Thanks.
Join Table4 CA on RTRIM(T1.Col1) + T2.Col2 = CA.Col1
This join is not working with spaces if Rtrim is not used.
Thanks! Eshwar.
Please don't forget to Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful. It will helpful to other users. -
Regading Select query for Inspection planed date
Hi,
I am working on one object.I need to select query to get the inspection plan valid from date(PLKO-DATUV).Logic is Find the group counter(QALS-PLNNR),Group counter(QALS_PLNAL) based on materail(SELMATNR) & BATCH(CHARG) and get the inspection plan valid from date(PLKO-DATUV).How to write the select performance wise.Please help me.
Regards,
Sujanhi Kat,
do this way ..
data : v_date like ekko-aedat value '20080424'.
select ebeln
bsart
aedat
ernam
lifnr
from ekko
into table it_ekko
where
*ebeln eq ekko-ebeln
and aedat gt v_date
and bsart 'AN'
and ( frggr is null or
frgsx is null or
frgke eq '1' ) .
Regards,
santosh -
Dynamic Select List based on TextField data
Hi,
I like to dynamically display the select list based on the value in the textfield, the data in the textfield is of character type.
ThanksHello,
Well as you now know HTML based select lists don't work like that, that widget is called a combo box and there will be built in combo boxes in APEX 3.0, it's a fairly complex dhtml widget.
What you might want to do is provide a text item next to your select box and an Add New Value Option in your select list.
Carl -
Need help in writing a select query to pull required data from 3 tables.
Hi,
I have three tables EmpIDs,EmpRoles and LatestRoles. I need to write a select Query to get roles of all employees present in EmpIDs table by referring EmpRoles and LatestRoles.
The condition is first look into table EmpRoles and if it has more than one entry for a particular Employee ID than only need to get the Role from LatestRoles other wise consider
the role from EmpRoles .
Sample Script:
Create Table #EmpIDs
(EmplID int )
Create Table #EmpRoles
(EMPID int,Designation varchar(50))
Create Table #LatestRoles
EmpID int,
Designation varchar(50)
Insert into #EmpIDs values (1),(2),(3)
Insert into #EmpRoles values (1,'Role1'),(2,'Role1'),(2,'Role2'),(3,'Role1')
Insert into #LatestRoles values (2,'Role2')
Employee ID 2 is having two roles defined in EmpRoles so for EmpID 2 need to fetch Role from LatestRoles table and for
remaining ID's need to fetch from EmpRoles .
My Final Output of select query should be like below.
EmpID Role
1 Role1
2 Role2
3 Role1
Please help.
MohanMohan,
Can you check if this answers your requirement:
Create Table #EmpIDs
(EmplID int )
Create Table #EmpRoles
(EMPID int,Designation varchar(50))
Create Table #LatestRoles
EmpID int,
Designation varchar(50)
Insert into #EmpIDs values (1)
Insert into #EmpIDs values (2)
Insert into #EmpIDs values (3)
Insert into #EmpRoles values (1,'Role1')
Insert into #EmpRoles values (2,'Role2')
Insert into #EmpRoles values (2,'Role1')
Insert into #EmpRoles values (3,'Role1')
Insert into #LatestRoles values (2,'Role2')
--Method 1
select e.EmplID,MIN(ISNULL(l.Designation,r.Designation)) as Designation
from #empids e
left join #emproles r on e.emplID=r.EmpID
left join #latestRoles l on e.emplID=l.EmpID
group by e.EmplID
--Method 2
;with cte
as
select distinct e.EmplID,r.Designation,count(*) over(partition by e.emplID) cnt
from #empids e
left join #emproles r on e.emplID=r.EmpID
select emplID,Designation
from cte
where cnt=1
UNION ALL
select a.EmplID,l.Designation
from
(select distinct EmplID from cte where cnt>1) a
join #Latestroles l on a.EmplID=l.EmpID
order by emplID
Thanks,
Jay
<If the post was helpful mark as 'Helpful' and if the post answered your query, mark as 'Answered'> -
Add a flag column in the Query based on Cancel date
hi Guys,
I have a Characterstic object 0canceldate and it gets the cancellation date of the sales order. Based on that date i need to add a column in the report like if 0canceldate has ahappened or populate then show Yes or No in the coumn like a flag.
Can anybody help me how i can do that?SELECT d.NAME
FROM
(SELECT jta.NAME
FROM apps.jtf_terr_values_all jtva,
apps.jtf_terr_qual_all jtqa,
apps.jtf_terr_all jta ,
apps.hz_party_sites ps,
apps.hz_locations hl
WHERE jtva.terr_qual_id = jtqa.terr_qual_id
AND jtqa.terr_id = jta.terr_id
AND jtva.low_value_char = hl.postal_code
AND jtqa.org_id = jtva.org_id
AND jta.org_id = jtqa.org_id
AND ps.location_id = hl.location_id
AND hl.country in( 'CA', 'US')) D;
UNION
SELECT jta.NAME
FROM apps.jtf_terr_values_all jtva,
apps.jtf_terr_qual_all jtqa,
apps.jtf_terr_all jta,
apps.hz_party_sites ps,
apps.hz_locations hl
WHERE jtva.terr_qual_id = jtqa.terr_qual_id
AND jtqa.terr_id = jta.terr_id
AND ps.location_id = hl.location_id
AND jtqa.org_id = jtva.org_id
AND jta.org_id = jtqa.org_id
AND hl.country = 'US'
AND hl.postal_code BETWEEN jtva.low_value_char AND jtva.high_value_char
)d; -
Can ditto select files based on modification date?
Hi. I'm trying to use ditto to make backups. I would like to have it select only files modified after a certain date. Is there a way to do this?
I noticed the bom (bill of materials) option... can that be used for this purpose somehow? Not quite sure how best to create a BOM that includes only files based on modification date.
Any suggestions? Thanks
Ericditto doesn't provide a way to select only files modified after a certain date -- but find does; see the "-newer" and "-newerXY" options. Using find, you can generate a BOM file listing files that have been modified after a certain date.
Then you can pass that BOM file to ditto.
Powerbook G4 1GHz Mac OS X (10.3.9) -
Select query case insensitive for data type VARG
Hi Experts,
I am having trouble in retrieving the results from a table using select query.
I have a table (Users) as below
Name (VARG) Number(INTEGER)
Murthy 0001
murthy 0002
when I am querying the table with select query as -
select * from Users where Name = 'Murthy'
this query is returning only one record which matches with capital letter of 'M', though both the record names are same.
I have seen in IBM forum to make the select query as case insensitive using the UPPER key word, I have tried this as below -
select * from Users where UPPER(Name) = 'MURTHY'
But this query is not working in my case...
My DB2 version is 8.1.5
Can any one please help in fixing this problem...
Thanks in Advance,
MurthyHi Murthy,
your query is the right to one to select both records. I don't see why it doesn't work.
Are you sure you posted the query that you have executed ?
What is the result of this query ?
But i don't really know how an VARG (Varying-length graphic string) will work. It could be that the UPPER-Function will work in another way as it works on VARCHAR.
regards
Kay -
A/r Invoice report query based on posting date selection criteria
Hi experts,
I am trying to write a query to get the A/r invoice report including Docnum, Docdate, cardname, project, linetotal, taxcode, taxtotal.
and i tried the below query
SELECT T0.[DocNum], T0.[DocDate], T0.[CardCode], T0.[CardName],T1.[LineTotal] FROM OINV T0 INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry WHERE T0.[DocDate] >=[%0] AND T0.[DocDate] <=[%1]
In this query ,
1) i am unable to sum up the linetotal( total before tax)
2) also unable to fetch the sum of taxamount.
3) its more important , the above query will me docnum , when i try to open that doc, it opens the 2007 docments. whereas the other details are correct like docdate, amount and all.
example: from date: 1/04/2011 to date: 28/07/2011
Docnum Docdate cardname linetotal.... etc
2 1/04/11 XYZ 5000
when i click the 2 (docnum) it opens me the 04/04/2007 documents.
how to limit the doc within the periods given?
thanks in advance
DwarakHi Rahul/Gordon,
thanks for your query's .
additionally, i want this query without the A/r invoices whichever having credit memo as target doc.
and
i have selection criteria as T0.[U_Sec_Category] = '[%2]' and this has 3 values like 1)CIM 2)BIN 3)DMP
among these CIM should be seen only only user1 and BIN & DMP only by user2.
can u plz get me this
thanks,
Dwarak
Maybe you are looking for
-
My iBook can't startup on its hard disk. It all started I noticed considerably slow reaction times to input and Safari kept crashing when I tried loading web pages. I restarted by holding down the power button. The iBook booted to the terminal. I res
-
Recording number of qRFC entries from SOLMAN
Hi All, We have qRFC's inbetween ECC, SCM, XI. We have configured CCMS agent,EWA,SMD etc on these system. Is there any way we can measure number of qRFC entris (inbound/outbound) per hour from these system with help of Solution Manager. Is there any
-
Hi folks: Am I able to use the webutil functionalities not in forms module but within stored procedure? If not, can I have any other way (using Forms 10g Rel 10.1.2.x) to stored a pdf file into my DB just as easy as I do with webutil function (webuti
-
Unable to install Adobe flash 11
I have just tried to install Adobe flash 11 but it is not installing. I get the 'run' screen- click on it and nothing happens. I had upload it before and it install correctly but all the videos and you tube videos did not download and kept freezing
-
Dear All, I am trying to get 4 fields in browser report. Cleared items till date,Cleared items till last month,open items till date,open items till last month. I am getting Cleared items till date by using "0clear_date" clearing date infoob