I am writting a mysql query. Help me out - sql

I have a typical situation.
my table has four column. (id, user, col2, status)
I want to write a query which gives me the results of col2. But it has a column status which has (0/1). So I want only col2 data which has 0 status + a user's all data (0/1).
id user col2 status
1 sam aa 1
2 sam bb 0
3 sam cc 1
4 max dd 0
5 max dd 1
6 max ee 1
7 jam ff 0
8 jam gg 1
My result should be like. I want sam's all result + other's only 0 status result.
id user col2 status
1 sam aa 1
2 sam bb 0
3 sam cc 1
4 max dd 0
7 jam ff 0

How about
SELECT * FROM yourTable WHERE user = "sam" OR status = 0;
?

your where condition would be:
where (user = 'sam' or status = 0)

select * from the_table where user = 'sam' or status = 0 order by id

select *
from yourtable as t
where t.user = "sam"
or t.status = 0

SELECT *
FROM YOUR_TABLE
WHERE user = 'sam' OR
status = 0
We use OR in the WHERE clause so that it will be true for all of sam's rows and for all other rows which have status as 0

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;

Select groups given a condition in a variable sql

I must do a query where I select those groups, given by de concatenation between sample and serial that could be defined as household, where at least one in the variable bplcountry = 1
sample serial bplcountry
1 1 2
1 1 1
1 3 2
2 1 2
2 2 2
2 3 2
3 1 2
3 3 2
3 3 1
I have made some research but I'm very amateur on SQL. I get some hint like this:
SELECT *
FROM latinCensus
GROUP BY sample AND serial
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
Also I got some idea in this way
SELECT *
FROM latinCensus
GROUP BY CONCAT(sample,serial)
HAVING COUNT(bplcountry NOT IN ('1') OR NULL) = 0
I would expect something like this:
sample serial bplcountry
1 1 2
1 1 1
3 3 2
3 3 1
I will appreciate your help!
You want the pairs where bplcountry is 1. You can use window functions:
select lc.*
from (select lc.*,
sum(case when bplcountry = 1 then 1 else 0 end) over (partition by sample, serial) as cnt_1
from latincensus lc
) lc
where cnt_1 > 0;
Or use exists:
select lc.*
from latincensus lc
where exists (select 1
from latincensus lc2
where lc2.sample = lc.sample and lc2.serial = lc.serial and
lc2.bplcountry = 1
);
You haven't tagged your db, but something along these lines should work (can also be expressed using joins)
select sample, serial, bplcountry
from t
where (sample,serial) in (select sample,serial
from t
where bplcountry=1);

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)

select query - eliminate rows with duplicate column value on condition

I have a select query that ends up with results like:
ID COMPLIANT
------------------
10 0
12 0
29 0
29 1
43 1
44 1
44 0
How can I get results without these duplicate ID rows, on the condition that if an ID has already been marked as COMPLIANT once (a 1 instead of a 0), the duplicate rows with COMPLIANT=0 do not appear? I'd want:
ID COMPLIANT
------------------
10 0
12 0
29 1
43 1
44 1
How about aggregation?
select id, max(complaint) as complaint
from t
group by id;
This returns one row per id. If you can have multiple complaints -- and you want all of those -- than an alternative is:
select id, complaint
from t
where complaint = 1
union all
select id, complaint
from t
where not exists (select 1 from t t2 where t2.id = t.id and t2.complaint = 1);
this will work:
select id, max(complaint)
from tablename
group by id;

Logic / query required for scenario and pivot is not helping me

I have following table structure
ID Status1 status 2 status 3
A1 1 0 1
A1 1 1 0
A2 1 0 0
A3 0 1 1
I want to collect it as one record like this (only 1's count for each column)
A1 2 1 1
A2 1 1 0
A3 0 1 1
I have tried using pivot but actually not the one am getting it correctly.
Please give some thought.
Hope you looking for this
Select ID, SUM(Status1) As Status1, SUM(Status2) As Status2, SUM(Status3) As Status3
from MyTable
Group By ID