My query:
SELECT skill_code FROM TableSkills
I have tables TableSkills and TableUsers. How can I achieve skill_code data that only shows the skill_code if the user from TableUsers has at least "1"?
Expected result :
skill_code
Skill_1
Skill_2
Skill_3
Skill_6
TableSkills :
ID
skill_code
1
Skill 1
2
Skill 2
3
Skill 3
4
Skill 4
5
Skill 5
6
Skill 6
TableUsers :
ID
User
Skill_1
Skill_2
Skill_3
Skill_4
Skill_5
Skill_6
1
Mark
1
1
0
0
0
0
2
John
0
0
1
0
0
0
3
Doe
0
1
1
0
0
0
4
Jason
1
1
0
0
0
0
5
Kevin
1
1
0
0
0
0
6
Mike
0
1
1
0
0
1
Join the tables with a CASE expression in the ON clause:
SELECT DISTINCT s.skill_code
FROM TableSkills s INNER JOIN TableUsers u
ON 1 = CASE s.ID
WHEN 1 THEN u.Skill_1
WHEN 2 THEN u.Skill_2
WHEN 3 THEN u.Skill_3
WHEN 4 THEN u.Skill_4
WHEN 5 THEN u.Skill_5
WHEN 6 THEN u.Skill_6
END;
Or, with EXISTS, which may perform better:
SELECT s.skill_code
FROM TableSkills s
WHERE EXISTS (
SELECT *
FROM TableUsers u
WHERE 1 = CASE s.ID
WHEN 1 THEN u.Skill_1
WHEN 2 THEN u.Skill_2
WHEN 3 THEN u.Skill_3
WHEN 4 THEN u.Skill_4
WHEN 5 THEN u.Skill_5
WHEN 6 THEN u.Skill_6
END
);
See the demo.
Related
I have table like this:
receipt
position
unit
booking time
1
1
1
08:00:00
1
2
1
08:00:05
1
3
1
08:00:11
1
4
1
08:00:18
1
5
1
08:00:21
1
6
5
08:00:25
1
1
1
08:00:30
1
2
1
08:00:33
1
3
1
08:00:37
1
4
1
08:00:40
1
5
1
08:00:49
2
1
1
08:01:55
2
2
1
08:01:58
2
3
1
08:02:04
3
1
1
08:02:20
3
2
5
08:02:24
3
1
1
08:02:30
3
2
1
08:02:35
I want to check for every receipt whether unit 5 exists or not. If unit 5 exists, I only want to select positions with a booking time after the entry with unit 5.
For the example above my result therefore should look like this:
receipt
position
unit
bookingtime
1
1
1
08:00:30
1
2
1
08:00:33
1
3
1
08:00:37
1
4
1
08:00:40
1
5
1
08:00:49
2
1
1
08:01:55
2
2
1
08:01:58
2
3
1
08:02:04
3
1
1
08:02:30
3
2
1
08:02:35
I have kind of a start, which delivers the right result if there was only one receipt:
Select * from test
where bookingtime> (case
when (select Max(bookingtime) from test where unit=5) is null
then (Select convert(time,'00:00:00'))
Else (select Max(bookingtime) from testdb where unit=5)
End)
What am I missing to let this code run through every single receipt separately so that I get the result I am looking for?
You can use a window function to get the time for unit 5:
select t.*
from (select t.*,
min(case when unit = 5 then bookingtime end) over (partition by receipt) as bookingtime_5
from t
) t
where bookingtime_5 is null or
bookingtime > bookingtime_5;
I have a table with Scenario,Product,AlgoKey,User Select,System Select columns, I have to select the algo key for each scenario, The first priority goes to user selected otherwise system selected.
I have Shared my Inout & output result below, could you please help me how to write query for this.
Scenario
Product
AlgoKey
User Select
SystemSelect
1
P101
1
0
1
1
P102
2
1
0
2
P101
1
0
1
2
P102
2
0
0
3
P101
1
1
1
3
P102
2
0
0
4
P101
1
0
0
4
P102
2
0
1
OutPut :
Scenario
AlgoKey
Columnselected
1
2
User
2
1
System
3
1
User
4
2
System
here is how you can do it
select scenario , AlgoKey, case when Userselect = 1 then 'User' else 'System' end Columnselected
from (
select *, ROW_NUMBER() over (partition by scenario,productkey order by userselect desc, systemselect desc) rn
from tableName
) t
where rn = 1
I need to create a new table base on two tables. I have a database in ms access and need to migrate.
tableT tableS
ID CustID DATE Exp1 Exp2 ID CustID DATE Tem1 Tem2
-------------------------------- ---------------------------------
1 1 1/1/00 5 5 1 1 1/1/00 3 4
2 2 1/1/00 1 3 2 2 1/1/00 5 0
3 1 3/1/00 3 2 3 1 5/1/00 0 3
4 3 4/1/00 4 1 4 3 6/1/00 0 0
Desired output table tableNew:
ID CustID DATE Exp1 Exp2 Tem1 Tem2
---------------------------------------------
1 1 1/1/00 5 5 3 4
2 2 1/1/00 1 3 5 0
3 1 3/1/00 3 2
4 3 4/1/00 4 1
5 1 5/1/00 0 3
6 3 6/1/00 0 0
If I use outer join, I will not get the output I need.
Any idea or help.
You want a full join. You can emulate this in MS Access using:
select t.CustID, t.DATE, t.Exp1, t.Exp2, s.tem1, s.tem2
from TableT as t left outer join
tableS as s
on t.CustId = s.CustId and t.date = s.date
union all
select s.CustID, s.DATE, null, null, s.tem1, s.tem2
from tableS as s left outer join
tableT as t
on t.CustId = s.CustId and t.date = s.date
where t.CustId is null;
I have a DB with 3 tables:
Alarm
ID Message
-------------------
1 Server01 Down
2 Switch01 Port 2 down
3 Webserver Down
ListAlarmStates
ID StateName
------------------
1 Raised
2 RaisedNotified
3 Cleared
4 ClearedNotified
5 ForceClear
AlarmStates
ID AlarmId ListAlarmStatesId
-----------------------------------------
1 1 1
2 1 2
3 1 3
4 1 4
5 2 1
6 2 5
7 3 1
Now I would like to know all alarms that don't have the status ClearedNotified but do have the status cleared (the status cleared I could catch in code)
Thanks in advance!
SELECT AlarmId FROM
AlarmStates AS
INNER JOIN Alarm A
ON (AS.AlarmID = A.ID)
INNER JOIN ListAlarmStates LA
ON ( AS.ListAlarmStatesId = LA.ID)
GROUP BY AS.AlarmID
HAVING COUNT(CASE WHEN LA.StateName = 'ClearedNotified' THEN 1 ELSE NULL END) = 0
AND COUNT(CASE WHEN LA.StateName = 'Cleared' THEN 1 ELSE NULL END) > 0)
I've got two tables.
I'm trying to calculating the SUM quantity of tbl1
tbl1.xid is the primary, while tbl2.xid is the foreign
tbl1
xid pub quantity
1 1 10
2 1 2
3 0 1
4 1 5
tbl2
id ttype fno xid qnty
1 A 0 1 0
2 A 1 1 3
3 B 1 1 4
4 A 1 2 1
5 A 1 3 2
6 A 1 4 3
7 A 1 4 1
8 A 0 1 0
We are calculating the sum of tbl1's quantity
1) Whos tbl1.pub is 1
Thus tbl1.xid 3 is removed form the list, for it's pub is 0
Results
tbl1
xid pub quantity
1 1 10
2 1 2
4 1 5
2) AND Who's tbl1 has at least one tbl2.xid who's tbl2.ttype is 'A' and who's tbl2.fno is '0'
Thus tbl1.xid 2 & 4 are removed form the list, because none of them have at least one tbl2.xid who's fno is '0' and who's tbl2.ttype is 'A'
Results
parent_tbl1
xid pub quantity
1 1 10
The final results should be 10
SELECT SUM(quantity) AS Total
FROM tbl1
WHERE pub=1
AND EXISTS
(SELECT *
FROM tbl2
WHERE tbl2.ttype = 'A'
AND tbl2.fno = 0
AND tbl1.xid = tbl2.xid
)