Continuation-Please help with a complex sql query
Hi all,
Thanks a lot for your suggestions and inputs in the last thread of this post.
With the help of your suggested approach,i went ahead and was able to get some more data in the format as needed..I worked on gradually adding one table after another exactly as the data is required stepwise.But,still I am facing issues with displaying them.
there are many issues I am not able to do and would appreciate if you please have a look at my latest modified SELECT given below and help me writing it to get the display as needed.
Below,is the attempted query i tried out using your inputs.But,am stuck and need your help in writing it.
**Here,there is 1 ->MANY lines for the t_objective_id--->This has many learning_record_ids which i want to group by the unique objective_id.
Also,prior to that,there is a tplan_id---->which has MANY t_objective_id's
SELECT DECODE (LAG (firstname, 1, 0) OVER (ORDER BY firstname),
firstname, ' ',
firstname
) firstname,
DECODE (LAG (emplid, 1, 0) OVER (ORDER BY emplid),
emplid, ' ',
emplid
) emplid,
DECODE (LAG (tplan_name, 1, 0) OVER (ORDER BY tplan_name),
tplan_name, ' ',
tplan_name
) tplan_name,
tplan_id,
DECODE (LAG (activities, 1, 0) OVER (ORDER BY activities),
activities, ' ',
activities
) activities,
activities,
--activities,
DECODE (LAG (t_objective_id, 1, 0) OVER (ORDER BY t_objective_id),
t_objective_id, ' ',
t_objective_id
) t_objective_id,
completed_activities,required_credits,
DECODE (LAG (learning_record_id, 1, 0) OVER (ORDER BY learning_record_id),
learning_record_id, ' ',
learning_record_id
) learning_record_id,
catalog_item_name,catalog_item_type
FROM (SELECT test_cp.firstname, test_op.emplid, tp.tplan_name,tp.tplan_id,FN_TP_GET_CMPLTD_ACT_CNT(tp.tplan_id,test_lp.lp_person_id,'1862') activities,
test_tpo.t_objective_id,
( fn_tp_obj_comp_req_act_cdt (test_lp.lp_person_id,
tp.tplan_id,
test_tpo.t_objective_id,
tp.is_credit_based
+ fn_tp_obj_comp_opt_act_cdt (test_lp.lp_person_id,
tp.tplan_id,
test_tpo.t_objective_id,
tp.is_credit_based
) completed_activities,test_tpo.required_credits,lr.learning_record_id,lr.catalog_item_name,lr.catalog_item_type
FROM test_learning_plan test_lp,
test_training_plan tp,
test_person test_cp,
test_org_person test_op,test_tp_learning_activity test_tplplr,
test_tp_objective test_tpo,test_learning_record lr,test_train_obj_activity tpobjact
WHERE test_lp.lp_person_id = '1862188559'
AND test_cp.person_id = test_lp.lp_person_id
AND tp.tplan_id = 'tplan200811200632352287621599'
AND test_tpo.t_objective_id = tpobjact.t_objective_id
AND test_lp.LP_CATALOG_HIST_ID = tp.tplan_id
AND test_tplplr.tp_lp_lr_id =test_lp.learning_plan_id
AND test_tplplr.activity_lp_lr_id = lr.learning_record_id
AND lr.LR_CATALOG_HISTORY_ID = tpobjact.activity_id
AND test_op.o_person_id = test_cp.person_id
AND test_tpo.tplan_id = tp.tplan_id)
If we see the outer SELECT ---then one of the main issues is for EACH t_objective_id----->There are n learning_record_ids.
But,this select only shows 1 learning_record_id for each objective_id which is wrong.
Similarly,the data displayed isnot proper.
Below is the way I am getting the data now from the above SELECT.
Note:- FIRSTNAME is not correctly displayed.
FIRSTNAME EMPLID TPLAN_NAME TPLAN_ID ACTIVITIES ACTIVITIES_1
001 TP1 tplan1 5 5
TESTNAME tplan1 5
Continuation of the other columns of the same rows--**couldnt paste it properly so.
T_OBJECTIVE_ID COMPLETED_ACTIVITIES REQUIRED_CREDITS LEARNING_RECORD_ID CATALOG_ITEM_NAME CATALOG_ITEM_TYPE
obj1 1 5 lr1 C1 Course
obj2 4 4
Something like this might solve your problem ->
satyaki>
satyaki>select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
Elapsed: 00:00:00.00
satyaki>
satyaki>
satyaki>select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7521 WARD SALESMAN 7698 22-FEB-81 226.88 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1815 1400 30
7788 SCOTT ANALYST 7566 19-APR-87 598.95 20
7839 KING PRESIDENT 17-NOV-81 7260 10
7844 TURNER SALESMAN 7698 08-SEP-81 2178 0 30
7876 ADAMS CLERK 7788 23-MAY-87 159.72 20
7900 JAMES CLERK 7698 03-DEC-81 1379.4 30
7902 FORD ANALYST 7566 03-DEC-81 5270.76 20
7934 MILLER CLERK 7782 23-JAN-82 1887.6 10
7566 Smith Manager 7839 23-JAN-82 1848 0 10
7698 Glen Manager 7839 23-JAN-82 1848 0 10
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7599 BILLY ANALYST 7566 10-JUN-09 4500 30
12 rows selected.
Elapsed: 00:00:00.00
satyaki>
satyaki>
satyaki>
satyaki>
satyaki>select decode(lag(job,1,null) over(order by job),'CLERK','CK',
2 'SALESMAN', 'SM',
3 'ANALYST','AL',
4 'BO') res
5 from emp;
RE
BO
AL
AL
AL
CK
CK
CK
BO
BO
BO
SM
RE
SM
12 rows selected.
Elapsed: 00:00:00.01
satyaki>
satyaki>
satyaki>select MAX(decode(lag(job,1,null) over(order by job),'CLERK','CK',
2 'SALESMAN', 'SM',
3 'ANALYST','AL',
4 'BO')) res
5 from emp;
select MAX(decode(lag(job,1,null) over(order by job),'CLERK','CK',
ERROR at line 1:
ORA-30483: window functions are not allowed here
Elapsed: 00:00:00.01
satyaki>
satyaki>select MAX(res)
2 from (
3 select decode(lag(job,1,null) over(order by job),'CLERK','CK',
4 'SALESMAN', 'SM',
5 'ANALYST','AL',
6 'BO') res
7 from emp
8 );
MA
SM
Elapsed: 00:00:00.00
satyaki>
satyaki>Regards.
Satyaki De.
Similar Messages
-
Need Help with Creating the SQl query
Hi,
SQL query gurus...
INFORMATION:
I have two table, CURRENT and PREVIOUS.(Table Defs below).
CURRENT:
Column1 - CURR_PARENT
Column2 - CURR_CHILD
Column3 - CURR_CHILD_ATTRIBUTE 1
Column4 - CURR_CHILD_ATTRIBUTE 2
Column5 - CURR_CHILD_ATTRIBUTE 3
PREVIOUS:
Column1 - PREV_PARENT
Column2 - PREV_CHILD
Column3 - PREV_CHILD_ATTRIBUTE 1
Column4 - PREV_CHILD_ATTRIBUTE 2
Column5 - PREV_CHILD_ATTRIBUTE 3
PROBLEM STATEMENT
Here the columns 3 to 5 are the attributes of the Child. Lets assume that I have two loads, One Today which goes to the CURRENT table and one yesterday which goes to the PREVIOUS table. Between these two loads there is a CHANGE in the value for Columns either 3/4/5 or all of them(doesnt matter if one or all).
I want to determine what properties for the child have changed with the help of a MOST efficient SQL query.(PARENT+CHILD is unique key). The Database is ofcourse ORACLE.
Please help.
Regards,
ParagHi,
The last message was not posted by the same user_name that started the thread.
Please don't do that: it's confusing.
Earlier replies give you the information you want, with one row of output (maximum) per row in current_tbl. There may be 1, 2 or 3 changes on a row.
You just have to unpivot that data to get one row for every change, like this:
WITH single_row AS
SELECT c.curr_parent
, c.curr_child
, c.curr_child_attribute1
, c.curr_child_attribute2
, c.curr_child_attribute3
, DECODE (c.curr_child_attribute1, p.prev_child_attribute1, 0, 1) AS diff1
, DECODE (c.curr_child_attribute2, p.prev_child_attribute2, 0, 2) AS diff2
, DECODE (c.curr_child_attribute3, p.prev_child_attribute3, 0, 3) AS diff3
FROM current_tbl c
JOIN previous_tbl p ON c.curr_parent = p.prev_parent
AND c.curr_child = p.prev_child
WHERE c.curr_child_attribute1 != p.prev_child_attribute1
OR c.curr_child_attribute2 != p.prev_child_attribute2
OR c.curr_child_attribute3 != p.prev_child_attribute3
, cntr AS
SELECT LEVEL AS n
FROM dual
CONNECT BY LEVEL <= 3
SELECT s.curr_parent AS parent
, s.curr_child AS child
, CASE c.n
WHEN 1 THEN s.curr_child_attribute1
WHEN 2 THEN s.curr_child_attribute2
WHEN 3 THEN s.curr_child_attribute3
END AS attribute
, c.n AS attribute_value
FROM single_row s
JOIN cntr c ON c.n IN ( s.diff1
, s.diff2
, s.diff3
ORDER BY attribute_value
, parent
, child
; -
Please help with workflow inspired SQL
Hi everyone, I need some help with a piece of sql I need to create. It will be used in a workflow scenario and is therefore a bit hairy and has some limitations. So here goes..
I have 2 current tables that will be used in this query. The first is a table used as a supervisor cross reference, and the second will be used for signing authority, or approval authorization. In short...the first table will give you the supervisor for the particular employee or employee in the where clause. This tells us who to route to. The second table has the operators authority level. As an example.... 1, 2, 3, or 4. A 1 could signify that this supervisor can approve for up to $5,000, and a 4 could signify that this person can approve up to $250,000. thats the basic idea.
So what I need to do in ONE statement is get the requestor's supervisor, and then check his/hers authority level for level 1, 2, 3 or 4. The authority level needed will be passed into the query, so I will have that as well as the requestor. And here is the hard part...... If no rows are returned (the supervisor does not have authority to approve) I need the query to apply the same logic to the next supervisor. In other words, the supervisor's supervisor. Every requestor will have a supervisor. Every supervisor will also have a supervisor....so it goes until we get to the CEO. So the query needs to keep going until it finds a row matching the signing authority.
So the limitations are......this needs to happen in one SQL query, and this query can only return ONE field!
Here are some creates and inserts to give you something to work with. Been suffering with this one for a few days so your help is GREATLY appreciated.
Supervisor cross reference table
CREATE TABLE PS_ROLEXLATOPR (ROLEUSER VARCHAR2(30) DEFAULT ' ' NOT
NULL,
DESCR VARCHAR2(30) DEFAULT ' ' NOT NULL,
OPRID VARCHAR2(30) DEFAULT ' ' NOT NULL,
EMAILID VARCHAR2(70) DEFAULT ' ' NOT NULL,
FORMID VARCHAR2(70) DEFAULT ' ' NOT NULL,
WORKLIST_USER_SW VARCHAR2(1) DEFAULT 'Y' NOT NULL,
EMAIL_USER_SW VARCHAR2(1) DEFAULT 'Y' NOT NULL,
FORMS_USER_SW VARCHAR2(1) DEFAULT 'Y' NOT NULL,
EMPLID VARCHAR2(11) DEFAULT ' ' NOT NULL,
ROLEUSER_ALT VARCHAR2(30) DEFAULT ' ' NOT NULL,
ROLEUSER_SUPR VARCHAR2(30) DEFAULT ' ' NOT NULL,
EFFDT_FROM DATE,
EFFDT_TO DATE) TABLESPACE PTTBL STORAGE (INITIAL 40000 NEXT 100000
MAXEXTENTS UNLIMITED PCTINCREASE 0) PCTFREE 10 PCTUSED 80
INSERT INTO PS_ROLEXLATOPR
DESCR,
OPRID ,
EMAILID ,
FORMID ,
WORKLIST_USER_SW ,
EMAIL_USER_SW ,
FORMS_USER_SW ,
EMPLID ,
ROLEUSER_ALT ,
ROLEUSER_SUPR ,
EFFDT_FROM
VALUES
DESCR,
'ABC123',
'XYZ123',
Signing Authority table..
CREATE TABLE PS_ZZ_WF_AUTHORITY (OPRID VARCHAR2(30) NOT NULL,
EMPLID VARCHAR2(11) NOT NULL,
EMAILID VARCHAR2(70) NOT NULL,
ZZ_SIGN_AUTHORITY VARCHAR2(1) NOT NULL) TABLESPACE APLARGE STORAGE
(INITIAL 40000 NEXT 100000 MAXEXTENTS UNLIMITED PCTINCREASE 0)
PCTFREE 10 PCTUSED 80
insert into PS_ZZ_WF_AUTHORITY
OPRID,
EMPLID,
EMAILID,
ZZ_SIGN_AUTHORITY
Values
'XYZ123',
'Any_Email',
'1'
)Hi,
Welcome to the forum!
Thanks for posting the CREATE TABLE and INSERT statements; that's very helpful!
Whenever you have a question, also post the results you want to get from the sample data your posted.
Always say what version of Oracle you're suing.
In the sample data you posted, I only see one row in each table. Does that really give a good picture of the problem? If the problem involves going up the chain of command 1, 2, 3 or more levels, then shouldn't you have a chain of at least 3 people to show the problem and test the results?
I think what you need is CONNECT BY Query , which works on a table with a parent-child relationship. Given a parent, you can find that ow's children, the children's children, their children, and so on, however many generations there are.
I don't think I can demonstrate this with your sample data, so I'll use the scott.emp table, whcih you should be able to query.
The emp table contains a hierarcy of employees, including this data:
NAME EMPNO MGR SAL JOB
KING 7839 5000 PRESIDENT
JONES 7566 7839 2975 MANAGER
SCOTT 7788 7566 3000 ANALYST
ADAMS 7876 7788 1100 CLERK
FORD 7902 7566 3000 ANALYST
SMITH 7369 7902 800 CLERK
BLAKE 7698 7839 2850 MANAGER
ALLEN 7499 7698 1600 SALESMAN
WARD 7521 7698 1250 SALESMAN
MARTIN 7654 7698 1250 SALESMAN
TURNER 7844 7698 1500 SALESMAN
JAMES 7900 7698 950 CLERK
CLARK 7782 7839 2450 MANAGER
MILLER 7934 7782 1300 CLERKThis show, among other things, that the employee with ename='KING' (empno=7839) has no boss.
JONES (empno=7566) does have a boss (mgr=7839), namely KING.
SCOTT (empno=7788) has a bos (mgr=7566), who is JONES.
I got the results above using this CONNECT BY query:
SELECT LPAD ( ' '
, 3 * (LEVEL - 1)
) || ename AS name
, empno
, mgr
, sal
, job
FROM scott.emp
START WITH mgr IS NULL
CONNECT BY mgr = PRIOR empno
;This is an example of a Top-Down Query , where we start with a parent, then find its children, grandchildren, and so on.
In your probelm, you want to do a Bottom-Up Query ; given a child, you want to see if its parent has a certain level of authority. If not, you need to look at that parent's parent, and keep going until to either reach someone with the right qualifications, or you reach the end of the chain of command.
That's similar to this problem: given a set of employees in scott.emp (say, everyone with job='CLERK') we want to find their closest ancestor who has a sal of 3000 or more. Look at the data above: you can see that SMITH is a CLERK, and SMITH'S boss, FORD, has sal=3000, so we want a row of output that shows SMITH and FORD.
For a different example, looks at MILLER. MILLER's boss, CLARK, only has sal=2450, so we need to look at CLARK's boss, KING.
One way to do that in a CONNECT BY query is:
SELECT CONNECT_BY_ROOT ename AS subordinate
, ename
, LEVEL - 1 AS steps_apart
FROM scott.emp
WHERE CONNECT_BY_ISLEAF = 1
START WITH job = 'CLERK'
CONNECT BY empno = PRIOR mgr
AND PRIOR sal < 3000
;Output:
SUBORDINAT ENAME STEPS_APART
SMITH FORD 1
ADAMS SCOTT 1
JAMES KING 2
MILLER KING 2You should be all set to write the query now.
Just kidding. I'll bet there's a lot of stuff in this message that's new to you. It's all documented in the SQL Language manual:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/index.htm
If you'd like help, post your version, a more complete set of sample data, and the results you want from that data.
Explain, with specific examples, how you get the results you posted from the data you posted.
Do as much of the query as you can, and post your code. -
Please Help with Forms and SQL Plus Connection Issues in XE
I am not able to connect with the Forms Builder or SQL Plus with the following errors:
When attempting to connect with the supplied website.edu:port/orcl
ORA-06401: NETCMN: invalid driver designator
When leaving the connection information blank and using my local login and password
for my copy of Oracle XE
ORA-12560: TNS:protocol adapter error
Based on some reading, I set the TNS_ADMIN user environmental variable, trying each of the tnsnames.ora locations but I'm still receiving the same errors
Location 1: DevSuiteHome\...\tnsnames.ora
Location 2: oraclexe\...\tnsnames.oraThanks, Vikas!
But I did a search for 6iServer.conf and did NOT locate any such file
Please help
TIA
SHANKAR
Shankar,
It would have helped if you had indicated what is the full version of 9iAS that you have installed. By default the portnumber that is used is 7778.
As per the entries, they should have already been made in the config files. But check that the 6iServer.Conf file and it should contain the entries that you are being told to update. If not, then add the entries in the 6iServer.conf file and that is all you need.
HTH.
Vikash -
Help with APP_USER in SQL query
Hi,
I would like to extract a user details in one Item of DML form based on the Logged user and standard authentication schema.
I have this query in the Item:
select ANALYST_FIRST_NAME ||' '|| ANALYST_LAST_NAME d, ANALYST_ID r from QTMT_ANALYST where EMAIL = :APP_USER The User in the session is equal to the EMAIL value. In fact, the value in the session is upper case and the other in the database is lower case. I believe that that is not relevant anyway.
The query do not bring any value. Could you please let me know what I have got wrong?
APEX: 4.2
Item Source Type: SQL Query (return single value)
Thank you and best regards,
VladimirTo be on the safe side you should uppercase both sides of the equation. It could be that the username is entered in lower case.
But if you think that your query is correct, test it by using a literal instead of the user name.
select ANALYST_FIRST_NAME ||' '|| ANALYST_LAST_NAME d
from QTMT_ANALYST
where upper(EMAIL) = upper(:APP_USER);for testing purposes use something like this:
select ANALYST_FIRST_NAME ||' '|| ANALYST_LAST_NAME d
from QTMT_ANALYST
where upper(EMAIL) = upper('[email protected]');Edited by: Sven W. on Oct 23, 2012 1:30 PM
APEX: 4.2
Item Source Type: SQL Query ( return single value )I just noticed that you use an item which has this as the SQL source. This works only if the sql returns one row and one column only. Therefore I removed the second column from your query.
Edited by: Sven W. on Oct 23, 2012 1:33 PM -
Please help with dynamic pl/sql
Trying to write a generic pl/sql package that can be used on any table I specify at runtime. The procedures are simple and there are only two. I don't understand advanced pl/sql enought to write it myself dynamically. Could someone please give me ideas on this? Because I don't know the syntax of dynamic pl/sql, books aren't helping much with a project I have due tomorrow :)
Yes, it will all be done at the same time, but I would like them to be able to run independent of each other in case i want one and not the other and vice versa. In procedure 1, the only thing to be done is to get the text in column_1 of tableA to column_1 in tableB.
I have a regular procedure for procedure 1 that I think will work:
CREATE OR REPLACE PROCEDURE UPDATE_COLUMN_1
IS
v_column_1 tableB.column_1%TYPE;
v_name tableC.name%TYPE;
CURSOR c_name_column_1 IS
SELECT column_1, name from tableA;
BEGIN
OPEN c_name_column_1;
LOOP
Fetch c_name_column_1 INTO v_column_1, v_name;
EXIT WHEN c_name_column_1%NOTFOUND;
UPDATE tableB
SET column_1 = v_column_1,
lst_updt = sysdate,
updt_by = 'anna'
WHERE name = v_name;
END LOOP;
CLOSE c_name_column_1;
END UPDATE_COLUMN_1;
My main email is in my husband's name. I will get it faster than my hotmail account. [email protected] -
Help with SYSDATE in SQL query
Hi all,
I'm trying to select some data from table base on SYSDATE. The below query does not return any data.
My query is:
select count(TICKET_ID) "ECEMEA" from QTMT_TICKETS where STATUS_ID=1 and TEAM_ID=3 and RECEIVED_DATE=sysdate
Could you please let me know, why this does not work? Date format in my application is: DD-MMM-YY (16-AUG-12). In the database the dates are stored in this format: MM/DD/YYYY (08/25/2012). Does it mean, that I have to play with the format?
Please let me know.
Thank you and best regards,
Vladimiruser13277783 wrote:
Hi all,Please update your forum profile with a real handle instead of "user13277783".
I'm trying to select some data from table base on SYSDATE. The below query does not return any data.
My query is:
select count(TICKET_ID) "ECEMEA" from QTMT_TICKETS where STATUS_ID=1 and TEAM_ID=3 and RECEIVED_DATE=sysdate
Always post code wrapped in <tt>\...\</tt> tags, as described in the FAQ.
Could you please let me know, why this does not work? Date format in my application is: DD-MMM-YY (16-AUG-12). In the database the dates are stored in this format: MM/DD/YYYY (08/25/2012). Assuming RECEIVED_DATE is actually of type DATE (and it should be), then no format is applied to dates until they are converted for display, however there may be a time component stored that is not visible in the default date display format. Try
select count(TICKET_ID) "ECEMEA"
from QTMT_TICKETS
where STATUS_ID=1
and TEAM_ID=3
and RECEIVED_DATE >= trunc(sysdate)
and RECEIVED_DATE < trunc(sysdate) + 1 -
Please help me correct this SQL query...
hi, please consider following:
create table test(col varchar2(255))
insert into test values ('DURATION');
insert into test values ('VOLUME');
select col from test where col in ('DURATION','VOLUME');
this returns the rows.
but my input string is a comma separated list:
DURATION,VOLUME
so i try
select col from test where col in (replace('DURATION,VOLUME',',',''','''));
but no results. Or:
select col from test where col in (''''||replace('DURATION,VOLUME',',',''',''')||'''');
however
select ''''||replace('DURATION,VOLUME',',',''',''')||'''' from dual
gives 'DURATION','VOLUME'
so why doesnt it work?
hope you can help. ThanksAnother solution will be:
SQL> variable comma_sep_value VARCHAR2(100);
SQL> exec :comma_sep_value := 'DURATION,VOLUME,ABCD'
PL/SQL procedure successfully completed.
SQL> SELECT *
2 FROM TEST
3 WHERE INSTR (:comma_sep_value, col) > 0
4 /
DURATION
VOLUME
2 rows selected.
SQL> exec :comma_sep_value := 'DURATION';
PL/SQL procedure successfully completed.
SQL> SELECT *
2 FROM TEST
3 WHERE INSTR (:comma_sep_value, col) > 0
4 /
DURATION
1 row selected.
SQL> exec :comma_sep_value := 'DURATION,VOLUME';
PL/SQL procedure successfully completed.
SQL> SELECT *
2 FROM TEST
3 WHERE INSTR (:comma_sep_value, col) > 0
4 /
DURATION
VOLUME
2 rows selected.Hope this helps.
Regards,
Jo -
Help with a advanced SQL query
I have a table with the help of standard lengths of material. (tblStdLength)
In another table are the lengths that are in stock at the material. (tblOnStock)
I would want to match these two tables to find out how much of the stock of standard lengths you have in stock the table.
If the length is longer than the nearest standard length, the drag of the excess down to the nearest standradlängd if there is an X "CUT field.
However, I have encountered a problem and can not solve this ...
Ex:
Material Cable23 have 4 standard lengths in Table tblStdLength:
{code}
Material StdLength
Cable23 | 2000
Cable23 | 4000
Cable23 | 6000
Cable23 | 8000
Materials Cable23 have the following lengths in stock in Table tblOnStock:
Material Factor Length TotalQty Cut
Cable23 | 1 | 383 | 383 |
Cable23 | 1 | 424 | 424 |
Cable23 | 1 | 998 | 998 |
Cable23 | 1 | 1000 | 1000 |
Cable23 | 3 | 4000 | 12000 | X
Cable23 | 1 | 4234 | 4234 | X
{code}
In this mode, shall the lengths of 12000 + 4000 include in the calculation of the total stock of standard lengths.
The answer should be 16000.
4000, it gets of 4234. It has X on the 'Cut' field and then it round down to the nearest standard length.
How do I do this best? Hope I explained so you understand ...Hi again,
SQL works great when there are several lengths (rows) in the table tblStdlength. But sometimes there is only one standard length per matrial in the table (1 row), then it does not work. Why? Assumes lead function that there should be several rows?
with tblStd as
select Material
, stdlength
, lead(stdlength) over (partition by material order by stdlength) hstdlength
from tblStdlength
select t.Material
, sum(floor(s.total_qty/t.stdlength)*t.stdlength) tot_quantity
from tblStd t, tblonstock s
where t.material=s.material
and s.length >= t.stdlength
and s.length< t.hstdlength
group by t.material -
Help me write a SQL query; urgent
Hi, can somebody please help me write a SQL query.
I have 3 tables each with the same column names (Col1, Col2, Col3). Col1 is PK with Unique Constraint.
I wanted to add values of Col2 and Col3 (from all 3 tables) and put it in a separate table (i.e aggregated) of all values found in Col1.
Does anybody help me please ?
thanks alot.Please don't mark your question as urgent. You've been around here long enough that you should know that it will not get your question anwered any faster, and may just irritate people into not answering at all.
I'm not sure exactly what you want.
Are you saying you want t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3 for the rows that have the same c1 in all three tables?
If so, it would be like this, I think: insert into t4
select t1.c1
, t1.c2 + t1.c3 + t2.c2 + t2.c3+ t3.c2 + t3.c3
from t1, t2, t3
where t2.c1 = t1.c1
and t3.c1 = t1.c1If that's not what you want, please clarify.
And next time maybe you should post your SQL question in a SQL forum. -
Please help with an sql to show more than one records into single row for each student
From the following data I would like to create an sql to get the information as the following layout
studentid, firstTerm, EnglishMark1,ScienceMark1,MathsMark1, Secondterm,EnglishMark2,ScienceMark2,MathsMark2,
ThirdTerm,EnglishMark3,ScienceMark3,MathsMark3 // As single rows for each student
Example
1 First, 30,40,20,Sec,30,40,20, simillarly next row for next row for another sudent. Please help to generate the sql for the same.
Please help it would be very appreciate.
With Thanks
Pol
polachancreate table yourdata (studentid int, term varchar(10), section varchar(50), Mark int)
insert into yourdata values
(1,'First','Math',20),(1,'First','English',30),(1,'First','Science',40),
(2,'First','Math',20),(2,'First','English',30),(2,'First','Science',40),
(3,'First','Math',20),(3,'First','English',30),(3,'First','Science',40),
(1,'Sec','Math',20),(1,'Sec','English',30),(1,'Sec','Science',40),
(2,'Sec','Math',20),(2,'Sec','English',30),(2,'Sec','Science',40),
(3,'Sec','Math',20),(3,'Sec','English',30),(3,'Sec','Science',40)
Select studentid
,max(case when term='First' and section='English' Then Mark End) as EnglishMark1
,max(case when term='First' and section='Science' Then Mark End) as ScienceMark1
,max(case when term='First' and section='Math' Then Mark End) as MathMark1
,max(case when term='Sec' and section='English' Then Mark End) as EnglishMark2
,max(case when term='Sec' and section='Science' Then Mark End) as ScienceMark2
,max(case when term='Sec' and section='Math' Then Mark End) as MathMark2
,max(case when term='Third' and section='English' Then Mark End) as EnglishMark3
,max(case when term='Third' and section='Science' Then Mark End) as ScienceMark3
,max(case when term='Third' and section='Math' Then Mark End) as MathMark3
From yourdata
Group by studentid
drop table yourdata -
Please help to re-write this query using exists or with
Hi please help to re-write this query using exists or with, i need to write same code for 45 day , 90 days and so on but sub query condition is same for all
SELECT SUM (DECODE (t_one_mon_c_paid_us, 0, 0, 1)) t_two_y_m_mul_ca_
FROM (SELECT SUM (one_mon_c_paid_us) t_one_mon_c_paid_us
FROM (
SELECT a.individual_id individual_id,
CASE
WHEN NVL
(b.ship_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 45
AND a.country_cd = 'US'
AND b.individual_id in (
SELECT UNIQUE c.individual_id
FROM order c
WHERE c.prod_cd = 'A'
AND NVL (c.last_payment_dt,
TO_DATE ('05-MAY-1955')
) >= SYSDATE - 745)
THEN 1
ELSE 0
END AS one_mon_c_paid_us
FROM items b, addr a, product d
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id)
GROUP BY individual_id)
Edited by: user4522368 on Aug 23, 2010 9:11 AMPlease try and place \ before and after you code \Could you not remove the inline column select with the following?
SELECT a.individual_id individual_id
,CASE
when b.Ship_dt is null then
3
WHEN b.ship_dt >= SYSDATE - 90
3
WHEN b.ship_dt >= SYSDATE - 45
2
WHEN b.ship_dt >= SYSDATE - 30
1
END AS one_mon_c_paid_us
FROM items b
,addr a
,product d
,order o
WHERE b.prod_id = d.prod_id
AND d.affinity_1_cd = 'ADH'
AND b.individual_id = a.individual_id
AND b.Individual_ID = o.Individual_ID
and o.Prod_CD = 'A'
and NVL (o.last_payment_dt,TO_DATE ('05-MAY-1955') ) >= SYSDATE - 745
and a.Country_CD = 'US' -
Please help with regular expression
Hello,
With the help of my previous posting answers, Re: Procedure to Extract multiple substring from a string , I updated the query. But, I am not getting desired answer in all case. Could you please help me out? My query is based on the previous posting. Any other simple way to achieve this?
I will really appreciate it.
select
ltrim ( regexp_substr(txt, '\[(\w+)', 1, level), '[') as id, /* id is number */
ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+-*\d*', 1, 1), ':') as qid, /* Qid could be char/number/space any combination except ':' */
ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+', 1, 2), ':') as num,
to_date( ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '[^:]+', 1, 3), ':'),'MM/DD/YY') as effdate
from (
select '[10946:M100:N:][10947:Q1222:N:][38198:PPP-2:N:][13935:PPP-6:N:][38244:QQQ-4:Y:01/01/10]' as txt
from dual
connect by level <= length(regexp_replace(txt, '[^[]'));I should get :
ID QID NUM EFFDATE
10946 M100 N
10947 Q1222 N
38198 PPP-2 N
13935 PPP-6 N
38244 QQQ-4 Y 01-JAN-10But, getting
ID QID NUM EFFDATE
10946 M100 N
10947 Q1222 N
38198 PPP-2 2
13935 PPP-6 6
38244 QQQ-4 4 01-JAN-10Thanks,Hi,
So the num column is wrong, is that it?
Describe what the num column should be. For example "num is the 3rd part of the :-delimited list enclosed in the square brackets".
If that's what you want, then change the definition of num from
... ltrim ( regexp_substr(ltrim ( regexp_substr(txt, ':[^]]+', 1, level), ':'), '\w+', 1, 2), ':') as num,to
... REGEXP_SUBSTR ( REGEXP_SUBSTR ( txt
, '[^]]+'
, 1
, LEVEL
, '[^:]+'
, 1
, 3
) AS num, -
Help needed in framing SQL query.
Hi,
I have table having following schema:
PRODUCT_ID INT
DATE DATETIME
ITEMS_SOLD INT
This table contains data for a week (sunday to saturday). I want to write an SQL query to get (filter out) PRODUCT_ID of products which has same no. of ITEMS_SOLD for all 7 days. Also for given PRODUCT_ID I need to find the longest period of successive days where the no. of ITEMS_SOLD is same for all days in this period. Eg.(PRODUCT_ID 23 was sold as following along the week in no. of units sold: 4,6,6,6,6,7,4 .So the longest period is *4 days* from Monday to Thursday.) The first condition is special case of second condition where no. of days is 7.
Any help to get the SQL query will be appreciated.
Thanks,
Akshay.PRODUCT_ID DATE ITEMS_SOLD
1 10/10/2011 3
1 11/10/2011 3
1 12/10/2011 3
1 13/10/2011 3
1 16/10/2011 5
2 10/10/2011 4
2 11/10/2011 4
2 12/10/2011 4
2 13/10/2011 4
2 14/10/2011 4
2 15/10/2011 4
2 16/10/2011 4
Output:
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
1 3 4
2 4 7
Explanation of results:
The table to be queried contains data for 1 week: from 10/10/2011(Sunday) to 16/10/2011(Saturday). Now, product with PRODUCT_ID '1' was sold on dates 10,11,12,13,16. Out of these 5 days 3 units were sold on 4 successive days (from 10-13). So output should be like :
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
1 3 4
as longest period of successive days is 4 where same no. of units were sold i.e 3 units (other period is of 1 day on 16th ).
For PRODUCT_ID 2 we have only one period of 7 days where 4 units were sold each day. So we output :
PRODUCT_ID ITEMS_SOLD NO_OF_DAYS
2 4 7
Other case where same PRODUCT_ID have different units sold on each day should be ignored.
I hope that clarifies the problem more. Let me know in case I have missed out anything which should have been mentioned.
-Akshay. -
Hello,
My question is: I would like to do more complex sql query ( i need to use GROUP BY, HAVING, ORDER BY). Is possible do it with CMP entity bean, or i have to use BMP entity bean or Session bean? Query return about 20-30 items. Can you recommend some design pattern for this situation?
Thanks in advance
Daniel HHello,
My question is: I would like to do more complex sql query ( i need to use GROUP BY, HAVING, ORDER BY). Is possible do it with CMP entity bean, or i have to use BMP entity bean or Session bean? Query return about 20-30 items. Can you recommend some design pattern for this situation?
Thanks in advance
Daniel H
Maybe you are looking for
-
Problem with Webcam on Lenovo G560
Hi guys, I need help with my Lenovo G560. I cannot access the Webcam, I have not been able to take any pictures/videos with my laptop. I am using Windows 7 Ultimate, 32-bit operating system. Thanks in advance guys! Nonz
-
How to get folders/icons in all finder areas to have the same size?
I'm trying to make the icons in all areas of my finder appear a larger size. I've managed to make this happen by going into view/view options, but it seems to change it only for that window. If I go into a different window they are the "default" size
-
Apple TV bricked after v6.0 Update Help Requested
My AppleTV was working before the v6.0 update. When I updated it I noticed it took a long time. It seemed to go ok but it then rebooted and showed to connect to iTunes. Never saw that before so I did a little Googling to figure out what was the cause
-
Particle Playground: trouble with wall and particle image
Hello Friends, I'm fairly new to all of the fancy stuff in After Effects, particularly particles, but I've had a lot of success so far, and just one problem that I can't seem to figure out. Perhaps someone here can help me. I'm trying to create a cli
-
I like to check the 'Preserve current file name in XMP metadata' box when using the Batch Rename in Bridge so I can possibly use the original name after running the batch renaming. But I cannot seem to locate where the "current file name" is stored i