Need a help in a query
Table structure is given below …
Test_role
SC_CODE VAR_NAME FLD_NAME
SC1 Var1 NBR1
SC1 Var2 NBR2
SC2 Var3 NBR1
SC3 Var1 NBR1
SC3 Var4 NBR2
Test_role table contains some code and associated variable name like Var1, Var2, Var3 etc . The third column FLD_NAME contains the column name of another table(Test_data) which contains actual value.
You can see here the field name NBR1 may not contain a particular variable always. It can vary for different SC_CODE .
Test_data
CUST_ID SC_CODE NBR1 NBR2
1 SC1 10 20
2 SC2 50
3 SC3 70 80
4 SC1 101 110
The above table contains CUST_ID which is mapped to SC_CODE of Test_role table.
Here CUST_ID 1 and 2 mapped to same variable SC1 but the actual value for cust_id=1 is (10,20) and for 2 is (101,110)
I need a resulting table like test _result which will take all variable name as column , and populate the data accordingly. Resulting table data is given below.
Test_Result
CUST_ID SC_CODE VAR1 VAR2 VAR3 VAR4
1 SC1 10 20
2 SC2 50
3 SC3 70 80
4 SC1 101 102 Table script is given below ..
TEST_DATA
create table TEST_DATA
CUST_ID NUMBER(10),
SC_CODE VARCHAR2(50),
NBR1 NUMBER(5),
NBR2 NUMBER(5)
insert into TEST_DATA (CUST_ID, SC_CODE, NBR1, NBR2)
values (1, 'SC1', 10, 20);
insert into TEST_DATA (CUST_ID, SC_CODE, NBR1, NBR2)
values (2, 'SC2', 50, null);
insert into TEST_DATA (CUST_ID, SC_CODE, NBR1, NBR2)
values (3, 'SC3', 70, 80);
insert into TEST_DATA (CUST_ID, SC_CODE, NBR1, NBR2)
values (4, 'SC1', 10, null);
TEST_RESULT
create table TEST_RESULT
CUST_ID VARCHAR2(50),
SC_CODE VARCHAR2(50),
VAR1 NUMBER(5),
VAR2 NUMBER(5),
VAR3 NUMBER(5),
VAR4 NUMBER(5)
insert into TEST_RESULT (CUST_ID, SC_CODE, VAR1, VAR2, VAR3, VAR4)
values ('1', 'SC1', 10, 20, null, null);
insert into TEST_RESULT (CUST_ID, SC_CODE, VAR1, VAR2, VAR3, VAR4)
values ('2', 'SC2', null, null, 50, null);
insert into TEST_RESULT (CUST_ID, SC_CODE, VAR1, VAR2, VAR3, VAR4)
values ('3', 'SC3', 70, null, null, 80);
insert into TEST_RESULT (CUST_ID, SC_CODE, VAR1, VAR2, VAR3, VAR4)
values ('4', 'SC1', 101, 110, null, 80);
test_role
create table TEST_ROLE
SC_CODE VARCHAR2(50),
VAR_NAME VARCHAR2(50),
FLD_NAME VARCHAR2(50)
insert into TEST_ROLE (SC_CODE, VAR_NAME, FLD_NAME)
values ('SC1', 'Var1', 'NBR1');
insert into TEST_ROLE (SC_CODE, VAR_NAME, FLD_NAME)
values ('SC1', 'Var2', 'NBR2');
insert into TEST_ROLE (SC_CODE, VAR_NAME, FLD_NAME)
values ('SC2', 'Var3', 'NBR1');
insert into TEST_ROLE (SC_CODE, VAR_NAME, FLD_NAME)
values ('SC3', 'Var1', 'NBR1');
insert into TEST_ROLE (SC_CODE, VAR_NAME, FLD_NAME)
values ('SC3', 'Var4', 'NBR2');
Please help , if it can be done
Hi,
The script below is one example of how to do a pivot:
-- How to Pivot a Result Set (Display Rows as Columns)
-- For Oracle 10, and earlier
-- Actually, this works in any version of Oracle, but the
-- "SELECT ... PIVOT" feature introduced in Oracle 11
-- is better. (See Query 2, below.)
-- This example uses the scott.emp table.
-- Given a query that produces three rows for every department,
-- how can we show the same data in a query that has one row
-- per department, and three separate columns?
-- For example, the query below counts the number of employess
-- in each departent that have one of three given jobs:
PROMPT ========== 0. Simple COUNT ... GROUP BY ==========
SELECT deptno
, job
, COUNT (*) AS cnt
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY deptno
, job;
Output:
DEPTNO JOB CNT
20 CLERK 2
20 MANAGER 1
30 CLERK 1
30 MANAGER 1
10 CLERK 1
10 MANAGER 1
20 ANALYST 2
PROMPT ========== 1. Pivot ==========
SELECT deptno
, COUNT (CASE WHEN job = 'ANALYST' THEN 1 END) AS analyst_cnt
, COUNT (CASE WHEN job = 'CLERK' THEN 1 END) AS clerk_cnt
, COUNT (CASE WHEN job = 'MANAGER' THEN 1 END) AS manager_cnt
FROM scott.emp
WHERE job IN ('ANALYST', 'CLERK', 'MANAGER')
GROUP BY deptno;
-- Output:
DEPTNO ANALYST_CNT CLERK_CNT MANAGER_CNT
30 0 1 1
20 2 2 1
10 0 1 1
-- Explanation
(1) Decide what you want the output to look like.
(E.g. "I want a row for each department,
and columns for deptno, analyst_cnt, clerk_cnt and manager_cnt)
(2) Get a result set where every row identifies which row
and which column of the output will be affected.
In the example above, deptno identifies the row, and
job identifies the column.
Both deptno and job happened to be in the original table.
That is not always the case; sometimes you have to
compute new columns based on the original data.
(3) Use aggregate functions and CASE (or DECODE) to produce
the pivoted columns.
The CASE statement will pick
only the rows of raw data that belong in the column.
If each cell in the output corresponds to (at most)
one row of input, then you can use MIN or MAX as the
aggregate function.
If many rows of input can be reflected in a single cell
of output, then use SUM, COUNT, AVG, STRAGG, or some other
aggregate function.
GROUP BY the column that identifies rows.
PROMPT ========== 2. Oracle 11 PIVOT ==========
WITH e AS
( -- Begin sub-query e to SELECT columns for PIVOT
SELECT deptno
, job
FROM scott.emp
) -- End sub-query e to SELECT columns for PIVOT
SELECT *
FROM e
PIVOT ( COUNT (*)
FOR job IN ( 'ANALYST' AS analyst
, 'CLERK' AS clerk
, 'MANAGER' AS manager
NOTES ON ORACLE 11 PIVOT:
(1) You must use a sub-query to select the raw columns.
An in-line view (not shown) is an example of a sub-query.
(2) GROUP BY is implied for all columns not in the PIVOT clause.
(3) Column aliases are optional.
If "AS analyst" is omitted above, the column will be called 'ANALYST' (single-quotes included).
{code}
Similar Messages
-
Hi,
I need a help in writing an SQL query . I am actually confused how to write a query. Below is the scenario.
CREATE TABLE demand_tmp
( item_id NUMBER,
org_id NUMBER,
order_line_id NUMBER,
quantity NUMBER,
order_type NUMBER
CREATE TABLE order_tmp
( item_id NUMBER,
org_id NUMBER,
order_line_id NUMBER,
open_flag VARCHAR2(10)
INSERT INTO demand_tmp
SELECT 12438,82,821,100,30 FROM dual;
INSERT INTO demand_tmp
SELECT 12438,82,849,350,30 FROM dual;
INSERT INTO demand_tmp
SELECT 12438,82,NULL,150,29 FROM dual;
INSERT INTO demand_tmp
SELECT 12438,82,0,50,-1 FROM dual;
INSERT INTO order_tmp
SELECT 12438,82,821,'Y' FROM dual;
INSERT INTO order_tmp
SELECT 12438,82,849,'N' FROM dual;
Demand_tmp:
Item_id org_id order_line_id quantity order_type
12438 82 821 100 30
12438 82 849 350 30
12438 82 NULL 150 29
12438 82 0 50 -1
Order_tmp :
Item_id org_id order_line_id open_flag
12438 82 821 Y
12438 82 849 N I need to fetch the records from demand_tmp table whose order_line_id is present in order_tmp and having open_flag as 'Y' or if order_type in demand_tmp table is 29.
The below query will give the records whose order line id is present in order_tmp. But, If i need records which are having order_type=29 the below query wont return any records as order_line_id is NULL. If I place outer join I will get other records also (In this example order_type -1 records) . Please help me how can we write a query for this. Expected o/p is below.
Query :
Select item_id,org_id,order_line_id,quantity,order_type,open_flag
from demand_tmp dt , order_tmp ot
where dt.order_line_id = ot.order_line_id
AND dt.item_id=ot.item_id
AND dt.org_id = ot.org_id
AND ot.open_flag = 'Y';
Expected Output :
item_id org_id order_line_id quantity order_type open_flag
12438 82 821 100 30 Y
12438 82 NULL 150 29 NULL Thanks in advance,
Rakesh
Edited by: Venkat Rakesh on Oct 7, 2012 6:32 PM
Edited by: Venkat Rakesh on Oct 7, 2012 8:39 PMHi Rakesh,
the query is not working as you would like ( but IS working as expected ) since your trying to compare null to another value.
Comparing null always results in FALSE, also if you compare null to null. This is because null means undefined.
select 1 from dual where null=null results in no data found.
I would suggest using a non natural key to join the tables.
For example include a column ID in the master table which is filled with a sequence and include that field as a foreign key in the detail table.
This way you can easily join master and detail on ID = ID, and you don't have to worry about null values in this column since it's always filled with data.
Regards,
Bas
btw, using the INNER JOIN and OUTER JOIN syntax in your SQL makes it better readable, since you're separating join conditions from the where clause, just a tip ;) -
Need urgent help with the query - Beginer
Hello - I need help with a query to populate data in a table.Here is the scenario.
Source1
MnthID BranchCod CustID SegCode FXStatus ProfStatus Profit
200712 B1 C1 20 Y Y 100
Source2
MnthID BranchCod CustID ProdCode ProdIndex
200712 B1 C1 12 1
200712 B1 C2 12 0
Destination
MnthID BranchCod SegCode ProdCode CountSegCust CountProdCust ProfitProdCust
Condition and Calculations:
1)Source1 customer are base customers.If Source2 has customers who is not in source1 then that customer's record should not be fetched.
2)SegCode, FX Status, ProfStatus is one variable in destination table. [ SegCode = SegCode+ FXStatus (if FXStatus = Y)+ ProfStatus (if FXStatus = Y) ]
3)CountSegCust = CountCustID Groupby MnthID,BranchCod,SegCode Only.
4)CountProdCust = CountCustID Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
5)ProfitProdCust = Sum of Profit of Customers Groupby MnthID,BranchCod,SegCode,ProdCode (when ProdIndex = 1)
Apologies for bad formatting.
Thanks in advance!!A total guess indeed.
It's not clear whether some aggregation can be done (summing counts of grouped data might cause some customers being counted more than once)
insert into destination
select mnthid,branchcod,segcode,prodcode,countsegcust,countprodcust,profitprodcust
from (select s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end segcode,
s2.prodcode,
count(s1.custid) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
order by null
) countsegcust,
count(case proindex when 1
then custid
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) countprodcust,
sum(case proindex when 1
then profit
end
) over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) profitprodcust,
row_number() over (partition by s1.mnthid,
s1.branchcod,
s1.segcode || case s1.fxstatus when 'Y' then s1.fxstatus || s1.profstatus end
s2.prodcode
order by null
) the_row
from source1 s1,source2 s2
where s1.mnthid = s2.mnthid
and s1.branchcod = s2.branchcod
and s1.custid = s2.custid
where the_row = 1Regards
Etbin -
Need some help with count query
Hello,
I'm terrible @ oracle, so bear with me. I am trying to use the count function with my query but it counts rows from different tables.
I want my query to basically add a column from another table but have it not be a part of the count.
Say, table1 table2 table3. My count is right between the first two tables (Buyers,5).But since in table3 there are duplicate values(or accounts in this case(3), the count multiples by that many (15).
I need it to read Buyers,5,account #. I've tried distinct and a union but to no avail.
Help me please.If I understand you correctly, you want to count the records in table1 and table2 which have a one-to-one relationship, but you need to display additional data from table3 which has a one-to-many relationship. If this is correct, you need to use an in-line view to join table1 and table2 then join that in-line view to table3 to display the data from it. Something along the lines of:
SELECT v.col1, v.col2, v.col3, v.col4, v.cnt, t3.col1 t3col1, t3.col2 t3col2
FROM (SELECT t1.col1, t1.col2, t2.col1 col3, t2.col2 col4, COUNT(*) cnt
FROM table1 t1, table2 t2
WHERE <join conditions between t1 and t2> and
<other conditions if required>
GROUP BY t1.col1, t1.col2, t2.col1, t2.col2) v,
table3 t3
WHERE <join conditions between v and t3>
<other conditions if required>John -
Hello,
I'm new to oracle DB.
Can someone help me with this query please?
I want a query which returns the details of all the DML and DDl queries performed on a database in a particular period of time like for example from that date to today.
Thank you..All changes made to user data or to the data dictionary are recorded in the Oracle redo logs. Therefore, redo logs contain all the necessary information to perform recovery operations. Because redo log data is often kept in archived files, the data is already available.
Actually to do this you got to have DBA privileges.
You have to use oracle LOGMINER
Oracle Corporation provides SQL access to the redo logs through LogMiner, which is part of the Oracle database server. LogMiner presents the information in the redo logs through the V$LOGMNR_CONTENTS fixed view. This view contains historical information about changes made to the database
Please refer the following link to get more details on the same.
http://docs.oracle.com/cd/B10501_01/server.920/a96521/logminer.htm
Hope this helps :) -
In below query, table A has 200,000 records and table B has 10 million records. There are no indexes on this table at this point, how can i improve the query perfromance ?
select *
from A, B
where A.VARCHAR = SUBSTR(B.VARCHAR, 1, 6)
and TO_DATE(A.DT_Varchar, 'YYYYMMDD') = TO_DATE(B.DT_Varchar, 'DD Mon YYYY')
A.VARCHAR & A.DT_Varchar form the uniqueness in Table A
B.VARCHAR & B.DT_Varchar form the uniqueness in Table BHi,
Krishna4Oracle wrote:
In below query, table A has 200,000 records and table B has 10 million records. There are no indexes on this table at this point, how can i improve the query perfromance ?
select *
from A, B
where A.VARCHAR = SUBSTR(B.VARCHAR, 1, 6)
and TO_DATE(A.DT_Varchar, 'YYYYMMDD') = TO_DATE(B.DT_Varchar, 'DD Mon YYYY')
A.VARCHAR & A.DT_Varchar form the uniqueness in Table A
B.VARCHAR & B.DT_Varchar form the uniqueness in Table BDo you need all columns from both tables?
You might want to consider using explicit joins instead, i.e
select a.column1, b.column2 from a
inner join b on
a.varchar = substr(b.varchar,1,6) and
TO_DATE(A.DT_Varchar, 'YYYYMMDD') = TO_DATE(B.DT_Varchar, 'DD Mon YYYY')HtH
Johan -
Need some help formulating a query
I need to formulate a query to list the branch number, branch name, revenue_target, and the revenue_generated for each branch that fails to meet its revenue_target where the revenue_generated by a branch is the sum of the total amount of all orders for salespersons working at that branch. The total amount of an order is given by the sum of the total amount for products and the total amount for installation.
Here are the tables that I am presented with:
Table: EMPLOYEE
PK: employeeID
FK: empBranch references BRANCH
FK: empSupervisor references EMPLOYEE
EMPLOYEEID EMPLNAME EMPFNAME EMPTITLE EMPSTARTDATE EMPBRANCH EMPSALARY EMPSUPERVISOR
e1 Adam Alan CEO 11-JAN-02 b1 600000 -
e2 Bryson Brad branch_manager 01-FEB-03 b2 400000 e1
e3 Clay Cedric branch_manager 21-JUN-01 b3 450000 e1
e4 Day Daisy branch_manager 17-AUG-03 b4 480000 e1
e5 Engle Eva salesperson 01-JAN-04 b2 120000 e2
e6 Falcon Fred salesperson 01-JAN-02 b2 80000 e2
e7 Gandhi Gagan salesperson 01-JAN-03 b3 90000 e3
e8 Hee Hwang salesperson 01-JUN-04 b3 95000 e3
e9 Ingram Irene salesperson 24-SEP-02 b4 110000 e4
e10 Jerome John salesperson 25-AUG-02 b4 75000 e4
Table: BRANCH
PK: branchNumber
FK: branchManager references EMPLOYEE
BRANCHNUMBER BRANCHNAME BRANCHSTREET BRANCHCITY BRANCHSTATE BRANCHZIP REVENUETARGET BRANCHMANAGER
b1 branch1 9700 NW 41 St Miami FL 33178 800000 e1
b2 branch2 8700 SW 24 St Miami FL 33170 600000 e2
b3 branch3 E 200 47 St New York NY 11010 1000000 e3
b4 branch4 300 Park Avenue New York NY 10010 1200000 e4
Table: PRODUCT
PK: productCode
PRODUCTCODE PRODDESCRIPTION PRICE STOCKLEVEL
p1 carpet 40 10000
p2 tile 20 100000
p3 pergo 50 50000
Table: INSTALLATION
PK: installationType
INSTALLTYPE INSTALLDESCRIPTION RATE
i1 carpet installation 40
i2 tile installation 50
i3 pergo installation 60
Table: ORDERS
PK: orderNumber
FK: customerID references CUSTOMER
FK: salesPerson references EMPLOYEE
ORDERNUMBER ORDDATE SALESPERSON CUSTOMERID
o1 12-AUG-07 e5 c1
o2 14-DEC-07 e5 c2
o3 04-NOV-07 e5 c3
o4 15-AUG-07 e5 c4
o5 22-NOV-07 e10 c5
o6 01-JUL-07 e10 c6
o7 12-DEC-07 e6 c6
o8 30-NOV-07 e9 c2
Table: PRODLINE
PK: orderNumber + prodCode
FK: orderNumber references ORDERS
FK: prodCode references PRODUCT
ORDERNUMBER PRODCODE QUANTITY
o1 p1 1000
o1 p2 500
o2 p3 200
o3 p1 600
o3 p3 100
o4 p2 1000
o5 p2 800
Table: INSTLINE
PK: orderNumber + instType
FK: orderNumber references ORDERS
FK: instType references INSTALLATION
ORDERNUMBER INSTTYPE HOURS
o1 i1 20
o1 i2 30
o1 i3 10
o2 i1 10
o2 i2 20
o6 i1 20
o6 i2 10
o7 i3 10
o8 i2 20
I can write the queries to get the TOTAL_AMOUNT_FOR_ORDERS, TOTAL_AMOUNT_FOR_INSTALLATIONS, and BRANCHNUMBER, BRANCHNAME, REVENUETARGET. But I can't seem to put them all together.
Query 1:
SELECT *
FROM
(SELECT o.ORDERNUMBER, SUM(QUANTITY*PRICE) as TOTAL_AMOUNT_FOR_ORDERS
FROM ORDERS o, PRODUCT p, PRODLINE pl
WHERE o.ORDERNUMBER=pl.ORDERNUMBER
AND p.PRODUCTCODE=pl.PRODCODE
GROUP BY o.ORDERNUMBER
ORDER BY o.ORDERNUMBER) TOTORD
FULL JOIN
(SELECT o.ORDERNUMBER, SUM(HOURS*RATE) as TOTAL_AMOUNT_FOR_INSTALLATIONS
FROM ORDERS o, INSTALLATION i, INSTLINE il
WHERE o.ORDERNUMBER=il.ORDERNUMBER
AND i.INSTALLTYPE=il.INSTTYPE
GROUP BY o.ORDERNUMBER
ORDER BY o.ORDERNUMBER) TOTINS
ON
TOTORD.ORDERNUMBER=TOTINS.ORDERNUMBER
Query 2:
SELECT BRANCHNUMBER, BRANCHNAME, REVENUETARGET
FROM ORDERS o, EMPLOYEE e, BRANCH b
WHERE e.EMPLOYEEID=o.SALESPERSON
AND b.BRANCHNUMBER=e.EMPBRANCH
Can someone please assist?Hi,
895231 wrote:
I need to formulate a query to list the branch number, branch name, revenue_target, and the revenue_generated for each branch that fails to meet its revenue_target where the revenue_generated by a branch is the sum of the total amount of all orders for salespersons working at that branch. The total amount of an order is given by the sum of the total amount for products and the total amount for installation.
Here are the tables that I am presented with: ...Whenever you have a problem, please post CREATE TABLE and INSERT statements for your sample data, and the final results you want to get from that data.
Explain how you get those results from that data, with specific examples, such as "Branch b1 is included in the results because its revenue_target (which I can get from this query ...) is x, but the order total (which I can see from this query ...) is y, which is less than x. Branch b2 is not included because ..."
Always say which version of Oracle you're using
... I can write the queries to get the TOTAL_AMOUNT_FOR_ORDERS, TOTAL_AMOUNT_FOR_INSTALLATIONS, and BRANCHNUMBER, BRANCHNAME, REVENUETARGET. But I can't seem to put them all together.Thanks for posting these, but without CREATE TABLE and INSERT statements for your sample data, nobody can run them.
I don't think you need a FUL OUTER JOIN. If all you need is a grand total, UNION ALL would be more efficient. -
Need some help over sql query format
Input :
TABLE 1 ppl
id name
1 ravi
2 andy
3 john
TABLE 2 fa_ppl
id attr_name attr_value
1 watch Guess
1 laptop Sony
2 fashion casual
2 laptop Dell
3 watch fossil
3 fashion formal
OUTPUT Required:(3 rows)
name watch laptop fashion
ravi guess sony NULL
andy NULL dell casual
john fossil NULL formal
SQL Statements that may help in schema objects:
create table ppl(id number,name varchar2(50));
create table fa_ppl(id number,attr_name varchar2(20), attr_value varchar2(20));
insert into ppl values(1,'ravi');
insert into ppl values(2,'andy');
insert into ppl values(3,'john');
insert into fa_ppl values(1,'laptop','sony');
insert into fa_ppl values(1,'watch','guess');
insert into fa_ppl values(2,'laptop','dell');
insert into fa_ppl values(2,'fashion','casual');
insert into fa_ppl values(3,'fashion','formal');
insert into fa_ppl values(3,'watch','fossil');
I tried in the below way:
Select P.name,
case when attr_name='fashion' then attr_value end as fashion ,
case when attr_name='laptop' then attr_value end as laptop,
case when attr_name='watch' then attr_value end as watch,
from ppl P join fa_ppl F on (P.id=F.id and F.attr_name in ('fashion','laptop','watch'))
PROBLEM:
Getting separate rows(6 rows in my case) for each attribute_value.
Thanks a lot.....What you are trying is a pivot. There is a [url https://forums.oracle.com/forums/thread.jspa?threadID=2174552#9360005]thread in the FAQ linking to various methods.
You are actually pretty close, if you just do a group by on your select:
Select P.name,
max(case when attr_name='fashion' then attr_value end) as fashion ,
max(case when attr_name='laptop' then attr_value end) as laptop,
max(case when attr_name='watch' then attr_value end) as watch
from ppl P join fa_ppl F on (P.id=F.id and F.attr_name in ('fashion','laptop','watch'))
group by
p.nameBut if you have several people with same name, you probably want to do this:
Select
p.id, /*you may omit this column if you do not need it*/
max(P.name) as name,
max(case when attr_name='fashion' then attr_value end) as fashion ,
max(case when attr_name='laptop' then attr_value end) as laptop,
max(case when attr_name='watch' then attr_value end) as watch
from ppl P join fa_ppl F on (P.id=F.id and F.attr_name in ('fashion','laptop','watch'))
group by
p.idSimilar can be done in 11G with the PIVOT statement. See the links in the FAQ thread. -
I have my select statement but I want to make order by 2 things(parameters) that the user choose.
I have my parameter as list of values where i have the constant value FIRSTNAME and the constant value DATE.
I have something like this
select * from TABLE1 as P,TABLE2 as AP
ORDER BY :CHOICE
I want that :CHOISE to be replaced by code(P.Fname,P.LName)
or by (AP.DATE).(depends on users selection)
How can i pass my order selection from a parameter????
ThanxHi,
Add user parameter P_ORDER_BY as character.
In the afterparamform trigger add this code:
:P_ORDER_BY := 'order by '||:choice;
In the data model replace ORDER BY :CHOICE with &P_ORDER_BY.
For more information pls read the help for lexical parameters.
Regards, Gicu -
Need help writing a query for following scenario
Hi all, I need some help writing a query for the following case:
One Table : My_Table
Row Count: App 5000
Columns of Interest: AA and BB
Scenario: AA contains some names of which BB contains the corresponding ID. Some
names are appearing more than once with different IDs. For example,
AA BB
Dummy 10
Me 20
Me 30
Me 40
You 70
Me 50
Output needed: I need to write a query that will display only all the repeating names with their corresponding IDs excluding all other records.
I would appreciate any input. ThanksIs it possible to have a records with the same values for AA and BB? Are you interested in these rows or do you only care about rows with the same value of AA and different BB?
With a slight modification of a previous posting you can only select those rows that have distinct values of BB for the same value of AA
WITH t AS (
SELECT 'me' aa, 10 bb FROM dual
UNION ALL
SELECT 'me' aa, 20 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
UNION ALL
SELECT 'you' aa, 30 bb FROM dual
SELECT DISTINCT aa, bb
FROM (SELECT aa, bb, COUNT(DISTINCT bb) OVER(PARTITION BY aa) cnt FROM t)
WHERE cnt > 1; -
Need help on af:query, accessing bind variable.
Hi All,
Im working on Jdev 11.3. I need some help on af:Query UI component. I have a View Criteria defined on my VO and I am using this as <af:Query> in my .jsff. My View Criteria has 2 Bind Variables (Emp. Id, Emp Division). Emp Division is an LOV on the jsff.
My requirement is to refresh the Results table when user changes the division LOV(and not when he clicks on Search button in the Query component).
Is there a way to do this in backing bean? Something like a valuechangelistener on a LOV? How can I get from af:query component?
Thanks.Hi,
You can find some related info here..
Can I achive valueChangeListener in af:query?
and
http://jobinesh.blogspot.com/2011/03/retrieving-viewcriteria-from-custom.html
Thanks,
TK -
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 -
Need Help with complex query for productio database
Hello again,
i need your help again, for an query how Shows me how long every production step takes per Order.
See sample data and what i expect.
Thank you all for your help.
We use Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
Here the sample data tables:
CREATE TABLE TABLE_2
( "ORDER_NR" VARCHAR2 (12)
, "PRIORITY" VARCHAR2 (2)
, "WO_STEP" VARCHAR2 (1)
, "STEP_DATE" DATE
CREATE TABLE TABLE_1
( "ORDER_NR" VARCHAR2 (12) PRIMARY KEY
, "PRIORITY" VARCHAR2 (2)
, "CREATE_DATE" DATE
, "ACT_STEP" VARCHAR2 (2)
, "STEP_DATE" DATE
, "EMPLOYEE" VARCHAR2 (5)
, "DESCRIPTION" VARCHAR2 (20)
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, CREATE_DATE, ACT_STEP, STEP_DATE, EMPLOYEE, DESCRIPTION)
VALUES ('1KKA1T205634', '12', TO_DATE('10-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'), 'U', TO_DATE('28-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'), 'W0010', 'CLEAN HOUSE');
INSERT INTO TABLE_1 (ORDER_NR, PRIORITY, CREATE_DATE, ACT_STEP, STEP_DATE, EMPLOYEE, DESCRIPTION)
VALUES ('1KKA1Z300612', '12', TO_DATE('08-FEB-13 14:00:00','DD-MON-RR HH24:MI:SS'), 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'), 'K0052', 'REPAIR CAR');
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'A', TO_DATE('12-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'K', TO_DATE('13-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 11:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'K', TO_DATE('13-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', '5', TO_DATE('13-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'C', TO_DATE('14-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'B', TO_DATE('14-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'E', TO_DATE('18-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'S', TO_DATE('21-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'R', TO_DATE('21-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1T205634', '12', 'U', TO_DATE('28-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'A', TO_DATE('12-FEB-13 13:52:42','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 09:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'K', TO_DATE('13-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 11:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'K', TO_DATE('13-FEB-13 12:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', '5', TO_DATE('13-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'C', TO_DATE('14-FEB-13 08:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'B', TO_DATE('14-FEB-13 10:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'E', TO_DATE('18-FEB-13 13:00:00','DD-MON-RR HH24:MI:SS'));
INSERT INTO TABLE_2 (ORDER_NR, PRIORITY, WO_STEP, STEP_DATE)
VALUES ('1KKA1Z300612', '12', 'F', TO_DATE('20-FEB-13 16:00:00','DD-MON-RR HH24:MI:SS'));
COMMIT;And here is what i expect from my query:
SYSDATE 28.Feb.13 14:00
ORDER_NR PRIORITYCREATE_DATE STATUS STATUS_DATE DESCRIPTION AGE_1 AGE_2 WAITNG STEP_A STEP_B STEP_C STEP_5 STEP_K STEP_E STEP_F STEP_S STEP_R
1KKA1T205634 12 10.Feb.13 10:00 U 28.Feb.13 12:00 CLEAN HOUSE 18,083 8,833 2,125 0,833 4,125 0,083 0,750 0,208 2,125 0,666 0,042 7,125
1KKA1Z300612 12 08.Feb.13 14:00 F 20.Feb.13 16:00 REPAIR CAR 20,000 16,042 2,125 0,833 4,125 0,083 0,750 0,208 2,125 0,666 And now the explanation to the query result:
The AGE_1 is the difference in days between the 'CREATE_DATE' and IF EXSIST the STEP 'U' then STEP_DATE or if the STEP 'U' is not found in TABLE_2 then it should show the difference in days between the 'CREATE_DATE' and the 'SYSDATE'
The AGE_2 is the difference in days between the STEP 'A' STEP_DATE and IF EXSIST the STEP 'R' then STEP_DATE or if the STEP 'R' is not found in TABLE_2 then it should show the difference in days between the 'CREATE_DATE' and the 'SYSDATE'
The WAITING is the difference in days between CREATE_DATE and STEP 'A' STEP_DATE
The following columns show the days how long the ORDER_NR stays in these STEP, if an ORDER_NR comes into the same STEP more then one time it should be calculated together.
If the ORDER_NR skips a step it should show a zero in the specific field.
I hope my explanation is good enough, my english skills are far away from good.
Thanks all for your help.
Greets Reinhard W.Hi,
i changed this query:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.*,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nr,
t1.priority,
t1.create_date,
t1.act_step,
t1.step_date,
t1.employee,
t1.descriptionTo this:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.order_nr,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nrthen i get the ORA-00979 Error.
Whats wrong?
I have another question.
How can i handle i i want to group to or more 'STEP's in one Column.
in Case of this i want that the column 'STEP_B' contains all days for STEP 'B' and STEP '5'.
I tried already with a + Operation like this:
with t2 as (
select t.*,
lead(step_date) over(partition by order_nr order by step_date) next_step_date
from table_2 t
select t1.*,
nvl(
max(
case t2.wo_step
when 'U' then t2.step_date
end
sysdate
) - t1.create_date age_1,
nvl(
max(
case t2.wo_step
when 'R' then t2.step_date
end
sysdate
) - t1.create_date age_2,
Round( sum(
case t2.wo_step
when 'B' then t2.next_step_date - t2.step_date
end
) +
sum(
case t2.wo_step
when '5' then t2.next_step_date - t2.step_date
end
), 3 ) step_b
from table_1 t1,
t2
where t2.order_nr = t1.order_nr
group by t1.order_nr,
t1.priority,
t1.create_date,
t1.act_step,
t1.step_date,
t1.employee,
t1.descriptionBut this does reply evertime a NULL.
Thank You. -
Need some help with the Select query.
Need some help with the Select query.
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
select single vkorg abgru from ZADS into it_rej.
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
This is causing performance issue. They are asking me to include the where condition for this select query.
What should be my select query here?
Please suggest....
Any suggestion will be apprecaiated!
Regards,
DeveloperHello Everybody!
Thank you for all your response!
I had changes this work area into Internal table and changed the select query. PLease let me know if this causes any performance issues?
I had created a Z table with the following fields :
ZADS :
MANDT
VKORG
ABGRU.
I had written a select query as below :
I had removed the select single and insted of using the Structure it_rej, I had changed it into Internal table
select vkorg abgru from ZADS into it_rej.
Earlier :
IT_REJ is a Work area:
DATA : BEGIN OF IT_REJ,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
Now :
DATA : BEGIN OF IT_REJ occurs 0,
VKORG TYPE VBAK-VKORG,
ABGRU TYPE VBAP-ABGRU,
END OF IT_REJ.
I guess this will fix the issue correct?
PLease suggest!
Regards,
Developer. -
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De.
Maybe you are looking for
-
Change standard view: remove event time
In iCal, a lot of visual space is wasted listing the time of an event. Fortunately, the time is unlisted in some cases (e.g., a half hour event in 'week' view), but for 1 hour events or more, the view of the event description is occluded by the start
-
Does an external hard drive have to stay plugged in with Time Machine?
I have an external hard drive but I do not always leave it plugged in. I usually back up my computer every week or so and am just now getting around to using Time Machine. If i plug in my external hard drive and press "Back up now" will Time machine
-
IDOC Number in BPM (Bhavesh & Michal Help)
Hi , I tried the using the concepts from the blog (/people/mitesh.parekh/blog/2008/12/01/receiving-aleaud-as-acknowledgment-in-ccbpm)where i have the Message ID which entered into BPM but not the message Id which triggered the IDoc send . I think i m
-
Hi all, I have a string of hex characters say aeef1234569a Now I want to store this in a byte array as (0xae,0xef,0x12,0x34..} How can this be done. Many thanks. Ram
-
Hello Experts, Is there a possibility to change or edit the Purchase requisition if it has been passed already from EBP to R/3 ? Regards ilhan