hai friends
i am having the table like this
TBLKEY EMPKEY EMPNAME
----------- ------------------------------ ------------------------------
1 101 RAJA
2 105 RAJA
3 106 RAJA
4 110 RAJA
i want to to update like this
TBLKEY EMPKEY EMPNAME
----------- ------------------------------ ------------------------------
1 101 RAJA
2 105 POOJA
3 106 THRIU
4 110 POOJA
here i sholud use only one query.i run that query i sholud get the output like this not updating one by one
Try it like this:
UPDATE myTable
SET EMPNAME = CASE WHEN TBLKEY = 2 THEN 'POOJA'
WHEN TBLKEY = 3 THEN 'THRIU'
WHEN TBLKEY = 4 THEN 'POOJA' END
WHERE TBLKEY IN ( 2, 3, 4 )
Have a look:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=96260
http://www.eggheadcafe.com/community/aspnet/13/10126702/update-single-column-with-multiple-rows.aspx
Sure this links will give you idea to proceed.
Related
If this is what my table looks like below:
my_id my_words my_people my_number
100 need more info? Jim 1
100 now Mary 2
100 what's that? Jim 3
101 okay now Jim 1
101 sounds good Mary 2
102 still hungry? Jim 1
102 now I'm thirsty though Mary 2
102 I don't understand Jim 3
102 no I'm not hungry Mary 4
103 are you there? Jim 1
103 I don't know Mary 2
103 That's okay Jim 3
How can I get this output?
my_id my_words my_people my_number
100 need more info? Jim 1
100 now Mary 2
100 what's that? Jim 3
102 still hungry? Jim 1
102 Now I'm thirsty though Mary 2
102 I don't understand Jim 3
right now I have: SELECT my_id, my_words, my_people, my_number from table where my_people="Mary" AND lower(my_words) like 'now%';
But I don't only want to return those rows, I also want to return Jim's comment right before and right after Mary's (before/after based on my_number column)
Maybe this is unrelated, but ultimately, I'm going to want this in Excel with this format:
my_id Jim_words Mary_words Jim_next_words
100 need more info? now what's that?
102 still hungry? now I'm thirsty though I don't understand
Could you please try below code? Code has explanations as comment.
WITH cte as (SELECT my_id, my_words, my_people, my_number
row_number() over( partition by my_id order by my_number) as rn --giving a unique row number for a my id
from table)
SELECT
distinct mytab.my_id, chosentab.my_words jims_words, mytab.my_people, mytab.my_number,
case when case when lower(mytab.my_words) like 'now%' then mytab.rn+1 end = chosentab.rn then chosentab.my_words end jims_words_after_marys_now,
case when case when lower(mytab.my_words) like 'now%' then mytab.rn-1 end = chosentab.rn then chosentab.my_words end jims_words_before_marys_now
FROM
cte mytab,
cte chosentab
where
mytab.my_id=chosentab.my_id and
case when lower(mytab.my_words) like 'now%' then mytab.rn+1 end = chosentab.rn and -- selecting jims rows where mary said now after jim
case when lower(mytab.my_words) like 'now%' then mytab.rn-1 end = chosentab.rn -- selecting jims rows where mary said now before jim
Now I created the SQL based on our discussion. Could you please validate and let me know it if worked?
I have a two table.One of them is student, the other one is salary.
Student table
id | name | code | status
1 | steven | 123 | 100
2 | joe | 678 | 200
3 | paul | 758 | 100
Salary table
id | code | status | currency
1 | 123 | 100 | euro
2 | 678 | 200 | dolar
3 | 758 | 520 | yuan
I want to delete row1 from Student table and row 1 and 2 from Salary table because code and status fields
are same.
I write that query
delete a,b Student as a , join Salary as b
on a.code= b.code and a.status = b.status
but it is not working.I want to delete rows with one query.Do you have any idea?
Would something like this do? PL/SQL, though, not SQL.
Initial data sets:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
1 steven 123 100
2 joe 678 200
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
1 123 100 euro
2 678 200 dollar
3 758 520 yuan
Remove common (CODE, STATUS) combinations:
SQL> begin
2 for cur_r in (select code, status from student
3 intersect
4 select code, status from salary
5 )
6 loop
7 delete from student where code = cur_r.code and status = cur_r.status;
8 delete from salary where code = cur_r.code and status = cur_r.status;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
Result:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
3 758 520 yuan
SQL>
You can use two statements to do so easily:
delete student s where exists(
select * from student stu inner join salary sal on stu.code=sal.code and stu.status=sal.status and stu.id=s.id);
delete salary sal where not exists (select code from student stu where stu.code=sal.code);
First one to delete all the students having same code and status in both tables and second is to delete all the rows from salary table where code doesn't exist in student table.
I having trouble with this query
it is executing quit well but I cannot make out
how is this select statement working.
Any help or explanation on this problem will be appreciated ..
thank you
these are my tables and query
here am looking for the employee who lives in same city as the the company for which they work
Table:-emp
eid name street city
----------- ---------------- ------------- ------------
1 yeman asd vasai
2 aksh adssd mumbai
3 chintan ghfgh mumbai
4 samual ghfdgh bandra
5 ddlj fghfgh andheri
6 jack fghnfg Bandra
7 bridge gfhfgh vasai
8 rahim ghfgh mumbai
9 chirag fghfghfg bandra
10 mistry hhhty bandra
11 ravi tytey andheri
Table:- company
cid companyname city
----------- ------------------- ------------
1 Vasai Industries vasai
2 Mumbai Pharmacy mumbai
3 bandra loft bandra
4 andheri tactics andheri
Table:= works
eid cid salary
----------- ----------- -----------
1 1 200
2 3 4831
3 4 4457
4 2 20001
5 1 32221
6 2 224
7 3 784
8 1 336
9 3 2489
10 2 4789
11 1 22541
Query
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
why not use this query with joins and its easy to understand then a bunch of subqueries.
select * from emp
inner join works on works.eid = emp.eid
inner join company on company.city=emp.city
Explanation:
1.select cid from company where city=emp.city
Here you are getting city id regarding cities which are same in emp and company
2.
select eid from works
where cid=(select cid from company
where city=emp.city)
Here you getting collection of id's from works table which cid is same in emp and company
3.
select * from emp
where eid
IN (select eid from works
where cid=(select cid from company
where city=emp.city))
here you are getting all records based on emp id's whose cities are same in emp and city
If I wanted to find all values in a table that occur more than twice without using group by, how would I do that? I understand how to do this with group by and was curious how to do it without group by (EDIT: could you do this with join?).
For example, if I had last names in a certain zip code, and I wanted to find entries with this last name more than twice, how would I do this without group by in SQL statements?
I tried
select name, count() from population order by name asc having count() > 2;
but that doesn't do what I want it to. Any suggestions?
Being this tagged only as sql it seems a general solution is being looked for. Since the SQL:2003 revision it should be fair to say that this can be solved with window functions:
SELECT name FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) rank,
name
FROM population
) s
WHERE rank = 3
See a sample fiddle here.
Anyway, the fact that it is possible to solve this without a GROUP BY doesn't mean that it should :)
It seems correlated query can work for you. Please check.
Assuming the data set given below
Id Zip Lastname
--- ----- --------
101 12345 John
102 12345 John
103 12345 John
104 12345 Ram
105 12345 Kelly
106 12345 Kelly
107 45678 Krishna
108 45678 Krishna
109 45678 Krishna
110 45678 David
111 45678 David
Query
select * from test.population pop1
where 2 < (select count(*) from test.population pop2
where pop1.Lastname=pop2.Lastname and pop1.Zipcode = pop2.Zipcode)
The output of above query is
Id Zip Lastname
--- ------ --------
101 12345 John
102 12345 John
103 12345 John
107 45678 Krishna
108 45678 Krishna
109 45678 Krishna
I have given the required table and resultant table below. Give query for SQL server 2005.
------------------
ID Name Mgr
------------------
1 Ajay 4
2 Vijay 5
3 Nayeem 1
4 Rakesh 3
5 Varun 4
6 Bhupesh 1
-----------------
I want the result as
-----------------
Name Mgr
-----------------
Ajay Rakesh
Vijay Varun
Nayeem Ajay
Rakesh Nayeem
Varun Rakesh
Bhupesh Ajay
Select sub.name , sup.name
from yourtab sub
join yourtab sup
on sup.id = sub.mgr
;