How to add extra value to select query result - sql

SubjectMaster
Id Subject
1 English
2 History
3 Maths
UserSubjectAssociation
Id Userid SubjectId
1 1 1
2 1 3
3 2 2
Logs
Id Userid SubjectId Examdate Percentage
1 1 1 02/20/2020 50
2 1 0 Null Null
3 2 1 02/20/2020 70
4 2 2 02/20/2020 60
5 3 0 Null Null
6 4 3 02/18/2020 56
These are my sample tables.
I want to show records from log table of zero as well as all assigned subject of user 1
Suppose user 1 has 2 subject 1 and 3.
Show records from logs where subjectid comes in 0 as well as 1,3
Required Output :
Logs
Id Userid SubjectId Examdate Percentage
1 1 1 02/20/2020 50
2 1 0 Null Null
3 2 1 02/20/2020 70
4 3 0 Null Null
5 4 3 02/18/2020 56
Query :
select * from logs where rdatetime >= '' and subjectid in (select id from subjectmaster where userid = 1)
'Or' did not work.It was giving wrong output.How to handle it.

If I understand correctly, you want a correlated subquery and condition like this:
select l.*
from logs l
where l.subjectid = 0 or
exists (select 1
from subjectmaster sm
where sm.userid = l.userid and
sm.subjectid = l.subjectid
);

You can do left join :
select l.*
from logs l left join
subjectmaster sm
on sm.userid = l.userid and
sm.subjectid = l.subjectid
where not (l.subjectid <> 0 and sm.subjectid is null);

This query will give you the desired result:
select *
from logs l
where subjectid = 0
OR
subjectid IN (select subjectid
from UserSubjectAssociation
where Userid = 1)

Related

How to write sql query to get this result

I had two tables as below:
request_id
update_from_id
sw_ref_number
raised_by_user_id
raised_date
1
0
1
3
2019-08-29 15:08:16.000
id
request_id
input_id
value
is_deleted
21
1
1
00001
0
22
1
2
3
0
75
2
1
00002
0
76
2
2
0
My query is:
select req.request_id,
req.sw_ref_number,
reqDet.[value] ,
reqDet.input_id
FROM SOF.tblSOFRequest req
left join SOF.tblSOFRequestDetails reqDet
on req.request_id = reqDet.request_id
where reqDet.input_id = 1
or reqDet.input_id = 2
And after that my result is:
request_id
sw_ref_number
value
input_id
1
1
00001
1
1
1
3
2
2
2
00002
1
2
2
2
I want a result as:
request_id
sw_ref_number
epi_db_no
manuf_no
1
1
00001
3
2
2
00002
Here input_id = 1 means epi_db_no and input_id = 2 means manuf_no.
How can I get this?
Thanks
You can do it using self-join like the following query.
SELECT req.request_id
,req.sw_ref_number
,reqDet.[value] AS epi_db_no
,reqDet2.[value] AS manuf_no
FROM SOF.tblSOFRequest req
LEFT JOIN SOF.tblSOFRequestDetails reqDet ON req.request_id = reqDet.request_id
LEFT JOIN SOF.tblSOFRequestDetails reqDet2 ON reqDet.request_id = reqDet2.request_id
AND reqDet2.input_id = 2
WHERE reqDet.input_id = 1
This seems like a simple pivot. I use conditional aggregation here:
SELECT req.request_id,
req.sw_ref_number,
MAX(CASE reqDet.input_id WHEN 1 THEN reqDet.[value] END) AS epi_db_no,
MAX(CASE reqDet.input_id WHEN 2 THEN reqDet.[value] END) AS manuf_no
FROM SOF.tblSOFRequest req
JOIN SOF.tblSOFRequestDetails reqDet ON req.request_id = reqDet.request_id
WHERE reqDet.input_id IN (1,2)
GROUP BY req.request_id,
req.sw_ref_number;

SQL Search for missing record, then insert value

Below is a very oversimplified problem I am trying to solve
I have the following tables:
**quiz**
id title
--------------
1 first
2 second
3 third
4 fourth
5 fifth
**quiz_status**
id status user_id quiz_id
-------------------------------
1 0 1 1
2 0 1 2
3 0 1 3
if a I run the following:
select *
from quiz as q
left join quiz_status as qs
ON q.id = qs.quiz_id
where qs.user_id=1
I'd get:
id title id status user_id quiz_id
-------------------------------------------
1 first 1 0 1 1
2 second 2 0 1 2
3 third 3 0 1 3
4 fourth null null null null
5 fifth null null null null
I would like to be able to insert values where missing/null in the quiz_status table.
so the final outcome would be:
id title id status user_id quiz_id
-------------------------------------------
1 first 1 0 1 1
2 second 2 0 1 2
3 third 3 0 1 3
4 fourth 4 0 1 4
5 fifth 5 0 1 5
What would be the insert statement for that?
Consider the insert ... select syntax:
insert into quiz_status(status, user_id, quiz_id)
select 0, u.user_id, q.id
from (select distinct user_id from quiz_status) u
cross join quiz q
left join quiz_status qz on q.id = qz.quiz_id and u.user_id = qz.user_id
where qz.quiz_id is null
This works by generating all combinations of users and quizs, and then left joining the status table to filter on missing records. In the real life, you would likely have a users table that you can use in place of the select distinct subquery.
If you need just one user it's simpler:
insert into quiz_status(status, user_id, quiz_id)
select 0, 1, q.id
from quiz q
left join quiz_status qz on q.id = qz.quiz_id and qz.user_id = 1
where qz.quiz_id is null
Note: presumably, id is a serial column so I left it apart in the inserts.

MS Access merge two tables

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;

Best way to by column and aggregation on another column

I want to create a rank column using existing rank and binary columns. Suppose for example a table with ID, RISK, CONTACT, DATE. The existing rank is RISK, say 1,2,3,NULL, with 3 being the highest. The binary-valued is CONTACT with 0,1 or FAILURE/SUCESS. I want to create a new RANK that will order by RISK once a certain number of successful contacts has been exceeded.
For example, suppose the constraint is a minimum of 2 successful contacts. Then the rank should be created as follows in the two instances below:
Instance 1. Three ID, all have a min of two successful contacts. In that case the rank mirrors the risk:
ID risk contact date rank
1 3 S 1 3
1 3 S 2 3
1 3 F 3 3
1 3 F 4 3
2 2 S 1 2
2 2 S 2 2
2 2 F 3 2
2 2 F 4 2
3 1 S 1 1
3 1 S 2 1
3 1 S 3 1
Instance 2. Suppose ID=1 has only one successful contact. In that case it is relegated to the lowest rank, rank=1, while ID=2 gets the highest value, rank=3, and ID=3 maps to rank=2 because it satisfies the constraint but has a lower risk value than ID=2:
ID risk contact date rank
1 3 S 1 1
1 3 F 2 1
1 3 F 3 1
1 3 F 4 1
2 2 S 1 3
2 2 S 2 3
2 2 F 3 3
2 2 F 4 3
3 1 S 1 2
3 1 S 2 2
3 1 S 3 2
This is SQL, specifically Hive. Thanks in advance.
Edit - I think Gordon Linoff's code does it correctly. In the end, I used three interim tables. The code looks like that:
First,
--numerize risk, contact
select A.* ,
case when A.risk = 'H' then 3
when A.risk = 'M' then 2
when A.risk = 'L' then 1
when A.risk is NULL then NULL
when A.risk = 'NULL' then NULL
else -999 end as RISK_RANK,
case when A.contact = 'Successful' then 1
else NULL end as success
Second,
-- sum_successes_by_risk
select A.* ,
B.sum_successes_by_risk
from T as A
inner join
(select A.person, A.program, A.risk, sum(a.success) as sum_successes_by_risk
from T as A
group by A.person, A.program, A.risk
) as B
on A.program = B.program
and A.person = B.person
and A.risk = B.risk
Third,
--Create table that contains only max risk category
select A.* ,
B.max_risk_rank
from T as A
inner join
(select A.person, max(A.risk_rank) as max_risk_rank
from T as A
group by A.person
) as B
on A.person = B.person
and A.risk_rank = B.max_risk_rank
This is hard to follow, but I think you just want window functions:
select t.*,
(case when sum(case when contact = 'S' then 1 else 0 end) over (partition by id) >= 2
then risk
else 1
end) as new_risk
from t;

Select Command return Duplicate Rows

I have 3 tables:
Table 1 ExamTB:
ID ExamTerm ExamDate
1 MidTerm 2017-09-24
2 FinalTerm 2017-12-01
Table 2 ExamSubMarksTB
ID ExamID ClassID SubjectID TotalMarks PassMarks
1 1 1 1 100 50
2 1 1 2 100 50
3 1 1 3 100 50
4 2 1 1 100 50
5 2 1 2 100 50
6 2 1 3 100 50
Table 3 ExamResultTB
ID ExamID ClassID SubjectID MarksObtain StdID
1 1 1 1 80 1
2 1 1 2 70 1
3 1 1 3 60 1
4 2 1 1 50 1
5 2 1 2 72 1
6 2 1 3 68 1
Now when I create a Stored Procedure the SELECT this Select Command returns duplicate rows
SELECT ExamResultTB.ExamID
, ExamTB.ExamTerm
, ExamTB.ExamDate
, ExamResultTB.StdID
, StudentTB.Name
, StudentTB.FatherName
, ClassTB.ClassName
, SubjectTB.Subject
, ExamResultTB.ObtainMarks
, ExamSubMarksTB.TotalMarks
, ExamSubMarksTB.PassMarks
FROM ExamResultTB
INNER JOIN ExamTB ON ExamResultTB.ExamID = ExamTB.ID
INNER JOIN ClassTB ON ExamResultTB.ClassID = ClassTB.ID
INNER JOIN SubjectTB ON ExamResultTB.SubjectID = SubjectTB.ID
INNER JOIN StudentTB ON ExamResultTB.StdID = StudentTB.ID
INNER JOIN ExamSubMarksTB ON ExamResultTB.ExamID = ExamSubMarksTB.ExamID
WHERE ExamResultTB.ExamID = 4
AND ExamResultTB.StdID=1
For sure this line make a duplicate row:
INNER JOIN ExamSubMarksTB ON ExamResultTB.ExamID = ExamSubMarksTB.ExamID
You should do it in this way:
INNER JOIN ExamSubMarksTB ON ExamResultTB.ExamID = ExamSubMarksTB.ExamID and
ExamResultTB.ClassId = ExamSubMarksTB.ClassId and ExamResultTB.SubjectID =
ExamSubMarksTB.SubjectID