SQL multiple columns value into one column - sql

Here is my table design
Location Inventory
ID Name HostName LID1 LID2 LID3
1 AAA Peter 1 2 3
2 BBB Betty 2
3 CCC Charlie 1 2
As my expected result is like this below.
HostName Name
Peter AAA
BBB
CCC
Betty BBB
Charlie AAA
BBB
But I run the sql statement not like this.
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID1 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID2 = location.ID
UNION
SELECT HostName,location.Name AS Department
FROM inventory
INNER JOIN location ON inventory.LID3 = location.ID
HostName Name
Peter AAA
Peter BBB
Peter CCC
Betty BBB
Charlie AAA
Charlie BBB
Anyone can help me to solve the problem?
Thanks.

Related

finding manager id from employee table

I have a table data like in the below.
Emp_id Emp_name Dept_id
111 aaa 1
222 bbb 2
333 ccc 3
444 ddd 4
555 eee 5
Then i want to populate new column manager id as next emp_id from the employee table like in the below.
Emp_id Emp_name Dept_id Manager_id
111 aaa 1 222
222 bbb 2 333
333 ccc 3 444
444 ddd 4 555
555 eee 5 111
Thanks for your help in advance!
You can return the value as:
select t.*,
coalesce(lead(empid) over (order by empid),
min(empid) over ()
) as manager_id
from t;
Perhaps a select query is sufficient. Actually modifying the table is a bit tricky and the best syntax depends on the database.

SQL Server display left join result set as additional columns instead of rows

I am working with SQL Server 2012, I have a two tables, Prospects and ProspectContact, with this sample data:
ProspectID ProspectName ProspectAdd
-----------------------------------------------
1 ABC Company India
2 XYZ Company UAE
3 PQR Company KSA
4 JKL Company INDIA
ProspectContacts table:
ProspectID CName CAdd CDesignation Email
---------------------------------------------------------------------------
1 Mr Sagar India Manager sagar#gmail.com
1 Mr Saurabh India Manager Saurabh#gmail.com
2 Mr Swami UAE Director swami#gmail.com
2 Mr .ABC UAE Engg abc#gmail.com
3 Mr PQR KSA Manager pqr#gmail.com
4 Mr XYZ INDIA Manager xyz#gmail.com
If I apply left join on the above tables with below query
SELECT
P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM
[PROSPECTS] P
LEFT JOIN
[PROSPECTCONTACTS] PC ON P.PROSPECTID = PC.PROSPECTID
WHERE
P.USERID = #UserID
ORDER BY
CDATE DESC
I get this result set:
ProspectName Adress Name Email
---------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com
ABC COMPANY INDIA Mr. Saurabh saurabh#gmail.com
whereas my expected result set would be this:
ProspectName Adress Name1 Email1 Name2 Email2
-----------------------------------------------------------------------------
ABC COMPANY INDIA Mr. Sagar sagar#gmail.com Mr.Saurabh saurabh#gmail.com
XYZ COMPNAY USA Mr.Swami swami#gmail.com Mr. ABC abc#gmail.com
PQR COMPANY KSA Mr.Pqr pqr#gmail.com NULL NULL
JKL COMPNAY INDIA Mr.XYZ xyz#gmail.com NULL NULL
Try something like this:
SELECT P.[PROSPECTNAME], P.[ADDRESS], PC.CONTACTPERSON, PC.EMAILID
FROM [PROSPECTCONTACTS] PC
LEFT JOIN [PROSPECTS] P
ON P.PROSPECTID=PC.PROSPECTID
WHERE P.USERID=#UserID ORDER BY CDATE Desc

Selecting Max Count on another Column

I've been stuck on trying to come up with a query to GROUP BY CODE, so that CCC for example should only appear once, and also selecting the Name with the highest count. Can someone point me in the right direction. Thanks
So I want my query to return:
AAA Lee, Albert
BBB Robert, Steven
CCC Jones, Albert
DDD Lim, Kevin
EEE Zhang, Wil
OR
AAA Lee, Albert 12
BBB Robert, Steven 4
CCC Jones, Albert 3
DDD Lim, Kevin 21
EEE Zhang, Wil 11
From Using Sample Data:
CODE NAME Count
AAA Lee, Albert 12
BBB Robert, Steven 4
CCC Robert, Steven 2
CCC Jones, Albert 3
DDD Lim, Kevin 21
EEE Zhang, Wil 11
EEE Wil Zhang 5
The standard SQL method uses the ANSI standard row_number() function:
select s.*
from (select s.*,
row_number() over (partition by code order by count desc) as seqnum
from sample s
) s
where seqnum = 1;
You could LEFT JOIN the table to itself and filter the results
SELECT t1.* FROM Data AS t1
LEFT JOIN Data AS t2
ON
(t1.CODE = t2.CODE) AND (t1.Count < t2.Count)
WHERE t2.Count is null
http://sqlfiddle.com/#!6/cc2ea/4

How to do Conditional Column Joins in SQL?

I have a tables below
Main Table: tblMain
Phase Country City
AAA India Bangalore
AAA USA Chicago
ZZZ USA
ZZZ UK
SubTable: tblSub
Phase Country City Value
AAA USA Chicago 3
AAA USA NY 6
AAA UK London 5
AAA India Bangalore 6
AAA India Delhi 9
ZZZ USA Chicago 7
ZZZ UK London 8
Expected Result
Phase Country City Value
AAA India Bangalore 6
AAA USA Chicago 3
ZZZ USA 7
ZZZ UK 8
I want to join this with my Main table which has Phase, Country and City, however the condition is
For Phase "ZZZ" i want to join only by Country where as for Phase "AAA" i want to join ty Country & City. Is is possible to do in SQL Query without a Stored procedure or temp tables
I am looking to achieve this in plain query. Thanks in advance!!!
SELECT *
FROM This_Table TT
LEFT JOIN Main_Table MT ON TT.Countrry = MT.Country
AND TT.Phase = 'ZZZ'
LEFT JOIN Main_Table MT2 ON TT.Countrry = MT2.Country
AND TT.City = MT2.City
AND TT.Phase = 'AAA'
This should do it:
WHERE
(a.phase = 'ZZZ' AND a.country = b.country)
OR
(a.phase = 'AAA' AND a.country = b.country AND a.city = b.city)

Joining these tables

I have 3 tables as below :
Table Name :
------------
UserList
Column Name
-------------
DealerID DealerUserID
AAA 111
AAA 222
AAA 333
BBB 111
BBB 444
CCC 111
CCC 555
--
Table Name :
------------
UserInfo
Coulmns
--------
DealerUserID Name
111 John
222 James
333 Dany
444 Daniel
555 Romie
--
Table Name :
------------
CarPermitted
Coulmns
--------
DealerID DealerUserID
AAA 111
AAA 222
BBB 111
CCC 111
I want a result as below of a query which will take input as :
For DealerID = AAA
Name DealerUserID AllowedStatus
John 111 true
James 222 true
Dany 333 false
I have tried many joins as below, but could not get my desired result. Any suggestion how can I get it.
AllowedStatus is the value I need to fetch as :
If the combination of DealerID and DealerUserID from table UserList is present in CarPermitted///rest will be false..
Note : It will display all the dealeruserId belongs to one dealer
Here you go, you don't show your queries so I can say how you were going wrong
From the comments, Allowed status comes from if the record exists in the permitted table.
SELECT UF.Name, UF.DealerUserID,
CASE P.DealerID IS NULL THEN 'false' ELSE 'true' END AS AllowedStatus
FROM UserList UL
JOIN UserInfo UF ON UL.DealerUserID = UF.DealerUserID
LEFT JOIN CarPermitted P ON UL.DealerUserID = P.DealerUserID AND UL.DealerID = P.DealerID
WHERE UL.DealerID = 'AAA'