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'
Related
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.
I have the below users and network information in a USER table. I would like to fetch all the Users for a given NetworkID.
ID Name Value Owner
1 UserID 123 111
2 NetworkID 567 111
3 FName ABC 111
4 LName BCD 111
5 UserID 234 222
6 NetworkID 567 222
7 FName DEF 222
8 LName EFG 222
9 UserID 345 333
10 NetworkID 567 333
11 FName GHI 333
12 LName HIJ 333
Below is the Self Join query, I have written to achieve the expected result set
select distinct U1.value NetworkID
, U2.value Users
from User U1
join User U2 on U2.owner = U1.owner and U2.name = 'UserID'
where U1.name = 'NetworkID' and U1.value = '567'
Expected Result
NetworkID Users
567 123
567 234
567 345
The volume of the table is very large and it is taking very long time to fetch the results using this self join. Based on the DB restrictions, I cannot make changes to the existing schema (adding Indexes). I need suggestion on how this query can be rewritten effectively to achieve same result set.
Your query is fine:
select U1.value as NetworkID, U2.value Users
from User U1 join
User U2
on U2.owner = U1.owner and U2.name = 'UserID'
where U1.name = 'NetworkID' and U1.value = '567';
For this query, you want indexes on (owner, name) and (name, value, owner).
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.
I want a query to merge each of two rows meeting the following conditions in a large table:
Surname, Name, FatherName are duplicates
1, 2 contain "---" on one of the rows and 3, 4 contain "---" on the other row
Example data:
Surname Name FatherName Phone Mobile 1 2 3 4
Smith Alex Nick 12345 00000 xxx zzz --- ---
Smith Alex Nick 12345 00000 --- --- vvv aaa
Jone Mary John 22222 11111 sss eee --- ---
Pan Peter Peter 33333 22222 ttt uuu --- ---
Bob Nick Nick 44444 77777 --- --- ppp qqq
Mary Maria John 99999 00000 jjj kkk --- ---
Mary Maria John 99999 00000 --- --- iii ---
Expected output:
Surname Name FatherName Phone Mobile 1 2 3 4
Smith Alex Nick 12345 00000 xxx zzz vvv aaa
Jone Mary John 22222 11111 sss eee --- ---
Pan Peter Peter 33333 22222 ttt uuu --- ---
Bob Nick Nick 44444 77777 --- --- ppp qqq
Mary Maria John 99999 00000 jjj kkk iii ---
Try this simple query:
Select
Surname,
Name,
FatherName,
Phone,
Mobile,
Max(T.[1]) As [1],
Max(T.[2]) As [2],
Max(T.[3]) As [3],
Max(T.[4]) As [4]
From
YourTable As T
Group By
Surname,
Name,
FatherName,
Phone,
Mobile
This will work for your sample data.
First with a self join I get the merged rows and then with UNION ALL for the unique rows:
SELECT t1.Surname, t1.Name, t1.FatherName, t1.Phone, t1.Mobile,
t1.[1], t1.[2], t2.[3], t2.[4]
FROM tablename t1 INNER JOIN tablename t2
ON t1.Surname = t2.Surname AND t1.Name = t2.Name AND t1.FatherName = t2.FatherName
AND (
(t1.[1] <> '---' OR t1.[2] <> '---')
AND
(t1.[3] = '---' AND t1.[4] = '---')
AND
(t2.[3] <> '---' OR t2.[4] <> '---')
AND
(t2.[1] = '---' AND t2.[2] = '---')
)
UNION ALL
SELECT * FROM tablename AS t1
WHERE NOT EXISTS (
SELECT 1 FROM tablename AS t2
WHERE t1.Surname = t2.Surname AND t1.Name = t2.Name AND t1.FatherName = t2.FatherName AND t1.[1] <> t2.[1]
)
I am trying to write oracle sql to select all emplids from table ABC
excluding the emplids with three specific roles. example is as follows -
TABLE1= ABC
EMPLID ROLE
______________________
111
Apple
111
Mango
111
Red_Apple
222
Apple
222
Orange
222
Red_Mango
222
Banana
333
Apple
333
Orange
444
Apple
444
Mango
444
Red_Mango
555
Grapes
666
Orange
666
Grapes
666
Blueberry
TABLE2 = DETAILS
EMPLID NAME EMAIL
__________________________________
111 John
info#email.com
222 Erica
info#email.com
and so on....
Basically, in above example since Apple, Mango, and Red% are the three roles
that needs to be excluded. The sql should return EMPLID and NAME for
222,333,555,and 666. It should exclude 111 and 444
I tried creating sub selects but still not working.`enter code here`. Any advice or help is
highly appreciated.
Use conditional aggregation:
SELECT t1.EMPLID,
t1.NAME,
t1.EMAIL
FROM DETAILS t1
INNER JOIN
(
SELECT EMPLID
FROM ABC
GROUP BY EMPLID
HAVING SUM(CASE WHEN ROLE = 'Apple' OR ROLE = 'Mango' OR ROLE LIKE 'Red%'
THEN 1 ELSE 0 END) < 3
) t2
ON t1.EMPLID = t2.EMPLID