How to write this query ?
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptno
Hi,
806540 wrote:
how to write this query ?
list the emp name who is working for the highest avg sal department.
I can use row_number over to get correct result. If we don't use this row_number function, just plain sql, how to do it ?ROW_NUMBER is just plain SQL, and has been since Oracle 8.1.
ROW_NUMBER (or its close relative, RANK) is the simplest and most efficient way to solve this problem. Why not do this the right way?
the row_number version is like this
select emp.* from emp ,
select deptno, row_number() over (order by avg(sal) desc) r from emp
group by deptno
)e
where e.r = 1
and emp.deptno = e.deptno
If there happens to be a tie (that is, two or more departments have the same average sal, and it is the highest), then the query above will only arbitrarily treat one of them (no telling which one) as the highest. Change ROW_NUMBER to RANK to get all departments with a claim to having the highest average sal.
You could use the ROWNUM pseudo-column instead of ROW_NUMBER, if all you want to do is avoid ROW_NUMBER.
Without using ROW_NUMBER or RANK, there are lots of ways involving other analytic functions, such as AVG and MAX.
If you really, really don't want to use analytic functions at all, you can do this:
SELECT *
FROM scott.emp
WHERE deptno IN (
SELECT deptno
FROM scott.emp
GROUP BY deptno
HAVING AVG (sal) = (
SELECT MAX (AVG (sal))
FROM scott.emp
GROUP BY deptno
;
Similar Messages
-
How to write this query to filter combination of few values
Hi,
I have a table CHIMM which is a transaction table and contains information of the vaccines given to a child.
columns are: child_id, vacc_id, vacc_given_dt. I have to query for remaining vaccines.
HEXA is a vaccine_id which is composite vaccine of DPT1,POL1,HBV1 & HIB1 (vaccine ids).
I want to write to query if any of DPT1,POL1,HBV1 & HIB1 given then HEXA should not be displayed in the result.
OR
if HEXA is given then of course any of DPT1,POL1,HBV1 & HIB1 should not be displayed in the result.
How to write this query?
RegardsHi,
I'm still not sure what the output you want from that sample data is. Do you just want the child_ids, like this
CHILD_ID
3
4? If so, here's one way to get them:
WITH all_vacc_ids AS
SELECT c.child_id
, c.vacc_id AS child_vacc_id
, v.vacc_id
, COUNT ( CASE
WHEN c.vacc_id = 'HEXA'
THEN 1
END
) OVER ( PARTITION BY c.child_id
) AS hexa_itself
FROM vacc v
LEFT OUTER JOIN chimm c PARTITION BY (c.child_id)
ON c.vacc_id = v.vacc_id
WHERE v.vacc_desc = 'HEXA' -- See note below
SELECT child_id
FROM all_vacc_ids
WHERE child_vacc_id IS NULL
AND vacc_id != 'HEXA'
AND hexa_itself = 0
GROUP BY child_id
rha2 wrote:there are alot of vaccines, i just put 3 for example. this query gives error: invalid relational operatorAre you saying that the vacc table contains other rows, but those other rows are not needed for this problem? It would be good if you included an example in the sample data. The query above considers only the rows in vacc where vacc_desc='HEXA'. You can have other rows in the vacc table, but they won't affect the output of this query. The query above makes no assumptions about the number of rows that have vacc_desc='HEXA'; it will report all child_ids who are missing any of them, regardless of the number (assuming the child does not have the 'HEXA' vacc_id itself, like child_id=1).
You still haven't said which version of Oracle you're using. The query above will work in Oracle 10 (and higher). -
How to write this query using correlation subquery or non exists clause
-- Tables description.
--step-1- 4 employees present in EMP table.
--step-2- each employee having 1 country_no in EMP_DOCS table.3 employees having same country_no(i.e emp's 1,2,3)
--step-3- 1 emp document can have multiple items.In this case each employee having one each in the EMP_ITEMS table.
--step-4- 1 EMPLOYEE can have Multiple Documents so we have a relation between EMP_ITEMS and DOCUMENT_ITEMS.whatever items present in EMP_ITEMS those items will be inserted into DOCUMENT_ITEMS.so we have a item-item relation.
--so we stored EMP_ITEMS id in EMP_ITEMS_REF_ID_1 of DOCUMENT_ITEMS table.
-- step-5- DOCUMENT-INVOICE has 1-1 relation once invoice is created we stored invoice id in DOCUMENT table.
--This is the requirement.Let's say in this example 3 employees are using same country_no and 4th employee is using another country_no
--which is not used by other 3 employees.
--Condtion-1:
--if all of the employees have created INVOICE which is using same country_no of different country_no then the query should display all records.
--Condition-2:
--if any one of the employee not created INVOICE which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
--Condition-3:
--if any one of the employee not created even DOCUMENT which is using same country_no of other employees then remaining employees also should not come in the query
-- even though invoice is created by other employees.
I hope I explain the conditions clearly.if you understand well by looking at the data i posted below may i know what is the final result should be displayed?
create table EMP
ID NUMBER not null,
TYPE VARCHAR2(1)
alter table EMP
add constraint ID primary key (ID);
create table EMP_DOCS
ID NUMBER not null,
EMP_ID NUMBER,
COUNTRY_NO VARCHAR2(15)
alter table EMP_DOCS
add constraint PK_EMP_DT primary key (ID);
alter table EMP_DOCS
add constraint FK_EMP_DT foreign key (EMP_ID)
references EMP (ID);
create table EMP_ITEMS
ID NUMBER not null,
EMP_DOC_ID NUMBER
alter table EMP_ITEMS
add constraint PK_EMP_ITEMS_DT primary key (ID);
alter table EMP_ITEMS
add constraint FK_EMP_ITEMS_DT foreign key (EMP_DOC_ID)
references EMP_DOCS (ID);
create table DOCUMENT
ID NUMBER not null,
DOCNO VARCHAR2(15),
INVOICE_REF_1 NUMBER
alter table DOCUMENT
add constraint DOC_PK_ID primary key (ID);
create table DOCUMENT_ITEMS
ID NUMBER not null,
DOC_ID NUMBER,
EMP_ITEMS_REF_ID_1 NUMBER
alter table DOCUMENT_ITEMS
add constraint PK_DOCUMENT_ITEMS_DT primary key (ID);
alter table DOCUMENT_ITEMS
add constraint FK_DOCUMENT_ITEMS_DT foreign key (DOC_ID)
references DOCUMENT (ID);
create table INVOICE
ID NUMBER not null,
INVOICE_NO VARCHAR2(15)
alter table INVOICE
add constraint INVOICE_PK_ID primary key (ID);
INSERT INTO EMP ( ID, TYPE ) VALUES (
1, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
2, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
3, 'A');
INSERT INTO EMP ( ID, TYPE ) VALUES (
4, 'A');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
1, 1, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
2, 2, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
3, 3, 'INDIA');
INSERT INTO EMP_DOCS ( ID, EMP_ID, COUNTRY_NO ) VALUES (
4, 4, 'USA');
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
1, 1);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
2, 2);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
3, 3);
INSERT INTO EMP_ITEMS ( ID, EMP_DOC_ID ) VALUES (
4, 4);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
1, 'DOC1', 1);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
2, 'DOC1', 2);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
3, 'DOC3', 0);
INSERT INTO DOCUMENT ( ID, DOCNO, INVOICE_REF_1 ) VALUES (
4, 'DOC4', 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
1, 1, 1);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
2, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
3, 2, 2);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
4, 3, 3);
INSERT INTO DOCUMENT_ITEMS ( ID, DOC_ID, EMP_ITEMS_REF_ID_1 ) VALUES (
5, 4, 4);
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
1, 'INV1');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
2, 'INV2');
INSERT INTO INVOICE ( ID, INVOICE_NO ) VALUES (
3, 'INV3');
commit;
-- I have written below query to satisfy above conditions but still required results are not coming..
SELECT *
FROM EMP E
WHERE E.TYPE = 'A'
AND NOT EXISTS (SELECT *
FROM EMP_DOCS EDOC1,
EMP_DOCS EDOC2,
EMP E1,
EMP_ITEMS EMPI,
DOCUMENT_ITEMS DOCITM,
DOCUMENT DOC,
INVOICE INV
WHERE EDOC1.EMP_ID = E.ID
AND EDOC2.EMP_ID = E1.ID
AND EDOC1.ID = EMPI.EMP_DOC_ID
AND EDOC1.COUNTRY_NO = EDOC2.COUNTRY_NO
AND EMPI.ID = DOCITM.EMP_ITEMS_REF_ID_1
AND DOCITM.DOC_ID = DOC.ID
AND INV.ID = DOC.INVOICE_REF_1);DB version:oracle 10g;10.2
-
How to write this query.. Please Help
Hello Guys,
I have a table that contains all possible leave types, it is a look up table. Table name is Att_Leave_Types and data in the table is:
Leavetype_ID Leavetype_Desc
1 Annaul
2 Sick
3 Casual
4 Long
5 Maternity
Now, another table Att_Emp_Leaves Contains leaves of an employee
Columns and values are
Emp_ID leave_date levetype_id
14210 28-AUG-06 2
14210 30-AUG-06 3
14210 12-JUL-06 1
14210 13-JUL-06 1
14210 14-JUL-06 1
6902 27-AUG-06 2
6902 30-AUG-06 3
Now i want to get the following result from the query:
Group by month and year
list all leavetypes and leaves employee have availved, if no leavetype availved then the result must show 0 for the leavetype. calculate leaves for a month based on leavetype ID.
This result will help you clear what i result i want by my query.
MON_YYYY employee_id leavetype_id number_of_leaves
JUL-2006 14210 1 3
JUL-2006 14210 2 0
JUL-2006 14210 3 0
JUL-2006 14210 4 0
JU:-2006 14210 5 0
AUG-2006 14210 1 0
AUG-2006 14210 2 1
AUG-2006 14210 3 1
AUG-2006 14210 4 0
AUG-2006 14210 5 0
AUG-2006 6902 1 0
AUG-2006 6902 2 1
AUG-2006 6902 3 1
AUG-2006 6902 4 0
AUG-2006 6902 5 0
Please guide me and help me, its very urgent.
I will be thankful to you.
Regards,
Imran BaigHere we are:
DROP TABLE Att_Leave_Types;
CREATE TABLE Att_Leave_Types
Leavetype_ID NUMBER
,Leavetype_Desc VARCHAR2(30)
INSERT INTO Att_Leave_Types VALUES(1, 'Annual');
INSERT INTO Att_Leave_Types VALUES(2, 'Sick');
INSERT INTO Att_Leave_Types VALUES(3, 'Casual');
INSERT INTO Att_Leave_Types VALUES(4, 'Long');
INSERT INTO Att_Leave_Types VALUES(5, 'Maternity');
DROP TABLE Att_Emp_Leaves;
CREATE TABLE Att_Emp_Leaves
Emp_ID NUMBER
,leave_date DATE
,leavetype_id NUMBER
INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('01-JUL-2006', 'DD-MON-YYYY'), 1);
INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('10-JUL-2006', 'DD-MON-YYYY'), 1);
INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('15-JUL-2006', 'DD-MON-YYYY'), 1);
INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('15-AUG-2006', 'DD-MON-YYYY'), 2);
INSERT INTO Att_Emp_Leaves VALUES (14210, TO_DATE('25-AUG-2006', 'DD-MON-YYYY'), 3);
INSERT INTO Att_Emp_Leaves VALUES (6902, TO_DATE('15-AUG-2006', 'DD-MON-YYYY'), 2);
INSERT INTO Att_Emp_Leaves VALUES (6902, TO_DATE('25-AUG-2006', 'DD-MON-YYYY'), 3);
SELECT
LP.*
,EL.Leave_date
COLUMN Emp_ID FOR 999999
COLUMN Leave_Month FOR A8
COLUMN Leavetype_ID FOR 999
COLUMN Leavetype_desc FOR A10
COLUMN Leave_Count FOR 999
SELECT
LP.Emp_ID
,TO_CHAR(LP.Leave_Month, 'MON-YYYY') Leave_Month
,LP.Leavetype_ID
,LP.Leavetype_desc
,COUNT(EL.Emp_ID) Leave_Count
FROM
SELECT *
FROM
SELECT
Emp_ID
,ADD_MONTHS(Min_Leave_month, RN - 1) Leave_Month
,Max_Leave_month
FROM
SELECT ROWNUM RN FROM
USER_OBJECTS
WHERE ROWNUM <= 100
SELECT
Emp_ID
,TRUNC(MIN(Leave_date), 'MM') Min_Leave_month
,TRUNC(MAX(Leave_date), 'MM') Max_Leave_month
FROM Att_Emp_Leaves
GROUP BY Emp_ID
WHERE ADD_MONTHS(Min_Leave_month, RN - 1) <= Max_Leave_month
,Att_Leave_Types
-- ORDER BY Emp_ID, Leave_Month, Leavetype_ID
) LP -- Leave periods
,Att_Emp_Leaves EL -- Employee leaves
WHERE EL.Emp_ID(+) = LP.Emp_ID
AND EL.Leavetype_ID(+) = LP.Leavetype_ID
AND EL.Leave_date(+) >= LP.Leave_Month
AND EL.Leave_date(+) < ADD_MONTHS(LP.Leave_Month, 1)
GROUP BY
LP.Emp_ID
,LP.Leave_Month
,LP.Leavetype_ID
,LP.Leavetype_desc
ORDER BY
LP.Emp_ID
,LP.Leave_Month
,LP.Leavetype_ID
EMP_ID LEAVE_MO LEAVETYPE_ID LEAVETYPE_ LEAVE_COUNT
6902 AUG-2006 1 Annual 0
6902 AUG-2006 2 Sick 1
6902 AUG-2006 3 Casual 1
6902 AUG-2006 4 Long 0
6902 AUG-2006 5 Maternity 0
14210 JUL-2006 1 Annual 3
14210 JUL-2006 2 Sick 0
14210 JUL-2006 3 Casual 0
14210 JUL-2006 4 Long 0
14210 JUL-2006 5 Maternity 0
14210 AUG-2006 1 Annual 0
14210 AUG-2006 2 Sick 1
14210 AUG-2006 3 Casual 1
14210 AUG-2006 4 Long 0
14210 AUG-2006 5 Maternity 0
BW -
Sql question how to write this query
This is my query:
SELECT msi.segment1 item, bsd.operation_code, bd.department_code,
bsd.operation_description
FROM mtl_system_items msi,
bom_operational_routings bort,
bom_operation_sequences bos,
bom_departments bd,
bom_operation_resources br,
bom_resources bor,
bom_standard_operations bsd
WHERE msi.inventory_item_id = bort.assembly_item_id
AND msi.organization_id = bort.organization_id
AND bort.routing_sequence_id = bos.routing_sequence_id
AND bos.department_id = bd.department_id
AND bd.department_id = bsd.department_id
AND bos.standard_operation_id = bsd.standard_operation_id
AND bos.operation_sequence_id = br.operation_sequence_id
AND bor.resource_id = br.resource_id
AND bos.reference_flag = 1
AND msi.organization_id = '82'
AND bos.disable_date IS NULL
GROUP BY msi.segment1,
bsd.operation_code,
bd.department_code,
bos.operation_description,
bsd.operation_description,
bor.resource_code
Which essentially produces this output:
Item Op code Dept Description
123 10 Warehouse Move parts
123 20 Assembly Finish Parts
123 30 Inspection Complete
I need to capture when the part goes into Inspection and from where it came, so in this case, From Assembly to Inspection.
I don't even know where to start
Thanks for any directionNot sure I've got the columns names right from your example, but does this get you started at all?
It sounds like you might need an analytic function like LAG or LEAD.
with t as
(select 123 item, 10 op, 'Warehouse' code, 'Move' dept, 'parts' description
from dual
union
select 123, 20, 'Assembly', 'Finish', 'Parts'
from dual
union
select 123, 30, 'Inspection', 'Complete',null
from dual)
select *
from (
select item, op, code, dept, description, lag(code) over (partition by item order by op) previous_code
from t
where code = 'Inspection';
ITEM OP CODE DEPT DESCR PREVIOUS_C
123 30 Inspection Complete AssemblyMessage was edited by:
dombrooks -
Help me:how to write this query
i have a table employees,the data is :
LAST_NAME DEP SALARY HIRE_DATE COMMISSION_PCT
gets 10 4000 20060101000000
davis 20 1500 20060303000000
king 20 4000 20051118000000
gets 30 5000 20040101000000
kochhar 5000 20051201000000
higens 40 3500 20050706000000
my question is :
create a query that will display the toatl number of employees and ,the number of employees hired in 2004,2005,2006.as follow:
total 2004 2005 2006
6 1 3 2
thanks a lot.This might help you
SQL> select * from emp;
LAST_NAME DEP SALARY HIRE_DATE COMMISSION_PCT
gets 10 4000 20060101000000
davis 20 1500 20060303000000
king 20 4000 20051118000000
gets 30 5000 20040101000000
kochhar 30 5000 20051201000000
higens 40 3500 20050706000000
6 rows selected.
SQL> SELECT COUNT(1),SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2004,1,0)) "2004",
2 SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2005,1,0)) "2005",
3 SUM(DECODE(SUBSTR(HIRE_DATE,1,4),2006,1,0)) "2006"
4 FROM EMP
5 /
COUNT(1) 2004 2005 2006
6 1 3 2
SQL>Regards,
Mohana
I didn't see Jameel's and APC's solution. They are faster than me :)
Message was edited by:
Mohana Kumari -
How to rewrite this query without sub query please help me
Hello All Good Evening,
Could you please help me with this query, how can i write this query without sub query, or how can write this query another ways
please help me
select planno, status1, count(*) Counts from
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 ---, COUNT (*)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
--group by a.ValetNO
a group by planno, status1
order by 2
Thank you in Advance
MilanWhats your objective here? Sorry, am not able to understand the reason for this change.
Try the below:(Not tested)
;With cte
As
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 ---, COUNT (*)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
select planno, status1, count(*) Counts from cte
a group by planno, status1
order by 2
Even below:
select a.ValetNO PlanNo ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end Status1 , COUNT (1)
from dbo.ppt a(NOLOCK) left join dbo.acts b on a.P_ID = b.P_ID and a.ValetNO = b.ValetNO
Group by a.ValetNO ,
case
when JoinCode in ('00', '01', '02') then 'Actcess'
when JoinCode in ('20', '21', '22', '23','38', '39') then
'Secured' else 'Other' end -
How to write this sql query in php code ?
for example:
insert into temp
select *
from testtable;
after this, i will query data from sql below:
select *
from temp;
how to write this php code ?
who can help me ?
thanks!Have a look at the manual to find out how to issue queries.
http://us3.php.net/oci8 -
How to write sql query with many parameter in ireport
hai,
i'm a new user in ireport.how to write sql query with many parameters in ireport's report query?i already know to create a parameter like(select * from payment where entity=$P{entity}.
but i don't know to create query if more than 1 parameter.i also have parameter such as
$P{entity},$P{id},$P{ic}.please help me for this.
thanksYou are in the wrong place. The ireport support forum may be found here
http://www.jasperforge.org/index.php?option=com_joomlaboard&Itemid=215&func=showcat&catid=9 -
How to write sql query for counting pairs from below table??
Below is my SQL table structure.
user_id | Name | join_side | left_leg | right_leg | Parent_id
100001 Tinku Left 100002 100003 0
100002 Harish Left 100004 100005 100001
100003 Gorav Right 100006 100007 100001
100004 Prince Left 100008 NULL 100002
100005 Ajay Right NULL NULL 100002
100006 Simran Left NULL NULL 100003
100007 Raman Right NULL NULL 100003
100008 Vijay Left NULL NULL 100004
It is a binary table structure.. Every user has to add two per id under him, one is left_leg and second is right_leg... Parent_id is under which user current user is added.. Hope you will be understand..
I have to write sql query for counting pairs under id "100001". i know there will be important role of parent_id for counting pairs. * what is pair( suppose if any user contains both left_leg and right_leg id, then it is called pair.)
I know there are three pairs under id "100001" :-
1. 100002 and 100003
2. 100004 and 100005
3. 100006 and 100007
100008 will not be counted as pair because it does not have right leg..
But i dont know how to write sql query for this... Any help will be appreciated... This is my college project... And tommorow is the last date of submission.... Hope anyone will help me...
Suppose i have to count pair for id '100002'. Then there is only one pair under id '100002'. i.e 100004 and 100005Sounds like this to me
DECLARE @ID int
SET @ID = 100001--your passed value
SELECT left_leg,right_leg
FROM table
WHERE (user_id = @ID
OR parent_id = @ID)
AND left_leg IS NOT NULL
AND right_leg IS NOT NULL
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
How to write a query for the given scenario ?
Hi All ,
I am having two tables EMP, DEPT with the below data.
EMP TABLE :--
EID ENAME JOB SAL DEPID
111 RAM MANAGER 1500 10
222 SAM ASST MANAGER 2000 20
333 KALA CLERK 2500 10
444 BIMA MANAGER 3000 20
555 CHALA MANAGER 3500 30
666 RANI ASST MANAGER 4000 10
777 KAMAL MANAGER 2400 10
DEPT TABLE :--
DEPID DNAME
10 XX
20 YY
30 ZZ
Q1 : I want the sum of salary of each department and for the particular job . Here in each departmant manager, asst. manager, clerk posts are there .
I want to display the result like below ....
JOB 10 20 30
MANAGER 3900 3000 3500
ASST MANAGER 4000 2000 NULL
CLERK 2500 NULL NULL
please tell me how to write a sql query ?
Thanks
SaiIn general case, you cannot write this query.
This is one of the limits of relational database concepts. The number of columns must be known up-front. In the SELECT clause, you have to list and name all columns returned by the query. So you have to know number of departments. (There are some workarounds - you can return one column with concatenated values for all departments, separated by space character).
If you know that you have 3 departments then you qurey will return 4 columns:
SELECT
e.job,
SUM ( CASE WHEN d.deptid = 10 THEN e.sal ELSE NULL END) d10,
SUM ( CASE WHEN d.deptid = 20 THEN e.sal ELSE NULL END) d20,
SUM ( CASE WHEN d.deptid = 30 THEN e.sal ELSE NULL END) d30
FROM dept d, emp e
WHERE d.deptno = e.deptno
GROUP BY e.job -
the query below , i am using in valueset :
select * from AR_LOCATION_VALUES
Where ar_location_values.location_segment_qualifier =
'COUNTRY' and ar_location_values.location_structure_id in ( select location_structure_id from ar_system_parameters )
how to use this query to set in R12?
Thankshi
i am using the following query in 11i and i want to use the same in R12 :
SELECT ar_location_values.location_segment_description,ar_location_values.location_segment_value,location_segment_id
FROM AR_LOCATION_VALUES
WHERE ar_location_values.location_segment_qualifier = 'COUNTRY'
AND ar_location_values.location_structure_id IN
(SELECT location_structure_id FROM ar_system_parameters)
note: the table ar_location_values is obsolette in R12 so what is the replacement table in R12 for this? -
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' -
How to modify this query to get the desired output format
I hv written a Query to display all the parent table names and their primary key columns(relevant to this foreign key of the child table).The query is given below...
SELECT DISTINCT(TABLE_NAME) AS PARENT_TABLE,COLUMN_NAME AS PARENT_COLUMN
FROM ALL_CONS_COLUMNS
WHERE CONSTRAINT_NAME IN (SELECT AC.R_CONSTRAINT_NAME
FROM ALL_CONSTRAINTS AC
WHERE AC.TABLE_NAME=TABLE_NAME
AND AC.TABLE_NAME='&TABLE'
AND AC.R_CONSTRAINT_NAME IS NOT NULL);
This query will display all the parent tables and their primary key columns.Now my problem is that how to modify this query to also display the foreign key column name of the child table.
I want the query result in the following format.The query should display the following columns.1)child table's name,2)child table's foreign key column name,3)the corresponding parent table's name,4)the parent table's primary key column name(which is the foreign key in the child table).
For Example I want the output as follows...
TAKE THE CASE OF SCOTT.EMP(AS INPUT TO YOUR QUERY)
CHILD_TABLE CHILD_COLUMN PARENT_TABLE PARENT_COLUMN
EMP DEPTNO DEPT DEPTNO
In this result I hv used alias name for the columns.The query should display this only for the foreign keys in the child table.In the query which I sent to you earlier will give the parent table and the parent column names,But I also want to append the child table and child column names there.
any help on how to tackle would be appreciated.Try this query
SELECT c.table_name child_table,
c.column_name child_column,
p.table_name parent_table,
p.column_name parent_column
FROM user_constraints a,user_constraints b,user_cons_columns c,
user_cons_columns p
WHERE a.r_constraint_name=b.constraint_name and
a.constraint_name=c.constraint_name and
b.constraint_name=p.constraint_name and
c.position=p.position
ORDER BY c.constraint_name,c.position
Anwar -
hi,
i have a product table like
product month1 month2 month3 .................
soap 1200 1256 1895 ............
i want use a query where i can select column name with a parameter.
like
select month||:num from product;
in num variable it cud be 1 to 10 of value that is dependent on my program.
so how to make this query .
thxsHi,
Here is an example that i am helpful.
In the example , I am using a table 'table_name' which contains columns like
assign_attribute1
assign_attribute2
assign_attribute15
Now I will pass any number from 1 to 15 to the function.
create or replace procedure pass_col_number(v_number varchar2) as
v_sql varchar2(2000);
v_assign_attribute1 varchar2(150);
begin
v_sql := 'select assign_attribute'||v_number||' from table_name where person_id = 1345';
execute immediate v_sql into v_assign_attribute1;
dbms_output.put_line('v_assign_attribute1='||v_assign_attribute1);
end;Edited by: Sreekanth Munagala on Dec 18, 2008 1:18 AM
Maybe you are looking for
-
Printing of Material desc on change of material no in the ADOBE form
Hi Experts, I want to print material description on change of material no. if there is 4 line item in same matnr then it will show only one desc for this. And on change of matnr it will change and total will print over there. for the 2nd MATNR desc w
-
Old PC crashed - Will new PC delete songs if I sync?
My old PC crashed and I bought a new one. My iTunes file says my iPod is in use under a different folder. It has brought back up all of my purchases, so I am safe there if I sync. However, I have a ton of music from CD that it won't bring back up and
-
Please recommend replacement Hard Drive for imac G5 17in
My 160gb drive died, and I would like to replace with a larger drive. Can I please have your recommendations for a replacement. The original drive is a Seagate Barracuda 7200.7 Model # st3160023as I'd like to find something at least 250gb and 100% im
-
I formatted my BOOTCAMP partion recently and just stuck with the default disk title BOOTCAMP. My first time, last year, I named it Windows. When I select the start-up disk, however, it still says Windows while everything else rightly says BOOTCAMP. F
-
Editing HTML through the browser itself
Today, I went to JotSpot and reached the following page saying that Safari is not supported [from http://feedback.jot.com/KnownIssues/]: Mac Support <item>1. Mac support: WYSIWYG will work with the Firefox browser (free, open-source), but not with Sa