Return value in SQL [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In my sql table, i have schema like,
username, FirstName, LastName, empID, manager
Jdoe John Doe 1234 Tango Charlie
Tcharlie Tango Charlie 5678 XYZ
I want to write a query that will give me, user's manager's empID.
so output should be,
Jdoe John Doe 1234 Tango Charlie 5678
I have like, 5000 records, and wanted to get a employeeID for all user's managers.

SELECT
yt1.username
, yt1.firstname
, yt1.lastname
, yt1.empid
, yt1.manager
, yt2.empid
FROM dbo.YourTable yt1
JOIN YourTable yt2 ON yt1.manager = yt2.firstname + ' ' + yt2.lastname

Related

SQL Query for desired output [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table which contains countries:
Countries
============
Australia
South Africa
Bangladesh
New zeland
Sri Lanka
England
...
Desired out put is
Country
===========
India
Sri Lanka
Followed by other countries in `Asc` or `Desc`
You can use CASE:
SqlFiddleDemo
SELECT *
FROM Countries
ORDER BY
CASE Name
WHEN 'India' THEN 0
WHEN 'Sri Lanka' THEN 1
ELSE 10
END ASC,
Name ASC -- DESC

SQL Query combination [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have following data on table User.
EMPID FIRSTNAME LASTNAME MANAGER
---------------------------------------
10017 Pawan Kumar 3448
3448 Anwar Sadad 1088
1088 Anand R 3525
I have 3 queries
SELECT * FROM USERS WHERE EMPID='10017';
SELECT FIRSTNAME,LASTNAME,MANAGER FROM USERS WHERE EMPID='3448';
SELECT FIRSTNAME,LASTNAME FROM USERS WHERE EMPID='1088';
Please help me combine these three query or a new query where I can select first and last name of the Manager of employ with empid 3448 who is the manager of 10017.
How can I select 1088 (Anand R) who is the Manager two level manager of 10017 (Pawan Kumar)
answer to your question will be this quesry if i have got your question right
SELECT FIRSTNAME,LASTNAME
FROM USERS
WHERE EMPID=(SELECT MANAGER
FROM USERS
WHERE EMPID=(SELECT MANAGER
FROM USERS
WHERE EMPID='10017'));
if you want to select the manager and again manager of employee with empid 10017

Approver and Submitter Same Person in SQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to identify situations in a vacation booking database where an approver and submitter are the same person. Data looks like this:
TIME VACATION BOOKING ACTION NAME
1:00:00 1 SUBMIT Mike
1:01:00 1 APPROVE Mike
1:02:00 2 SUBMIT Jane
1:03:00 2 APPROVE Mike
Is "Count" the most efficient way to do this in SQL Queries?
I would want to "catch" the Mike results in Vacation Booking 1 above.
You could use count, but I would prefer a self-join
SELECT * FROM Bookings B1
INNER JOIN Bookings B2
ON B1.[Vacation Booking]= B2.[Vacation Booking]
AND B1.Action = 'SUBMIT'
AND B2.Action = 'APPROVE'
AND B1.Name = B2.Name

Find who gets the maximum salary in each location [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have two table as follows:
employ_offical:
id name salary
1 raj 5000
2 ram 8000
3 balu 3000
4 david 4000
employee personal:
name location
raj india
ram china
balu india
david china
From these two tables, I need to know how to retrieve who gets maximum salary in every location; in one location one person should come under these category.
How to retrieve who gets maximum salary in every location?
Here is one way to solve this:
select p.location, p.name, o.salary
from employ_offical o,
employee_personal p
where o.name = p.name
and o.salary = (
select max(o2.salary)
from employ_offical o2,
employee_personal p2
where o2.name = p2.name
and p2.location = p.location);
There is a SQL Fiddle here.
Please note that I have joined on name here as it is the only join available, but from a design perspective it would be much better to have id in both tables and join on id.
This should work:
SELECT eo.name, ep.location, eo.salary
FROM employ_offical eo
INNER JOIN employee_personal ep ON eo.name = ep.name
WHERE eo.salary = (SELECT MAX(salary)
FROM employ_offical eo1
INNER JOIN employee_personal ep1 ON eo1.name=ep1.name
WHERE ep.location = ep1.location
)
It might not be the most efficient or pretty way to do this though.
Note that if multiple persons in the same location share the max salary for that location this query will return all of them.

update row value with another row value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
we have the below table
COUNTRY TOP_COUNTRY
-------------------
ST. HELENA OTHERS
BARBADOS OTHERS
UNITED STATES UNITED STATES
**RUSSIA OTHERS**
NETHERLANDS OTHERS
**GERMANY OTHERS**
ANGUILLA OTHERS
AUSTRALIA AUSTRALIA
CHINA CHINA
I would like to update TOP_COUNTRY row value for a few countries with the names as shown in COUNTRY column.
For eg:
Right now, we RUSSIA shown as 'OTHERS' in TOP_COUNTRY but i would like to update it to the 'RUSSIA'.
This needs to be done for a couple of values..
Can you please let me know how we can get this done..
You can do:
UPDATE tableName
SET TOP_COUNTRY = COUNTRY
WHERE <YourCLause>
In if you want a list of COUNTRY to be updated you can do:
WHERE COUNTRY IN ("COUNTRY1","COUNTRY2",...);
update [YourTableName] set top_country = 'RUSSIA'
where COUNTRY='RUSSIA'