I'm trying to select rows where a certain column does not have a certain value, such as 0, but I'm unable to get this to work.
SELECT *
FROM rentals
GROUP BY date, rooms, price
HAVING show_independently < 1
If show_independently is 1, then I don't want to group them. However, this statement shows no rows even though most rows have show_independently as 0.
SELECT date, rooms, price
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price
If you only want to group some rows and leave others ungrouped, you can use a UNION:
SELECT *
FROM rentals
WHERE show_independently <> 1
GROUP BY date, rooms, price
UNION ALL
SELECT *
FROM rentals
WHERE show_independently = 1
This groups only those where show_independently is not 1, and includes the rest without grouping them.
A HAVING clause is used when you are using an aggregate function to filter data.
A typical query with HAVING:
SELECT yourColumn, aggregate_function(otherColumn)
FROM yourTable
WHERE yourColumn = someValue
GROUP BY yourColumn
HAVING aggregate_function(otherColumn) = someOtherValue
I think you want to be using a WHERE clause:
SELECT date, rooms, price
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price
Related
I have a table name agents_commission have columns id, agent_id, deal_id, commission, deal_date.
I want to select agent_id, commission from agents_commission where month(date_deal) = '$month' and year(date_deal) ='$year';
but if the table have some rows have the same agent_id join the same rows in one row and calculate the commission values like row 66666.7 + 100000 as in row 1, 2
to be the final fetch_assoc as
array('agent_id'=>1, 'commision'=>166666.7);
thanks
You can not select all when you want to group by only one column(agentid). you can do something like below
select agent_id, sum(commission) as commission from temp where MONTH(deal_date) = '09' and year(deal_date) ='2018' group by agent_id;
The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions.
Refer https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html
Try using group by and aggregation:
select agentid, sum(commission) as commission
from agents_commission
where month(date_deal) = '$month' and year(date_deal) ='$year';
group by agentid
I have the following ORDERS table
I know query to select the customer that has ordered the greatest quantity. However, how would it work, if say, two customers have the same quantity. What query should I write to show both the customers?
You can use a subquery which checks that the quantity for a given record matches the largest quantity observed in the table:
SELECT *
FROM yourTable
WHERE qty = (SELECT MAX(qty) FROM yourTable)
This will return multiple records if there are more than one customer sharing the maximum quantity.
If you only wanted to get back a single record, even in the presence of ties, you could use this approach:
SELECT *
FROM yourTable
ORDER BY qty DESC
LIMIT 1
I think you want sum of qty per custNum.
If so you can try like:
select custNum,
sum(qty) as qty
from Orders
group by custNum
order by sum(qty) desc;
Fiddle here:
http://sqlfiddle.com/#!9/47931b/10
SELECT custnum,sum(qty) as total
FROM orders
group by custnum
having sum(qty) = (SELECT MAX(qty) FROM orders);
This will return both values.
Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;
I'm storing ratings for an item in a table called ratings.
value is an integer between 0 and 7 (the rating value).
As an example, let's say showcase_id = 1 has 10 total ratings:
5 ratings are value = 7
2 ratings are value = 6
2 ratings are value = 5
1 rating is value = 4
no ratings for values 0,1,2,3
Is there any efficient way I can select the total number of ratings for each specific value, the total number of ratings and the average rating from 1 single query?
e.g. the number of rows/count WHERE value = 6 is 2. Would I need to do 7 separate subqueries?
SELECT AVG(value),COUNT(*),????
FROM ratings
WHERE showcase_id = :showcase_id
Are you referring to count(distinct)?
SELECT AVG(value), COUNT(*), COUNT(DISTINCT value)
FROM ratings
WHERE showcase_id = :showcase_id;
EDIT:
If you want the total for each value, you can stuff this into one column, using a subquery:
SELECT SUM(cnt * value) / SUM(cnt) as average,
SUM(cnt) as total_cnt,
GROUP_CONCAT(value, '-', cnt ORDER BY VALUE) as values
FROM (SELECT value, COUNT(*) as cnt
FROM ratings r
WHERE showcase_id = :showcase_id
GROUP BY value
) r;
Perhaps the subquery meets your needs as well.
You can use the WITH ROLLUP modifier to GROUP BY to get the counts and average for each value, as well as the totals for everything (if you group by multiple columns it will also create subtotals for each inner group).
SELECT value, AVG(value) AS avg, COUNT(*) as count
FROM ratings
WHERE showcase_id = 1
GROUP BY value WITH ROLLUP
This will produce a row for each value with its count, and then a row with value = NULL for that aggregates the entire result.
DEMO
What's wrong with group by?
SELECT AVG(value),COUNT(*), value
FROM ratings
WHERE showcase_id = :showcase_id
GROUP BY value;
EDIT (with the overall avg & totes):
select count(*) total_by_value, value, s1.full_avg, s1.full_total
from ratings r,
(select avg(value) full_avg, count(*) full_total from ratings) s1
group by s1.full_avg, s1.full_total, value;
I have a table with emp_id, income, etc.
I wish to get number of records for a query like
select * from table_name where income <= 500;
There will be at least 3 such income groups - which will b given at report generation time.
Further I wish to get all 3 Counts - and group the results by the count of their respective income group - all this in a single query.
What is the easiest way to do this?
Can you try this ,if this doesn't suite your need you may need to write a custom stored procedure
SELECT
sum((income <= 500)) as range1,
sum((income <= 1000)) as range2
FROM table_name
sample fiddle
You can use a CASE expression to create your categories, and then a GROUP BY to summarize the categories.
SELECT COUNT(*) AS num,
CASE WHEN income IS NULL THEN 'missing'
WHEN income <= 0 THEN '0'
WHEN income <= 500 THEN '1 - 500'
WHEN income <= 1000 THEN '501-1000'
ELSE '> 1000'
END AS category
FROM table_name
GROUP BY category WITH ROLLUP
Including the WITH ROLLUP clause will give you an overall count as well as the count of each category.