Sql query null value insertion - sql

Sql Query
SELECT respondant.respondant_firstname as first_name,
question.question_id as question_id,
answer.answer_id,
answer.answer_text,
answer.answer_rate,
answer.answer_nps,
question_radio.question_radio_text as opt
FROM question
LEFT JOIN answer on answer.answer_question_id = question.question_id
LEFT JOIN question_radio on answer.answer_question_radio_id = question_radio.question_radio_id
LEFT JOIN respondant on answer.answer_respondant_id = respondant.respondant_id
WHERE question.question_feedback_id = 1
ORDER BY question.question_id, answer.answer_id
Output:
first_name question_id answer_id answer_text answer_rate answer_nps opt
RM 1 1 5 NULL NULL
Y 1 3 0 NULL NULL
Ben 1 5 0 NULL NULL
akash 1 8 2.5 NULL NULL
RM 2 2 0 4 NULL
Y 2 4 0 3 NULL
Ben 2 6 0 0 NULL
akash 2 9 0 0 NULL
Ben 3 7 Thanks 0 0 NULL
akash 3 10 0 0 NULL
I Need the output as:
first_name question_id answer_id answer_text answer_rate answer_nps opt
RM 1 1 5 NULL NULL
Y 1 3 0 NULL NULL
Ben 1 5 0 NULL NULL
akash 1 8 2.5 NULL NULL
RM 2 2 0 4 NULL
Y 2 4 0 3 NULL
Ben 2 6 0 0 NULL
akash 2 9 0 0 NULL
RM 3 NULL 0 0 NULL
Y 3 NULL 0 0 NULL
Ben 3 NULL ThankS 0 0 NULL
akash 3 NULL 0 0 NULL
Where in third and fourth row from last doesnt have the id 3 but i need to replace as 3 and other values in that two rows has to be null

What you want to do is rather complicated. You want a row for each responder and question, and then to fill in the details for those that responded. My guess is that you want the answer_id filled in, where available. That is, your desired results should have answer_ids for "Ben" and "Akash".
The following query generates all the rows using a cross join between the responders and the questions. Then it brings in the additional information:
SELECT r.respondant_firstname as first_name, q.question_id as question_id,
a.answer_id, a.answer_text, a.answer_rate, a.answer_nps,
qr.question_radio_text as opt
FROM respondant r CROSS JOIN
question q LEFT JOIN
answer a
on a.answer_question_id = q.question_id and
a.answer_respondant_id = r.respondant_id LEFT JOIN
question_radio qr
on a.answer_question_radio_id = qr.question_radio_id
WHERE q.question_feedback_id = 1
ORDER BY q.question_id, a.answer_id;

Related

How to get the data with a condition for other table?

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.

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.

How to add extra value to select query result

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)

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;

SQL left join - and after on clause is not working

I have a scenario in left join of SQL which is not generating required output which i need. Following is description in tabular form and my tried queries,
Table A
A_ID // PK OF TABLE A
IS_ACTIVE // VALUE=1 OR 0
Table B
B_ID // PK OF TABLE B
A_ID // FK OF TABLE A IN TABLE B
Sample Records of Table A
A_ID IS_ACTIVE
1 1
2 0
3 1
4 0
5 0
Sample Records of Table B
B_ID A_ID
1 1
2 1
3 4
4 4
5 4
6 4
Select * from A left join B on A.A_ID=B.A_ID
A_ID IS_ACTIVE B_ID A_ID
1 1 1 1
1 1 2 1
2 0 NULL NULL
3 1 NULL NULL
4 0 3 4
4 0 4 4
4 0 5 4
4 0 6 4
5 0 NULL NULL
Select * from A left join B on A.A_ID=B.A_ID and A.IS_ACTIVE=0
Following output is the actual output of above query with no effect to records by adding AND is_active=0 after ON clause.
A_ID IS_ACTIVE B_ID A_ID
1 1 1 1
1 1 2 1
2 0 NULL NULL
3 1 NULL NULL
4 0 3 4
4 0 4 4
4 0 5 4
4 0 6 4
5 0 NULL NULL
Following output is the required output which i need to solve my problem.
A_ID IS_ACTIVE B_ID A_ID
1 1 NULL NULL
1 1 NULL NULL
2 0 NULL NULL
3 1 NULL NULL
4 0 3 4
4 0 4 4
4 0 5 4
4 0 6 4
5 0 NULL NULL
I am facing problem in getting exact records which are required.
I need all records from Table A and matching records from Table B but
those records of Table B which are equal to is_active=0 of Table A.
Note : Query should show all records of Table A
Please help me how can i get this scenario in Left Join of SQL.
I tried your examples as code. And I get the result you needed. What is the problem?
CREATE TABLE #a(a_id int, is_active bit)
CREATE TABLE #b(b_id int, a_id int)
INSERT INTO #a(a_id,is_active)
VALUES(1,1),(2,0),(3,1),(4,0),(5,0)
INSERT INTO #b(b_id,a_id)
VALUES(1,1),(2,1),(3,4),(4,4),(5,4),(6,4)
SELECT *
FROM #a as a
LEFT JOIN #b as b
ON a.a_id = b.a_id
AND a.is_active = 0
DROP TABLE #a
DROP TABLE #b
Have you tried:
Select * from A left join B on A.A_ID=B.A_ID
Where A.IS_ACTIVE=0