find records with same key but different second column value - sql

My Input data is :
Id salary
101 1000
101 1000
102 2500
102 3000
105 5000
105 5000
105 5000
106 12
106 142
106 12
Output :
102 2500
102 3000
106 12
106 142
106 12
I mean based on the id , I want to find out which Id has different salary. If the salary is the same in all the records , I want to discard those records. Kindly help.

select *
from t
where id in(
select Id
from t
group by Id
having max(salary) <> min(salary)
)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
Fiddle

SELECT `Id`, `salary` FROM salary
WHERE Id IN( SELECT Id FROm salary GROUP By Id HAVING COUNT(DISTINCT SALARY) > 1)
Id
salary
102
2500
102
3000
106
12
106
142
106
12
fiddle

Related

DolphinDB - Fill missing values of a sequence

I have a table t
t=table(100 101 103 106 as price,take(10,4) as n)
price n
100 10
101 10
103 10
106 10
For the missing values of the column price, like 102, 104, and 105, I want to fill it with n=0.
price n
100 10
101 10
102 0
103 10
104 0
105 0
106 10
Not sure how will you generate the price seq, but you can do something like:
t=table(100 101 103 106 as price,take(10,4) as n)
t2=table(100..106 as price)
select price, n.nullFill(0) from t2 left join t on t2.price = t.price

I'm having this error when compiling my code: value with SQL

Team numbers are found by dividing the employee id by four. If 4 divides the employee id evenly, then that employee is part of team 1. For the other cases, a remainder of 1 places that employee on team 2, etc.
I got this result when I run the Code
SELECT EMPLOYEE_ID, MOD(EMPLOYEE_ID, 4) AS "TEAM"
100 0
101 1
102 2
103 3
104 0
105 1
106 2
107 3
but I need to get the following result:
100 1
101 2
102 3
103 4
104 1
105 2
106 3
107 4
Trivially, you may just add one to result of the MOD function:
SELECT
EMPLOYEE_ID,
MOD(EMPLOYEE_ID, 4) + 1 AS "TEAM"
FROM yourTable;

Oracle select query based on multiple conditions

MESSAGE_ID GROUP_ID REV_NO
100 200 1
101 201 1
102 202 1
103 203 1
104 204 1
105 200 2
106 201 2
107 202 2
108 203 2
109 204 2
110 205 2
First I want to select all group ID's and their correpsponding lowest revision number.
Then I want select first X message ID's (Controllable X input) with condition that it should contain all the revisions of of any selected group. For e.g if I select first 5 messages by rownum then all revisions of group_id 200 is not selected.
Hope I made it clear.

update corresponding value from other table

I have two tables named sales and login.My table structure is given below.Some times my program update the custid instead of userid in sales table column userid, but the logid updated correctly in sales table. I have the another table tbl_log shown below. I want to update the sales table userid based on logid using the tbl_log.
sales table
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 S1003
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 S1006
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 S1009
9 S1010 104 31 d2124
10 S1011 104 60 S1011
Log Table
Fld_log_id Fld_user_id
101 d2121
102 d2122
103 d2123
104 d2124
Exact output
Fld_id Fld_cust_id Fld_log_id Fld_amount Fld_user_id
1 S1002 101 100 d2121
2 S1003 102 121 d2122
3 S1004 103 120 d2123
4 S1005 102 130 d2122
5 S1006 102 1234 d2122
6 S1007 102 111 d2122
7 S1008 103 21 d2123
8 S1009 103 234 d2123
9 S1010 104 31 d2124
10 S1011 104 60 d2124
To update the values in sales based on the values in the log table you do:
UPDATE sales S
SET S.Fld_user_id = (SELECT l.Fld_user_id
FROM logSales l
WHERE l.Fld_log_id = s.Fld_log_id);
sqlfiddle demo

ORA 00918- Column ambiguosly defined error [duplicate]

This question already has answers here:
ORA-00918: column ambiguously defined in SELECT *
(4 answers)
Closed 9 years ago.
There are two tables in my Oracle database.
First table is (customers)-
customer_id Customer_name Customer_age Customer_address salary
103 Giriraj Rathi 22 Kolkata 12000
100 Subir Adhikari 22 Bolpur 10000
101 Rakesh Chatterjee 21 Tarkeshwar 8000
102 Jayanta Patra 20 Tarkeshwar 9000
104 Abhi Karmakar 22 Burdwan 8000
105 Mainak Manna 21 Burdwan 9000
106 Subho Gupta 20 Kolkata 10000
107 Aritra Das 23 Kolkata 7000
108 Pradip Paul 22 Kolkata 5000
109 Sourav Banerjee 22 Bolpur 9000
Second table is (Orders):
Order_id Order_date customer_id amount
200 12-03-13 100 1100
201 09-05-13 101 1400
202 07-04-12 103 2500
204 29-05-13 104 2400
203 09-02-13 105 9000
205 18-06-13 106 2100
206 09-07-13 107 1600
207 18-05-13 108 2900
209 18-04-13 109 2400
Now I wanted to join both the tables. So I used the query:
select customer_id,
customer_name,
customer_address,
order_id,order_date,
amount
from customers,
orders
where customers.customer_id=orders.customer_id;
I Googled about the error and found this happens when there is ambiguity in the SQL code itself, but in this case I see nothing.
It is always a good idea to add the table name/alias to the column like this
select c.customer_id,
c.customer_name,
c.customer_address,
o.order_id,
o.order_date,
o.amount
from customers c
inner join orders o on c.customer_id = o.customer_id
If you don't then the DB don't know which column to take and both tables have a column named customer_id.