need help to build a query that takes the dynamic column name return by another query as record - oracle11g

I have 2 tables:
Employee_tbl
empid empname empsalary emplocation
----- ------- --------- ------------
1 santhosh 15000 East godavari
2 Srinivas 25000 Westgodavari
3 sandeep 35000 Hyderabad
4 prathap 55000 Hyderabad
5 praveen 45000 West godavari
configuration_tbl
config_id column_name
--------- -----------
1 empid
2 empname
3 empsalary
4 emplocation
When I pass input as config_id it should display the values from that column.
Ex: If I pass config_id then it should display all empname from employee_tbl.

When there is only a limited number of columns I advice you to use a SQL with a case statement. Depending on whether you want to fold numbers into one text field or not, you may need two case columns. Execute immediate is possible but has additional overhead.

Related

MS Access Query counting instances

I have a sample table with following values
SNO | Mon
-----+-------
100 | 1
101 | 1
102 | 1
100 | 2
101 | 2
102 | 2
100 | 3
101 | 3
Now I need a query to count the total sno's which are in 3 months
The result should be 2, as 100 & 101 are in mon 1,2 and 3. However, 102 is only present in mon 1,2.
Thanks,
RK
This Query in theory should work.
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo));
The result would be,
sNo
----
100
101
I have used two SubQueries to get the result. Teh both are used in the HAVING clause of the SQL. First SqubQuery (inner most). Will get the number of Unique Month's available in your table, the outer SubQuery will then Count the number of Unique months. So the Overall Query can be translated as "SELECT the serial number FROM the table HAVING the Count of Month equal to the Number of unique records in the same table".
The reason I used SbQuery instead of a number is because of the fact this will also be applicable when your month number increases. Hope this helps !
EDIT
Here is the Query for getting the count.
SELECT
Count(*) As simpleCount
FROM
(
SELECT
tmpTbl.sNo
FROM
tmpTbl
GROUP BY
tmpTbl.sNo
HAVING
Count(tmpTbl.monNo) = (SELECT Count(*) FROM (SELECT tmpTbl.monNo FROM tmpTbl GROUP BY tmpTbl.monNo))
);

copying data from one table to another table

MY tables
table name:emp
Name Null Type
------------------------------ -------- --------------------------------------------------------------- NUMBER
EMP_NAME VARCHAR2(10)
ADDRESS VARCHAR2(15)
PH_NO NUMBER(10)
DPT_NO NUMBER
result:
1 ram ctr 8892939927 100
2 mohan ptr 7569936347 101
3 mallu ppt 9553438342 102
4 scoot dmc 9874563210 103
5 jim plr 9236548875 104
6 ravi tpt 8562398756 105
7 manju hyd 7562398742 106
8 manoj hyd 869523654 107
9 sarath ctr 9632158769 108
10 hemanth mpk 9632147852 109
table anme: emp_department
Name Null Type
------------------------------ -------- -------------------------------------------------------------
EMP_NO NUMBER
DPT_NO NUMBER
PERIOD VARCHAR2(10)
now how to exatract(copy) emp_no and dpt_no values to emp_ department
I think you mean you want to populate emp_department from the data in the emp table - hopefully so the dpt_no column in that table can be removed. You haven't shown the emp_no column in emp, but it was in your earlier questions so I assume that's a typo. Your data model still seems a bit strange.
So to insert values from another table:
insert into emp_department (emp_no, dpt_no)
select emp_no, dpt_no
from emp;
You seem to be asking vey basic questions without showing much research. Please read the documentation or at least some tutorials and show what you've tried and what specific problems you're having.

column is amguously defined in column

my tables
DESC emp
Name Null Type
------------------------------ -------- --------------------------
EMP_NO NUMBER
EMP_NAME VARCHAR2(10)
ADDRESS VARCHAR2(15)
PH_NO NUMBER(10)
DEPT_NO NUMBER
result:
1 ram ctr 8892939927 100
2 mohan ptr 7569936347 101
3 mallu ppt 9553438342 102
4 scoot dmc 9874563210 103
5 jim plr 9236548875 104
6 ravi tpt 8562398756 105
7 manju hyd 7562398742 106
8 manoj hyd 869523654 107
9 sarath ctr 9632158769 108
10 hemanth mpk 9632147852 109
desc salary
Name Null Type
------------------------------ -------- --------------------------
EMP_NO NUMBER
SALARY NUMBER(10)
PERIOD VARCHAR2(10)
START_DATE DATE
END_DATE DATE
result:
1 12580 15months 12-DEC-07 10-DEC-10
2 15500 19months 10-JAN-07 10-DEC-11
3 7777 18months 11-JUL-07 21-APR-11
4 9999 11months 07-JUL-07 31-JAN-11
5 8500 9months 12-MAR-07 27-MAR-11
6 10000 20months 17-SEP-07 01-AUG-11
7 25000 7months 17-NOV-07 26-JUL-11
8 100000 6months 05-MAY-07 21-JUN-11
9 35000 16months 28-FEB-08 21-JUN-11
10 5000 16months 02-DEC-08 19-AUG-11
joinning query :
select emp_no,
emp_name,
dpt_no,
salary
from emp
join salary on emp.dpt_no=salary.dpt_no
but am getting error is"column is amguously defined". How to resolve this problem?
You need to fully qualify the columns in the select list (the way it's done in the JOIN condition). Otherwise Oracle wouldn't know from which table the column dept_no should be taken.
select emp.emp_no,
emp.emp_name,
emp.dpt_no,
salary.salary
from emp
join salary on emp.dpt_no=salary.dpt_no;
It's good coding style to always qualify the columns - at least in a query involving more than one table - even if they are not ambigous.
If you don't want to type the full table name, you can use a (maningful) alias:
select emp.emp_no,
emp.emp_name,
emp.dpt_no,
sal.salary,
sal.period
from emp
join salary sal
on emp.dpt_no = sal.dpt_no;
If the columnname is the same in tables (salary and emp) and youre joining the tables, you have to specify form wich table you want to selecte the column (salary or from emp)
in youre case the solution is to use salary.dpt_no instead of dpt_no
select emp_no,emp_name, salary.dpt_no,salary from emp join salary on emp.dpt_no=salary.dpt_no

Filtering in Oracle based on a group of values contained in a list of values

I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.

Getting an SQL statement to output a table in a certain order

I'm doing a test web shop that has sections within sections. There can be an unlimited number of levels so I have just one table Section.
The table has the following columns:
SectionID, SectionTitle, SectionLevel, ParentID, PageOrder
SectionLevel: 1 being topmost (no parent)
PageOrder: Within it's parent group, which order it should go in.
And for test data :
SectionID SectionTitle SectionLevel ParentID PageOrder
--------- ------------ ------------ -------- ---------
2 Ladies 1 0 2
3 Mens 1 0 3
4 Jewellery 2 2 1
5 Clothing 2 2 2
6 Clothing 2 3 1
7 Accessories 2 3 2
I want to return this data in one table so that the first top level section is first, and then all of it's children's sections are next, and then the second top level section etc.
I've had a play around with it but can't get to come out right. I think that it should be possible to do it if I redesigned the table but can't think how.
The data should come back in the following order:
SectionID SectionTitle SectionLevel ParentID PageOrder
--------- ------------ ------------ -------- ---------
2 Ladies 1 0 2
4 Jewellery 2 2 1
5 Clothing 2 2 2
3 Mens 1 0 3
6 Clothing 2 3 1
7 Accessories 2 3 2
WITH tree (SectionID, ParentID, SectionLevel, SectionTitle) AS
(
SELECT SectionID, ofs.ParentID, ofs.SectionLevel, ofs.SectionTitle
FROM Section ofs
WHERE ofs.ParentID = 0
ORDER BY SectionID
UNION ALL
SELECT SectionID, ofs.ParentID, ofs.SectionLevel, ofs.SectionTitle
FROM Section ofs
JOIN tree ON tree.ID = ofs.ParentID
ORDER BY PageOrder
)
This is deliberately slightly niaive to make it easier on my fingers. It can be tuned if necessary to allow more nodes in the tree to be accomodated.
DECLARE
#maxPageOrder INT,
#maxLevel INT,
#multiplier INT
SELECT
#maxPageOrder = MAX(PageOrder) + 1,
#maxLevel = MAX(SectionLevel)
FROM
Section
SELECT
#multiplier = POWER(#maxPageOrder, #maxLevel - 1)
;
WITH
recursed_tree
AS
(
SELECT
SectionID AS SectionID,
PageOrder * #multiplier AS finalOrder,
#multiplier / #maxPageOrder AS multiplier
FROM
Section
WHERE
ParentID = 0
UNION ALL
SELECT
child.SectionID,
parent.finalOrder + child.PageOrder * parent.multiplier,
parent.multiplier / #maxPageOrder
FROM
recursed_tree AS parent
INNER JOIN
Section AS child
ON child.ParentID = parent.SectionID
)
SELECT
Section.*
FROM
Section
INNER JOIN
recursed_tree
ON Section.SectionID = recursed_tree.SectionID
ORDER BY
recursed_tree.finalOrder
Note: This assumes all page orders start from 1, rather than 0.
Here's the messy solution I was talking about..
select
a.SectionID, a.SectionTitle, a.SectionLevel, a.ParentID, a.PageOrder
from
section a
left outer join section b on b.SectionID=a.ParentID
left outer join section c on c.SectionID=b.ParentID
order by
c.PageOrder, b.PageOrder, a.PageOrder
As I mentioned in the comment, better to do it in code.. this is only for 3 levels (you could make it more by adding another outer join and order by clause).
This is rather complicated in SQL. If you have access to Safari Books, there are some relevant sections in Joe Celko's Trees and Hierarchies in SQL for Smarties.

Resources