column is amguously defined in column - oracle11g

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

Related

How to query a table with a Where clause that loops through a different table?

I have 2 tables in my database
Table 1
DC Item Day
6006 123 May 1
6006 123 May 2
7036 456 May 6
Table 2
DC Item Day ShippedCases Label Type
6006 123 May 1 100 A
6006 123 May 2 200 A
6006 123 May 2 500 B
7036 456 May 2 300 B
7036 456 May 6 400 A
Table 1 has all the unique records that I am interested in but it doesnt contain ShippedCases or Label Type. How can i write a query to select all the records from Table 2 that match the records in Table 1?
In this case I want to select the DC, Item, Day fields in Table 2 that Match Table 1 and sum the Shipped Cases where Label Type = A.
Result View
DC Item Day Shipped Cases Label Type
6006 123 May 1 100 A
6006 123 May 2 200 A
7036 456 May 6 400 A
I think this is a simple Select statement but I am not where how to setup my where clause with my first 3 parameters being the Table 1 fields and a 4th parameter of Table 2 Label Type = A.
Any advice would be appreciated!
You want to use an INNER JOIN here and aggregation with SUM() and a GROUP BY
SELECT
table1.DC,
table1.Item,
table1.Day,
Sum(table2.ShippedCases) as ShippedCases,
Table2.LabelType
FROM
Table1
INNER JOIN Table2
ON Table1.DC = Table2.DC
AND Table1.Item = Table2.Item
AND Table1.Day = Table2.Day
WHERE
Table2.LabelType = 'A'
GROUP BY 1,2,3,5
If you have items in Table1 that don't appear in Table2, then you'll want to convert this to a LEFT OUTER JOIN:
SELECT
table1.DC,
table1.Item,
table1.Day,
Sum(table2.ShippedCases) as ShippedCases,
Table2.LabelType
FROM
Table1
LEFT OUTER JOIN Table2
ON Table1.DC = Table2.DC
AND Table1.Item = Table2.Item
AND Table1.Day = Table2.Day
AND Table2.LabelType = 'A'
GROUP BY 1,2,3,5
Notice that we move the WHERE clause up into the ON clause of the LEFT OUTER JOIN to insure that only items in Table2 are restricted for that LabelType. In other words, Teradata will filter Table2 BEFORE joining to Table1. BEcause it's a LEFT OUTER JOIN you will get ALL records from Table1, and then only those from Table2 that pass the filter and match the ON conditions.

Query will not give exact maximum salary result

It's a question I got this afternoon:
There a table contains ID, emp_Name,emp_mailid and Salary of Employees, get names of the first-highest salary employees, in oracle
here this is my table
id emp_name emp_mailid salary
2 dinesh dinesh#gmail.com 5000
3 ganesh ganesh#gmail.com 6000
6 ramesh ramesh#gmail.com 4500
10 suresh suresh#gmail.com 10000
11 rajesh rajesh#gmail.com 15000
15 kamesh kamesh#gmail.com 16000
16 kamalesh kamalesh#gmail.com 7800
19 neelash neelash#gmail.com 12563
20 rajan rajan#gmail.com 156231
22 vignesh vignesh#gmail.com 45220
30 rubesh rubesh#gmail.com 78000
31 john john#gmail.com 6522
and this my query:
select *
from
(
select
salary
,dense_rank() over (order by salary desc) ranking
from test
)
where ranking = 1
when i execute this it shows maximum salary as 78000 but actually its wrong maximum salary is 156231
Can you suggest me a better query?
If it shows you the maximum as 78000, it's probably because the salary column is varchar and not number.
try this:
select *
from
(
select
salary
,dense_rank() over (order by to_number(salary) desc) ranking
from test
)
where ranking = 1

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))
);

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

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.

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.

Resources