SQL Query - Returning 0 when select count(*) - php

First, I already read all post with same title post. But no solution solve my problem.
I want my query result's like this
kategori | hitung
Good | 2
Nice | 0
great | 1
with this query:
SELECT kategori, COALESCE(COUNT(kategori), 0) AS hitung FROM tb_cust WHERE id_sales=9 GROUP BY kategori DESC
But the result only return 2 data:
kategori | hitung
Good | 2
great | 1
Did my query is wrong?
Edit:
this is my table and the data inside of it
table name: tb_cust
id_cust | name_cust | kategori | id_sales
1 | Name 1 | Good | 9
2 | Name 2 | Good | 9
3 | Name 3 | Great | 9

The values of kategori that are available in tb_cust according to your condition(WHERE id_sales=9) is 'Good', 'Great'.
What you could have instead is
Step 1.First have a block in cte -> kategori_val to show all the possible values of kategori
Step 2. Left join with the data in tb_cust and get the count of records
--I guess "with" isnt working in mysql database
select a.kategori,count(b.kategori) as hitung
from (select distinct kategori /*I am assuming we have Good,Great and Nice available in tb_cust*/
from tb_cust) a
left join tb_cust b
on a.kategori=b.kategori
and b.id_sales=9
group by a.kategori
order by 2 DESC

You can probably do this with conditional aggregation:
SELECT kategori, SUM(id_sales = 9) as hitung
FROM tb_cust
GROUP BY kategori DESC;
This assumes that the categories have some record in the table.
If not, you can use the lookup table for the categories:
SELECT k.kategori, SUM(c.id_sales = 9) as hitung
FROM tb_kategori k LEFT JOIN
tb_cust c
ON k.kategori = c.kategori
GROUP BY k.kategori DESC;

Related

Merging a SELECT query and a SELECT COUNT query

I have a table (associations) in MySql with unique and duplicate keys:
id | key
1 | a
2 | b
3 | b
4 | c
5 | d
6 | d
And I have a table (products) with products:
id | product | type
1 | product1 | one
2 | product2 | one
3 | product3 | two
4 | product4 | two
5 | product5 | two
6 | product6 | two
Now I only what the products that are unique in associations and type 'two' so I get those ID's by this query:
SELECT assoc.id, count(*)
FROM __associations assoc
GROUP BY assoc.key
HAVING COUNT(*) <= 1
Which returns ID 1 and 4 and
SELECT prod.id, prod.product, prod.type
FROM __products prod
WHERE prod.type = two
which return 3,4,5 and 6
But my attempts to combine these two queries and get ID 4 all fails :(
This Query should give you the correct result:
SELECT prod.id, prod.product, prod.type
FROM __products prod
WHERE prod.type = two and prod.id in (SELECT assoc.id
FROM __associations assoc
GROUP BY assoc.key
HAVING COUNT(*) <= 1)
Try this:
SELECT assoc.id, count(*)
FROM __associations assoc
JOIN __products prod ON assoc.id = prod.id
GROUP BY assoc.key
HAVING COUNT(*) <= 1 AND
COUNT(CASE WHEN prod.type = 'two' THEN 1 END) > 0
The query uses a conditional aggregate in its HAVING clause, in order to filter out assoc.key groups not being related to at least one products record with type = 'two'.
You need to select from multiple tables.
SELECT table1.column1, table2.column2 FROM table1, table2 WHERE table1.column1 = table2.column1;
SQL basics: Query multiple tables
SELECT assoc.id, count(*), prod.product, prod.type
FROM __associations assoc, __products prod´
WHERE prod.id = assoc.id AND prod.type = two
GROUP BY assoc.key
HAVING COUNT(*) <= 1

Need help for SQL QUERY. Merging items using outer joins or union

[TABLE 1]
+---------+---------+------------------+
| post_id | user_id | description |
+---------+---------+------------------+
| 1 | 1 | Sample post 1 |
| 2 | 1 | Sample post 2 |
| 3 | 2 | Sample post 3 |
| 4 | 2 | Sample post 4 |
| 5 | 3 | Sample post 5 |
+---------+---------+------------------+
[TABLE 2]
+---------+---------+---------+
| id | user_id | post_id |
+---------+---------+---------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
+---------+---------+---------+
When the USER_ID 1 in TABLE 1 already existed in TABLE 2 with its respected POST_ID, it should not be part of return select query. Instead, it would return POST_ID 2,3,4,5 in TABLE 1.
When the USER_ID 2 in TABLE 1 already existed in TABLE 2 with its respected POST_ID, the expected return select query would be POST_ID 1,3,4,5 in TABLE 1 as well as the other id's.
Thanks in advance guys! :)
Looks like an anti join pattern could achieve the specified result.
SELECT t.post_id
, t.user_id
, t.description
FROM `[Table 1]` t
LEFT
JOIN `[Table 2]` u
ON u.post_id = t.post_id
AND u.user_id = ?
WHERE u.post_id IS NULL
ORDER BY t.post_id
The ? is the placeholder character for specifying a userid.
This essentially says return all rows from Table 1 along with matching rows from Table 2, but (here's the trick) the WHERE clause says to exclude all rows that found a matching row in Table 2. Leaving only rows from Table 1 that don't have a match in Table 2.
The anti join takes a little bit of effort to get your brain wrapped around, but once you get it, it's an invaluable tool to keep handy in the SQL toolbelt.
There are other query patterns that will return an equivalent result, such as a NOT EXISTS (correlated subquery) or the more common NOT IN (subquery). (With the NOT IN, be careful that the subquery doesn't return any NULL values.)
EDIT
Removed the condition u.user_id = t.user_id from the join predicate. Looks like the user_id in [Table 1] is the "author" of the post. And has no relation to whether a user has "viewed" a post or not.
Base on your sample data, because USER_ID 1 in TABLE 1 already existed in TABLE 2 with its respected POST_ID, it should return POST_ID 2,3,4,5.
Query:
SELECT t1.* FROM table2 t2
RIGHT OUTER JOIN table1 t1
ON t2.user_id = t1.user_id AND t2.post_id = t1.post_id
WHERE t2.user_id IS NULL
Final Result:
post_id user_id description
2 1 Sample post 2
3 2 Sample post 3
4 2 Sample post 4
5 3 Sample post 5
Is this what you are looking for?

MySQL 'Group By' messes up virtual column count

I am trying to select distinct rows within my SQL table, however I'm not having luck in labeling the returned rows appropriately using the code below:
SELECT #row:=#row+1 as rank,
a.id,
a.name
FROM table a,
( SELECT #row:=0) b
GROUP BY a.id
ORDER BY a.name ASC
This query will return the following:
| RANK | ID | NAME
--------------------------
2 | 4483 | Bob
8 | 9453 | Joe
10 | 4543 | Maurice
What I want it to return is this, however:
| RANK | ID | NAME
--------------------------
1 | 4483 | Bob
2 | 9453 | Joe
3 | 4543 | Maurice
Would it be more appropriate for me to use a DISTINCT query for a query of this magnitude?
As per Marc B's solution, I decided to wrap my query with another one however instead I decided to Select DISTINCT columns rather than grouping them which would remove my margin of error, by using this code
SELECT #row:=#row+1 as rank, a.id, a.name FROM
(
SELECT DISTINCT id, name
FROM Table1
) a, (SELECT #row:=0) b
ORDER BY a.name ASC

how to select sum, and count from diffrent tables using multiple joins

I need to generate some big data from many tables, regarding filters, at there also i need to get the sum of some columns, and also counts of rows like example
i have 5 records
ID | NAME | DELETED
1 | A | 1
2 | A | 0
3 | A | 1
4 | B | 1
5 | C | 1
I have the query,
SELECT p.name, sum(p.deleted) as del, count(p.id) as numbers from products as p
join other AS b ON p.id=b.id
The output i need is,
The sum of deleted records
NAME | Deletion | Count
A | 2 | 3
B | 1 | 1
C | 1 | 1
Try this ::
SELECT
p.name,
sum(p.deleted) as del,
count(id) as numbers
from products as p
join other AS b ON p.id=b.id
group by p.name
You should not need to join to get your result. This should work:
SELECT name, sum(deleted), count(1)
FROM products
GROUP BY name
SELECT name,
SUM(CASE WHEN deleted = 1 THEN 1 ELSE 0 END) Deletion,
COUNT(*) `COunt`
FROM products
GROUP BY name
OR
SELECT name,
SUM(deleted) Deletion,
COUNT(*) `COunt`
FROM products
GROUP BY name;
SQLFiddle Demo (both queries)

SQL query to select only the maximum items?

I have this table: I want to search by UID
ID | VID | UID
1 | 1 | 5
1 | 1 | 6
1 | 2 | 6
2 | 3 | 5
2 | 3 | 6
2 | 4 | 6
I want to end up with this result:
ID | VID | UID
1 | 2 | 6
2 | 4 | 6
In other words, only select the entries where the VID is MAX of the UID but keeping in min NID could differ. Something like this I suppose:
select * from TABLE where uid = 6 and max(vid)
???
But this doesn't work?
One way is to order by the value in descending order (so the max is at the top), then just select the first result.
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
ORDER BY t.VID DESC
LIMIT 1
Or do you mean you want all rows where t.VID is the highest value? In which case you could do something like this,
SELECT t.ID,
t.VID,
t.UID
FROM table t
WHERE t.ID = 1
AND t.VID = (SELECT MAX(VID) FROM table);
EDIT: Based on the edit to your question, it looks like you just want the max VID value for each ID? If I'm understanding you correctly, then this should give you what you need.
SELECT t.ID,
max(t.VID) as VID,
t.UID
FROM table t
WHERE t.UID = 6
GROUP BY t.ID
You need to have a subquery. This should work:
select * from TABLE where ID='1' AND VID=(select max(VID) from TABLE)
I expect your real-life example is more complicated (at least has more data).
This query will give you the row you want.
SELECT id,vid, uid
FROM TABLE
where id = 1
and vid in (select max(vid) from TABLE where id = 1)

Categories