I have table that contain clients_status, and my query just like these:
SELECT ROUND((SUM(client_status='BAD DEBT')/COUNT(*))*100) AS BAD,
ROUND((SUM(client_status='ALERT')/COUNT(*))*100) AS ALERT,
ROUND((SUM(client_status='REMIND BAYAR')/COUNT(*))*100) AS REMIND,
ROUND((SUM(client_status='RUTIN BAYAR')/COUNT(*))*100) AS RUTIN,
ROUND((SUM(client_status='POTENSI KOREKSI')/COUNT(*))*100) AS POTENSI,
ROUND((SUM(client_status='TOP 10')/COUNT(*))*100) AS TOP FROM clients
And the output of my query just like this:
What should i do, to make my output like this:
Data value
===============
Bad 29
Alert 29
Remind 14
Rutin 14
Potensi 0
Top 15
Please help guys... thanks,
Aggregate grouping by the status and cross join a subquery getting the total count to calculate the percentage. For the renaming and the custom order you can use CASEs.
SELECT cs.s data,
cs.c / ca.c * 100 value
FROM (SELECT CASE client_status
WHEN 'BAD DEPT' THEN
1
WHEN 'ALERT' THEN
2
WHEN 'REMIND BAYAR' THEN
2
WHEN 'RUTIN BAYAR' THEN
4
WHEN 'POTENSI KOREKSI' THEN
5
WHEN 'TOP 10' THEN
6
END o,
CASE client_status
WHEN 'BAD DEPT' THEN
'Bad'
WHEN 'ALERT' THEN
'Alert'
WHEN 'REMIND BAYAR' THEN
'Remind'
WHEN 'RUTIN BAYAR' THEN
'Rutin'
WHEN 'POTENSI KOREKSI' THEN
'Potensi'
WHEN 'TOP 10' THEN
'Top'
END s,
count(*) c
FROM clients
GROUP BY client_status) cs
CROSS JOIN (SELECT count(*) c
FROM clients) ca
ORDER BY cs.o;
Related
The following SQL (PHP, MySQL) gives me rows with one id per row and the number of matches for each id, as the COUNT() for each.
SELECT ot.id, COUNT(ot.id)
FROM ot
JOIN tm on tm.mediaid = ot.id
WHERE tm.uploadedToS3 = 1
AND (
ot.something IN (1)
OR ot.somethingelse IN (1)
OR ot.anotherthing IN (1)
OR ot.morestuff IN (1)
OR ot.evenmorestuff IN (1)
)
GROUP BY ot.id
The result is something like...
ot.id COUNT(ot.id)
40153 4
40305 3
40309 35
40314 29
40315 12
40317 10
40318 16
40319 14
40324 154
40331 113
I would just like to know how many rows this query returns. I don't need any other information, just one integer. For the above, I am looking for the number of rows, i.e. I'd expect to just get the number 10.
How would I get this?
Thanks.
Use count(distinct) with no group by:
SELECT COUNT(DISTINCT ot.id)
FROM ot JOIN
tm
ON tm.mediaid = ot.id
WHERE tm.uploadedToS3 = 1 AND
(ot.something IN (1)
OR ot.somethingelseIN (1)
OR ot.anotherthing IN (1)
OR ot.morestuff IN (1)
OR ot.evenmorestuff IN (1)
)
I have qualified risks with description and creation date, who are attached to subcategory of risks this last ones are attached to category of risks, each risk has a name like 'Risk_1' , my aim is to count the number of risks by month and risk category including zero.
I have this request :
SELECT DISTINCT risk_names.type as risk_name, MONTH(risk.creation_date) as month, count(risk.id) as number FROM risk As risk , risk_category
JOIN (
SELECT risk_category.name as type
FROM
risk_category
) as risk_names on risk_names.type = risk_category.name
where risk.creation_date >= (NOW()-INTERVAL 3 MONTH) GROUP BY MONTH(risk.creation_date), risk_names.type;
Who return this result :
Risk_name month number
---------------------------------
Risk_1 1 10 ---> instead 8
Risk_2 1 10 ---> instead 1
Risk_3 1 10 ---> instead 1
Risk_1 2 12 ......
Risk_2 2 12
Risk_3 2 12
Risk_1 12 4
Risk_2 12 4
Risk_3 12 4
As you can see the number returned is the total for each month , but my aim is to get total for each distinct risk.
Can you help me . thanks
The comma in your FROM is doing a CROSS JOIN. A Cartesian product is unnecessary and throws all the counts off.
I suspect you want something like this:
SELECT rc.type as risk_name, MONTH(r.creation_date) as month,
count(r.id) as number
FROM risk_category rc LEFT JOIN
risk r
ON r.?? = rc.??
where risk.creation_date >= (NOW()-INTERVAL 3 MONTH)
GROUP BY rc.type, MONTH(r.creation_date);
I don't know what the JOIN criterion is between risk and risk_category.
Then try using distinct keuword with count() like count(distinct risk.id) as number instead
I need a little help with a MySQL query.
I have two tables one table is a list of backlinks with a is_homepage (bool) flag. The second table is a list of the domains for all of the backlinks, a was link_found (bool) flag, and a url_count column which is the number of rows in the backlinks table that are associated with each domain.
Note that the domain_id column is the foreign key to the domain table id column. Heres some sample data.
backlinks
id domain_id is_homepage page_href
1 1 1 http://ablog.wordpress.com/
2 1 0 http://ablog.wordpress.com/contact/
3 1 0 http://ablog.wordpress.com/archives/
4 2 1 http://www.somewhere.org/
5 2 0 http://www.somewhere.org/page=3
6 3 1 http://www.great-fun-site.com/
7 3 0 http://www.great-fun-site.com/index.html
8 4 0 http://red.blgspot.com/page=7
9 4 0 http://blue.blgspot.com/page=9
domains
id url_count link_found domain_name
1 3 1 wordpress.com
2 2 0 somewhere.org
3 2 1 great-fun-site.com
4 2 1 blgspot.com
The results Im looking to get from the above data would be: count = 2, total = 5.
Im trying to get the count of rows from the domains table (count) and then the sum of the url_count (total) from the domains table WHERE link_found is 1 and where one of the links in the backlink table is_homepage is 1.
Here's the query I'm trying to work with.
SELECT SUM(1) AS count, SUM(`url_count`) total
FROM `domains` AS domain
LEFT JOIN `backlinks` AS link ON link.domain_id = domain.id
WHERE domain.id IN (
SELECT DISTINCT(bl.domain_id)
FROM `backlinks` AS bl
WHERE bl.tablekey_id = 11
AND bl.is_homepage = 1
)
AND domain.link_found = 1
AND link.is_homepage = 1
GROUP BY `domain`.`id`
The problem with this query is that it returns a row for each entry in the domains table. I think I might need one more sub query to add up the returned results but I'm not sure if that's correct. Does anyone see what I'm doing wrong? Thank you!
EDIT:
The problem I'm having is that if there are more than one homepage in the back-links table then its counted multiple times. I need to only count each domain once.
Well, you shouldn't have to do a group by as you are not selecting anything other than aggregated fields. I'm no mysql expert, but this should work:
SELECT count(d.id) as count, sum(d.url_count) as total from domains as d
inner join backlinks as b
on b.domain_id = d.id
Where d.Link_found = 1 and b. is_homepage = 1
The reason you're getting a row for each entry in the domains table is that you're grouping by domain.id. If you want grand totals only, just leave off the GROUP BY piece.
I think a fairly simple query will do the trick:
SELECT COUNT(*), SUM(domains.URL_Count)
FROM domains
WHERE domains.link_found = 1 AND domains.id IN (
SELECT domain_id FROM backlinks WHERE is_homepage = 1)
There's a working SQLFiddle here.
Thanks for the help. Sorry it was so hard to explain I need a MySQL fiddle :)
If anyones interested heres what I ened up with:
SELECT SUM(1) AS count, SUM(total) AS total
FROM
(
SELECT SUM(`url_count`) total
FROM `domains` AS domain
LEFT JOIN `backlinks` AS link ON link.domain_id = domain.id
WHERE domain.id IN (
SELECT DISTINCT(bl.domain_id)
FROM `backlinks` AS bl
WHERE bl.tablekey_id = 11
AND bl.is_homepage = 1
)
AND domain.link_found = 1
AND link.is_homepage = 1
GROUP BY `domain`.`id`
) AS result
I have the following query:
SELECT a.feeder_id, b.feeder_pr
FROM authors_article_feeders a
LEFT JOIN feeders b ON b.id = a.feeder_id
WHERE website_id =1
LIMIT 0 , 30
which results in:
feeder_id feeder_pr
18 2
18 2
18 2
18 2
32 6
What I need is to modify the above query so that it will manipulate this data so that the result would end up with a count of each feeder_pr, so in this case the result would be:
feeder_pr count
2 4
6 1
Any assistance is appreciated. If you have time please describe your solution so that I can learn from it while I'm at it.
Everything I've tried has ended in inaccurate results, usually with just one row instead of the expected 2.
You just need to add a GROUP BY And, you would not even need the joins
SELECT b.feeder_pr, COUNT(b.feeder_pr)
FROM feeders b
GROUP BY b.feeder_pr
SELECT b.feeder_pr, count(a.feeder_id) as count
FROM authors_article_feeders a
LEFT JOIN feeders b ON b.id = a.feeder_id
WHERE website_id =1
GROUP BY 1
I have tried this multiple distinct from MySQL and I cant seem to get anything to work... I have a table that is a history table. The appartments can be found many times from the same building with different status. I need to find the newest one for each appartment (the one with the highest id ORDER BY id)
id building appartment_id status
208 1 2 2
209 1 3 2
210 1 4 2
211 1 5 2
212 1 6 2
213 1 7 2
214 1 2 1
215 1 2 3
But how do I do that?! :S
I have tried this:
SELECT *, GROUP_CONCAT(appartment_id, building)
FROM `ib30_history`
group by appartment_id, building
order by id DESC
It seems to work but im not sure that is the right way of doing it and the code that uses the output seems to make funny things running through the data so im not sure it really works!
SELECT yourtable.id, yourtable.building, yourtable.appartment_id, yourtable.status
FROM yourtable
INNER JOIN (
SELECT MAX(id) AS id
FROM yourtable
GROUP BY building, appartment_id
) AS child ON yourtable.id = child.id
Get rid of distinct and use something like:
GROUP BY building , appartment_id
What you're looking for is called GROUP BY, and MySQL's documentation knows a lot about how it's to be used. At the timeI type this, the OP doesn't contain a query so I cannot give you an example...
SELECT a.*
FROM table_name a
INNER JOIN
(SELECT MAX(id) as max_id
FROM table_name
GROUP BY appartment_id) b
ON (b.max_id = a.id)