my query:
SELECT events.*, SUM(analyt_event_hits.ajax_hits) AS ajax_hits
FROM events
LEFT JOIN analyt_event_hits
ON events.event_id = analyt_event_hits.event_id
WHERE events.date >= '$d'
the problem:
this is only returning one result,
when it should be returning many.
The query works fine if i remove SUM(anal_event_hits.ajax_hits) AS ajax_hits
I'm a bit of a MySQL novice so hopefully i'm missing something obvious!
Try adding
GROUP BY events.event_id
to the end.
If you use a group function like SUM in a statement containing no GROUP BY clause, it is equivalent to grouping on all rows.
and That's why you get only one row in results
More details on Aggregate Functions :
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Related
I have this tbl_transactions, this is the table
the image is the result after querying
SELECT DISTINCT vehicle_type_name, vehicle_plate_number FROM tbl_transactions
now, I need to count the number of 2 Wheeler, 4 Wheeler, etc... can someone help me to this. I've used the combination of DISTINCT and WHERE clause, but it shows error in query.
You can select in a subquery only the distinct values and count it on the outer query. If this is what you are trying please try and let me know if it helps and feel free to ask if something is unclear.
SELECT vehicle_plate_number,count(*) as number_of_rows
FROM (
SELECT vehicle_type_name,
vehicle_plate_number
FROM tbl_transactions
GROUP BY vehicle_plate_number,vehicle_type_name
) as t1
GROUP BY vehicle_plate_number;
Demo: https://www.db-fiddle.com/f/vwi9bkdUVPoZeURdNAEoDX/4
How can I made the query below select only one record? Each game has two records (one record for each team). If there are nine (9) games there would be eighteen (18) records. I want to select only one record, not both records, for each game.
If I use DISTINCT with only one column (game_id_2) it works fine or returns only nine records. However, if I try to add more columns the DISTINCT directive no longer works.
SELECT DISTINCT
B.game_id_2,
B.GmeYear,
B.GmeMonth,
B.GmeDay,
B.GmeDate,
B.GmeTime,
B.GmeOrd,
B.Home,
B.DivPlay,
L.Instit,
FROM BsbGme B LEFT JOIN LeagueTeam L
ON B.team_id = L.team_id
WHERE B.NonD1=''
AND B.team_id IN ($participant_str)
AND B.GmeMonth = $GameMonth
AND B.GmeDay = $GameDay
ORDER BY B.game_id_2 ASC
According to w3schools dot come the DISTINCT directive is supposed to work with multiple columns. But it's not working with multiple columns in my example.
w3schools example:
SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;
http://www.w3schools.com/sql/sql_distinct.asp
I have looked at several of the other answers to this same question on StackOverflow but I can't get any of them to work.
Thank you in advance.
try using brackets DISTINCT (your, columns) or use GROUP BY.
trying to make the move to MySQLi and some of it is baffling me. Quick question as my query is currently saying there's 1 result even if there isn't one...
How do I go about finding the number of rows returned for this:
$sql = <<<SQL
SELECT u.*, t.*
FROM Users u
LEFT JOIN Transactions t USING (UserID)
WHERE UserID = $UserID
ORDER BY Date DESC
LIMIT 5
SQL;
Presently it is returning 1 result from an empty table with all the values being null.
You should try to place the value instead of $UserID. Then, you should try to run this query in MySQL.
Also, check your code which processes query results.
The reason I was getting results when there weren't any was because of using Left Join which nulls any results that don't match. as was pointed out in comments. I needed Inner Join instead, which is working as required.
I need help with an advanced SQL-query (MSSQL 2000).
I have a table called Result that lists athletics 100 meter race-times. A runner can have several racetimes but I want to show only the best time from each runner.
The Result-table contains three columns, Result_id, athlete_id, result_time. So athlete_id must be unique when I list the values and result_time must be the fastest (lowest) value.
Any ideas?
In SQL Server 2000, you can't use windows functions. You can do this as follows:
select r.*
from result r join
(select athlete_id, min(result_time) as mintime
from result r
group by athlete_id
) rsum
on rsum.athlete_id = r.athlete_id and r.time = rsum.mintime
In more recent versions of SQL Server, you would use row_number().
If you simply need the fastest time for each athlete_id, do this:
select athelete_id, min(result_time) as FastestTime
from result
group by athelete_id
To show additional columns from the result table, you can join back to it like this:
select r.*
from result r
inner join (
select athelete_id, min(result_time) as FastestTime
from result
group by athelete_id
) rm on r.athelete_id = rm.athelete_id and r.result_time = rm.FastestTime
What you want is to use an aggregate function. in this case min() which will select the minumin data from all the rows that have the same data in the other selected columns. This means you also have to us the group by clause. The query below should give you the results you want.
Edit: If you need other columns, just bring them into the select clause, then add them to the group by clause like below:
select althlete_id, result_id, min(result_time) as result_time from result-table group by althlete_id, result_id
select althlete_id, result_id, min(result_time) as result_time, race_date from result-table group by althlete_id, race_date, result_id
Edit: You need to add all the columns into the group by that aren't part of an aggregate function. Aggregate functions are ones like min(), max(), avg() and so on.
Short answer: If you aren't putting a column in brackets, it probably has to be in the group by.
Hi, this is my query:
SELECT tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
I am getting this output,
I want to get only one data-row for one means distinct value of Order ID. How can I change my sql query to get my desired result?
SELECT distinct(tbl_order_detail.order_id), tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
FROM tbl_order_detail
INNER JOIN tbl_order_lead_send_detail
ON tbl_order_detail.order_id=tbl_order_lead_send_detail.order_id
where tbl_order_detail.order_status='Active'
ORDER BY tbl_order_lead_send_detail.send_date_time DESC
SELECT DISTINCT tbl_order_detail.order_id, ...
Two possibilities:
1) Select distinct ..
2) Select ... group by tbl_order_detail.order_id, tbl_order_lead_send_detail.customer_id, tbl_order_detail.order_title, tbl_order_detail.dealer_id , tbl_order_lead_send_detail.send_date_time
Be aware that even if you use the keyword distinct in your query, it'll still return more than one rows for the same order id if at least one of the columns return a different data.
Your result image shows 4 columns, while your query asked for 5; so, it's not 100% possible to determine where the problem lies.
That being said, use select distinct, and see if it solves your problem. If it doesn't, you might have to remove the column(s) with the different data from the query.
Happy coding!
From some of your screenshots it appears that customer id is different for each row. You need to show all the output to get sensible answers.
I'm pretty certain SELECT DISTINCT isn't broken, so you must be selecting non-unique rows.