Laravel count and return 0 - sql

Table A:
id name
1 Apple
2 Orange
Table B:
id table_a_id
1 1
2 1
3 1
How can i return like? :
Name count
Apple 3
Orange 0
I only got Apple =3 from my join sql, how can i count the orange with 0 result?
Here is my sql:
A::select('A.name', DB::raw('COUNT(B.table_a_id) AS count'))
->leftJoin('B', 'a.id', '=', 'B.table_a_id')
->groupBy('A.name','B.table_a_id')
->get();

$data = DB::table('A')
->join('B','A.id','=','B.table_a_id')
->select('name',DB::raw('COUNT(table_a_id)')->groupBy('A.id')->get();

Related

SQL Query to get multiple resultant on single column

I have a table that looks something like this:
id name status
2 a 1
2 a 2
2 a 3
2 a 2
2 a 1
3 b 2
3 b 1
3 b 2
3 b 1
and the resultant i want is:
id name total count count(status3) count(status2) count(status1)
2 a 5 1 2 2
3 b 4 0 2 2
please help me get this result somehow, i can just get id, name or one of them at a time, don't know how to put a clause to get this table at once.
Here's a simple solution using group by and case when.
select id
,count(*) as 'total count'
,count(case status when 3 then 1 end) as 'count(status1)'
,count(case status when 2 then 1 end) as 'count(status3)'
,count(case status when 1 then 1 end) as 'count(status2)'
from t
group by id
id
total count
count(status3)
count(status2)
count(status1)
2
5
1
2
2
3
4
0
2
2
Fiddle
Here's a way to solve it using pivot.
select *
from (select status,id, count(*) over (partition by id) as "total count" from t) tmp
pivot (count(status) for status in ([1],[2],[3])) pvt
d
total count
1
2
3
3
4
2
2
0
2
5
2
2
1
Fiddle

Enquiry on Query in Oracle SQL

I have data as below:
Category | Type | Rank
Milk 1 1
Milk 2 2
Milk 3 3
Chocolate 1 2
Candy 1 1
Any idea to achieve the output of below with a flat SQL query:
Category
Milk
Query must satisfy the below conditions:
1. Only Type 1 and Rank 1 will be selected.
2. Only Category that has Type 1 and Type 2 will be selected.
In the sample data above, only Milk that satisfy the conditions mentioned above.
My query is below. But it's incorrect, because it will return Candy as well.
SELECT DISTINCT Category
FROM table
WHERE Type = 1 AND rank = 1
Thanks in advance!
You can try below -
DEMO
select distinct category
from table a
WHERE Type = 1 AND rank = 1
and exists
(select 1 from table b where a.category=b.category and type in (1,2)
group by category having count(distinct type)=2)
OUTPUT:
category
Milk
You can use aggregation:
select category
from t
group by category
having sum(case when type = 1 and rank = 1 then 1 else 0 end) > 0 and
sum(case when type = 2 then 1 else 0 end) > 0;
Assuming no duplicates, this can be simplified to:
select category
from t
where (type = 1 and rank = 1) or type = 2
group by category
having count(distinct type) = 2;

postgresql statement prob

I have two table A and B as following.
A:
key type
0 t
1 f
2 t
3 f
4 t
5 t
.......
B:
key name
0 Mary
0 Tony
0 Krolik
1 Tom
2 Tony
3 Tony
3 Mary
3 Tom
4 Tony
4 Tim
5 Tim
5 Mary
5 Wuli
.....
I hope to find top n occurence name that it's type is 'f'.
For example, in A, the type of key 1 and 3 are 'f', we find key 1 and 3 in table B, there are 2 'Tom' and 1 'Mary' and 1 'Tony'.
1 Tom
3 Tony
3 Mary
3 Tom
if n = 1 and the table is just showed as before, we hope to get 'Tom', because its occurence is top 1.
How can I write sql statement to satisfy these requirement?
I write something like below, but it is wrong. Can anyone help me? I assume n = 20.
SELECT DISTINCT TOP 20 name
FROM B
WHERE key IN (
SELECT key
FROM A
WHERE "type" = 'f'
)
GROUP BY name
ORDER BY DESC;
You don't seem to need aggregation. And the equivalent of top in Postgres is limit or fetch first <row> rows:
SELECT name, key
FROM B
WHERE B.key IN (SELECT A.key
FROM A
WHERE "type" = 'f'
)
ORDER BY key;
This corresponds to the results presented in the question. Your description doesn't quite match those results.

Building SQL with GROUP BY

I need to create a SQL that will give me the total number of fans for a category.
select count(fo.id)
from fans_fanofvote as fov, fans_fanof as fo
where fov.fanof_id = fo.id
GROUP BY fo.fanofcategory_id
But the problem is that a fan can vote more then once for a category and because of that my query returns 2 fans for MMA but it should be 1 since the FAN (id:1) has voted twice for that category.
FanOf
ID FANOFCATEGORY NAME
== ============== =====
1 1 Ronda Rousey
2 1 GSP
3 2 Carey Price
4 2 Sidney Crosby
FanOfCategory
ID NAME
== ======
1 MMA
2 Hockey
3 Football
FanOfVote
ID FAN FANOF
== ==== =====
1 1 1
2 1 2
3 2 3
4 1 4
5 1 3
To get each category and the number of distinct fans that voted for it:
select foc.name, count(distinct fov.fan)
from fans_fanofvote as fov
join fans_fanof as fo
on fov.fan = fo.id
join fanofcategory foc
on fo.fanofcategory = foc.id
GROUP BY foc.name
To get the number of distinct fans for a specific category:
declare #CategoryID int = 1 //This should be a parameter, but just putting it here so you can test
select count(distinct fan)
from fanofvote
where fanof = #CategoryID

SQL Theory - Group by all attributes of relation

Theoretical question. Say we have relation R(A,B,C).
For fun, let's say that this is the Relation Table
A B C
1 2 3
1 2 2
1 2 3
1 1 1
And we execute the following query:
SELECT *
FROM R
GROUP BY A,B,C;
What would the result be?
Assuming it is SQL Server 2008 (or similar compliance).
Input:
A B C
1 2 3
1 2 2
1 2 3
1 1 1
Query:
SELECT *
FROM R
GROUP BY A,B,C;
Output
A B C
1 1 1
1 2 2
1 2 3
SQL Fiddle: http://sqlfiddle.com/#!3/d2bcd/1/0