I have two tables:
builders
b_id fk_c_id
1 1
2 1
3 1
4 1
5 1
6 2
7 2
subbuilders
fk_b_id sb_id
1 2
1 3
1 4
2 5
6 7
and i want Distinct b_id which are not exist in subbuilders table and must have same fk_c_id
I create:
SELECT DISTINCT b.id FROM pms_builder_to_subbuilders bsb
LEFT JOIN pms_builders b ON b.id = bsb.sb_id WHERE b.fk_c_id = '1'
but it show Distinct records from subbuilders.
You can get the desired results with the following query:
SELECT DISTINCT b.b_id FROM builders b LEFT JOIN subbuilders sb ON sb.fk_b_id = b.b_id WHERE b.fk_c_id = '1' AND ISNULL(fk_b_id);
i think you want this query:
SELECT DISTINCT b_ID
FROM builders
WHERE b_ID NOT IN
(SELECT DISTINCT fk_b_id FROM subbuilders)
but it returns
b_ID
========
3
4
5
7
but i didn't understand your statement: must have same fk_c_id. What do you mean by that?
b_id = fk_c_id
if that's the case then there will be no rows returned because only record 1 has the same b_ID and fk_c_id but exists in table subbuilders
Related
table:
id lesson
11 11 A
8 11 B
4 11 A
4 11 A
2 11 A
6 11 A
5 11 A
13 11 A
11 11 B
the id 11 has both taught in classroom 11A, 11B. How to select the ids that have both values 11a,11b?
I tried this with no luck:
select id from table where lesson in '11A' and lesson in '11B'
because it gives empty table, because it can't be both 11a and b at the same time.
If it's exectly two values you can make an inner join
select a.id
from myTable a
inner join myTable b
on a.id = b.id
and a.lesson = '11 A' and b.lesson = '11 B'
for obtain the id with both the lesson
if could try using a subquery for the involved id and the count if the id have more then a result
select id
FROM (
select id
from my_table
where lesson in ('11A', '11b')
) t
group by id
having count(*) = 2
I have a table where exists 4 entries like this.
class_type
id type
1 A
2 B
3 M
4 T
and another table where these values are foreign key.
id number id_class_type
1 10 1
2 11 1
3 12 2
4 13 1
5 14 2
6 15 3
7 16 1
8 17 3
So what i want is count(*) and group by id_class_type but with all class_type (1,2,3,4) although there is not present the id 4.
if you want only the class tha match you can use inner join
select a.class_type, count(*)
from class_type a
inner join table2 b on a.id = b.id_class_type
group by a.class_type
otherwise you can use left join
select a.class_type, count(*)
from class_type a
left join table2 b on a.id = b.id_class_type
group by a.class_type
Complaint_status Table : multiple rows for a complaints
id complaint_id status_type status_value
1 11 2 0
2 11 2 2
3 11 2 1
4 11 2 2
Trying to get min(id) having status_type=2 and status_value=2 but showing null,have to use case statement for doing other stuff.
SELECT c3.id AS Ticket_id,
CASE
WHEN cs.status_value=2 THEN cs.created_at
ELSE NULL
END AS Closure_date
FROM complaint c3
INNER JOIN complaint_status cs ON cs.complaint_id=c3.id
WHERE cs.id IN
(SELECT min(id)
FROM complaint3_status
WHERE complaint_id=c3.id
AND status_type=2)
AND c3.id IN(11)
Here is one method that gives you the entire row:
select cs.*
from complaint_status cs
where status_type = 2 and status_value = 2
order by id asc
limit 1;
You can just use select id if that is all you want. I have no idea what case has to do with this query.
Table A
Status
1
1
1
2
2
3
Table B
Status
1
1
2
2
2
2
3
3
How to Get following Result Table
Result Table
Status Count
1 5
2 6
3 3
Help me to build SQL Query to get Count in Result Table.
Try this
SELECT Status, COUNT(*) FROM
(SELECT a.Status FROM TableA AS a
UNION ALL
SELECT b.Status FROM TableB AS b) UN
GROUP BY Status
SELECT STATUS, COUNT(*) FROM
(SELECT STATUS FROM TABLE1
UNION ALL
SELECT STATUS FROM TABLE2) un
GROUP BY STATUS
FIDDLE
a table
a_id a_value
1 text1
2 test2
b table
b_id b_num a_id
1 5 1
2 7 1
3 2 1
4 7 2
5 56 2
Results base a table (edited)
a_id:1 a_value:text1 total:3 records
a_id:2 a_value:text2 total:2 records
How can get this format in sql?
query a table and add a field(total) count b.a_id = a.a_id in table b
thank you..
You can try:
SELECT a.a_id AS id, a.a_value AS value, (SELECT count(b.b_id) AS count FROM b WHERE (b.a_id = a.a_id)) AS total FROM a GROUP BY a.a_id
Then the result for your example using the data from tables a and b:
**id value total**
1 text1 3
2 text2 2
I imagine you have an error in your b table, so I will assume what you call b_id is actually a_id, or your results would be wrong
Anyway you could use:
SELECT COUNT(b.a_id) AS total FROM b GROUP BY (SELECT a.a_id FROM a)
ORDER BY b.a_id
The updated query based on changes to the question
SELECT a_id, a_value, x.total
FROM a
INNER JOIN
(SELECT b.a_id, COUNT(1) AS total
FROM b
GROUP BY (b.a_id)) X
ON a.a_id = x.a_id
ORDER BY a.a_id