MySQL count listing between certain price range - mysql

From another post on the site, I took the following type of query. They were using sum, I am looking for a count of listings.
SELECT
COUNT(IF(sold_price < 20,1,0)) as 'Under 20',
COUNT(IF(sold_price BETWEEN 20 and 50,1,0)) as '20 - 50',
COUNT(IF(sold_price BETWEEN 50 and 100,1,0)) as '50 - 100',
COUNT(IF(sold_price BETWEEN 100 and 250,1,0)) as '100 - 250',
COUNT(IF(sold_price BETWEEN 250 and 500,1,0)) as '250 - 500',
COUNT(IF(sold_price BETWEEN 500 and 1000,1,0)) as '500 - 1000',
COUNT(IF(sold_price BETWEEN 1000 and 2000,1,0)) as '1000 - 2000',
COUNT(IF(sold_price > 2000,1,0)) as 'Over 2000'
FROM listings
where current_batch = 'Y'
All my results are coming back as the same number
Under 20 20 - 50 50 - 100 100 - 250 250 - 500 500 - 1000 1000 - 2000 Over 2000
94665 94665 94665 94665 94665 94665 94665 94665
Does anyone have any suggestion on show to do this or if this can be done with count?

MySQL documentation says the following(http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html#function_count):
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement
So replace:
IF(sold_price BETWEEN 20 and 50,1,0)
by
IF(sold_price BETWEEN 20 and 50,1,NULL)

Use SUM instead of COUNT in your query like:
SELECT
SUM(CASE WHEN our_price <= 10000 THEN 1 ELSE 0 END) AS p10000,
SUM(CASE WHEN our_price >= 10001 AND our_price <= 25000 THEN 1 ELSE 0 END) AS p25000,
SUM(CASE WHEN our_price >= 25001 AND our_price <= 50000 THEN 1 ELSE 0 END) AS p50000,
SUM(CASE WHEN our_price >= 50001 AND our_price <= 75000 THEN 1 ELSE 0 END) AS p75000,
SUM(CASE WHEN our_price >= 75001 AND our_price <= 100000 THEN 1 ELSE 0 END) AS p100000,
SUM(CASE WHEN our_price >= 100001 AND our_price <= 150000 THEN 1 ELSE 0 END) AS p150000,
SUM(CASE WHEN our_price >= 151000 AND our_price <= 200000 THEN 1 ELSE 0 END) AS p200000,
SUM(CASE WHEN our_price > 200000 THEN 1 ELSE 0 END) AS p200001
FROM products;

Related

How to change result SQL syntax for age group from colomn to new row

I Tried to grouping age from and found this answer(SQL Group by Age Range)
But the result is as a colomn not a row Like this
I Just want to convert the colomn into a row Like This
The query is:
SELECT SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') < 18 THEN 1 ELSE 0 END) AS '< 18',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 18 AND 25 THEN 1 ELSE 0 END) AS '18-25 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 26 AND 35 THEN 1 ELSE 0 END) AS '26-35 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 36 AND 45 THEN 1 ELSE 0 END) AS '36-45 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 46 AND 55 THEN 1 ELSE 0 END) AS '46-55 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 56 AND 65 THEN 1 ELSE 0 END) AS '56-65 Tahun',
SUM(CASE WHEN DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),user_detail.tanggal_lahir)), '%Y') BETWEEN 66 AND 200 THEN 1 ELSE 0 END) AS '>66 Tahun'
FROM user_detail
My table for tanggal_lahir:

SQL printing all in one row

I want to print data in two columns, like:
Count Category
But my code prints all in one row, like:
Count Category Count Category
Any tips?
Select sum(Case when population >= 1000000 and population < 5000000 then 1 else 0 end) as Count, '1 000 000 - 4 999 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '100 000 - 499 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '500 000 - 999 999' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, 'Under 100 000' as Category,
sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, 'Over 5 million' as Category
from cities
Select sum(Case when population >= 1000000 and population < 5000000 then 1 else 0 end) as Count, '1 000 000 - 4 999 999' as Category FROM cities
UNION
SELECT sum(Case when population >= 500000 and population < 100000 then 1 else 1 end) as Count, '100 000 - 499 999' as Category FROM cities
and so on...
Concat all single resuts in one result table with UNION keyword
your else in case part should be everywhere 0 not 1 if you just want to count the cities...
A Better solution anyway would be
SELECT COUNT(cities.primKey) as Count, '1 000 000 - 4 999 999' as Category FROM cities WHERE population >= 1000000 and population < 5000000
UNION
SELECT COUNT(cities.primKey) as Count, '1 000 000 - 4 999 999' as Category FROM cities WHERE population >= 500000 and population < 100000
UNION ...
SELECT CASE WHEN Population >= 5000000
THEN 'Over 5 million'
WHEN Population >= 1000000 AND Population < 5000000
THEN '1 000 000 - 4 999 999'
WHEN Population >= 500000 AND Population < 1000000
THEN '500 000 - 999 999'
WHEN Population >= 100000 AND Population < 500000
THEN '100 000 - 499 999'
WHEN Population < 100000
THEN 'Under 100 000'
END [Category]
,COUNT(*) [Count]
FROM Cities
If you want to get all categories even if there are no cities that belong to it, you can do:
select sum(if(ci.population is null, 0, 1)) as 'Count', cat.Category
from (
select 0 as 'low', 100000 as 'high', 'Under 100 000' as Category, 1 as 'ord'
union
select 100000, 499999, '100 000 - 499 999', 2
union
select 500000, 999999, '500 000 - 999 999', 3
union
select 1000000, 4999999, '1 000 000 - 4 999 999', 4
union
select 5000000, null, 'Over 5 million' , 5
) as cat
left join cities ci on ci.population between cat.low and ifnull(cat.high, ci.population)
group by cat.Category, cat.ord
order by cat.ord

making rows to columns in mysql

i have this hour wise data , i want to generate reports where i have to show the revenue every hour in a single row groupy by product,mode,region (sorry for bad format)
price product mode region HOUR
1. 0 p1 direct reg1 1
2. 10 p3 indirect reg2 2
3. 0 p2 direct reg1 1
4. 0 p1 indirect reg2 2
5. 0 p2 direct reg5 3
6. 0 p1 direct reg1 3
7. 0 p1 direct reg2 3
8. 0 p3 indirect reg4 17
9. 0 p4 direct reg2 17
10. 21 p1 direct reg2 17
HOUR1 HOUR2 HOUR3 HOUR4 HOUR5....HOUR23 product mode region
1. 0 10 1 30 p1 direct reg1
2. 0 5 1 10 p1 indirect reg2
Unfortunately mysql does not have any inbuilt pivot function so you need to either write a dynamic sql for the unknown set of pivot element or can use the most commonly used pivot generating technique for known set of pivot element, and in your case its known set since you are looking at pivot data from hour1 till hour23 and it could be done as
select
sum( case when HOUR = 1 then price else 0 end) as `HOUR1`,
sum( case when HOUR = 2 then price else 0 end) as `HOUR2`,
sum( case when HOUR = 3 then price else 0 end) as `HOUR3`,
sum( case when HOUR = 4 then price else 0 end) as `HOUR4`,
sum( case when HOUR = 5 then price else 0 end) as `HOUR5`,
sum( case when HOUR = 6 then price else 0 end) as `HOUR6`,
sum( case when HOUR = 7 then price else 0 end) as `HOUR7`,
sum( case when HOUR = 8 then price else 0 end) as `HOUR8`,
sum( case when HOUR = 9 then price else 0 end) as `HOUR9`,
sum( case when HOUR = 10 then price else 0 end) as `HOUR10`,
sum( case when HOUR = 11 then price else 0 end) as `HOUR11`,
sum( case when HOUR = 12 then price else 0 end) as `HOUR12`,
sum( case when HOUR = 13 then price else 0 end) as `HOUR13`,
sum( case when HOUR = 14 then price else 0 end) as `HOUR14`,
sum( case when HOUR = 15 then price else 0 end) as `HOUR15`,
sum( case when HOUR = 16 then price else 0 end) as `HOUR16`,
sum( case when HOUR = 17 then price else 0 end) as `HOUR17`,
sum( case when HOUR = 18 then price else 0 end) as `HOUR18`,
sum( case when HOUR = 19 then price else 0 end) as `HOUR19`,
sum( case when HOUR = 20 then price else 0 end) as `HOUR20`,
sum( case when HOUR = 21 then price else 0 end) as `HOUR21`,
sum( case when HOUR = 22 then price else 0 end) as `HOUR22`,
sum( case when HOUR = 23 then price else 0 end) as `HOUR23`,
product,
mode,
region
from mytable
group by product,mode,region;
http://sqlfiddle.com/#!9/b2e868/1

Calculate percentage and total after create categories mysql

I've this query
SELECT
trage,
CASE trage
WHEN '<18' THEN SUM(CASE WHEN AGE <18 THEN 1 ELSE 0 END)
WHEN '18-24' THEN SUM(CASE WHEN AGE >= 18 AND AGE <= 24 THEN 1 ELSE 0 END)
WHEN '25-34' THEN SUM(CASE WHEN AGE >= 25 AND AGE <= 34 THEN 1 ELSE 0 END)
WHEN '35-44' THEN SUM(CASE WHEN AGE >= 35 AND AGE <= 44 THEN 1 ELSE 0 END)
WHEN '45-54' THEN SUM(CASE WHEN AGE >= 45 AND AGE <= 54 THEN 1 ELSE 0 END)
WHEN '>=55' THEN SUM(CASE WHEN AGE >= 55 THEN 1 ELSE 0 END)
END Total
FROM
( SELECT
t_personne.pers_date_naissance,
t_personne.pers_date_inscription,
TIMESTAMPDIFF(Year, t_personne.pers_date_naissance, t_personne.pers_date_inscription)
- CASE
WHEN MONTH(t_personne.pers_date_naissance) > MONTH(t_personne.pers_date_inscription)
OR (MONTH(t_personne.pers_date_naissance) = MONTH(t_personne.pers_date_inscription)
AND DAY(t_personne.pers_date_naissance) > DAY(t_personne.pers_date_inscription))
THEN 1 ELSE 0
END AS AGE
FROM t_personne
) AS Total
CROSS JOIN
( SELECT '<18' trage UNION ALL
SELECT '18-24' UNION ALL
SELECT '25-34' UNION ALL
SELECT '35-44' UNION ALL
SELECT '45-54' UNION ALL
SELECT '>=55'
)a
GROUP BY trage
ORDER BY FIELD(trage, '<18', '18-24', '25-34', '35-44', '45-54', '>=55')
it give a table with two columns trage and Total for all categories
How to add a column percentage with a line TOTAL for the column Total and %
Thanks for your help
For the time being, you can't do this. To support this MySQL needs Window Function support which it still doesn't have. If you need functions like these I would recommend switching to PostgreSQL.
Also take a look at this question: MySql using correct syntax for the over clause

Count records for each age range

I have a mysql table
TABLE name test
Id INT PRIMARY KEY
Lahir Date
Gender CHAR(1)
Nama Varchar(100)
id_desa CHAR(4)
How can I get report like this?
id_desa 0-10 11-20 21-30 31-40 41-50
10B 1 2 0 1 8
10C 2 4 7 1 0.
try
select
id_desa,
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 0 and 10 then 1 else 0 end) as '0-10',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 11 and 20 then 1 else 0 end) as '11-20',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 21 and 30 then 1 else 0 end) as '21-30',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 31 and 40 then 1 else 0 end) as '31-40',
sum(case when FLOOR(DATEDIFF(Lahir,NOW())/365) between 41 and 50 then 1 else 0 end) as '41-50'
from tbl
You can try to use this query.
I assume that the table name is name_test.
SELECT id_desa,
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 0 AND 10) as "0-10",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 11 AND 20) as "11-20",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 21 AND 30) as "21-30",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 31 AND 40) as "31-40",
(SELECT Count(*) FROM name_test WHERE DATE_FORMAT(FROM_DAYS(DATEDIFF(Now(),Lahir)), "%y")+0 BETWEEN 41 AND 50) as "41-50"
FROM name_test
MySQL has a very nice function called TIMESTAMPDIFF
SELECT
id_desa,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 0 AND 10) `0-10`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 11 AND 20) `11-20`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 21 AND 30) `21-30`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 31 AND 40) `31-40`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) BETWEEN 41 AND 50) `41-50`,
SUM(TIMESTAMPDIFF(YEAR, Lahir, CURRENT_DATE) > 50) `50+`
FROM tablename
GROUP BY id_desa