Is it possible to pull 2 results from a sub query in a sql statement?
I have:
"SELECT
(SELECT bid FROM auction_bids WHERE itemID=a.id ORDER BY bid DESC LIMIT 1) as topbid,
a.* FROM auction_items a ORDER BY a.date DESC LIMIT 15"
The part where it returns the topbid, i'd also like it to pull not only bid (as topbid) but also date (as topdate) as well. How can I do that? Do I need another sub query or can it pull both in one?
Dependent subquery (depending on some values outside, like a.id in your case) is not a very efficient way to find maximum values in subsets.
Instead use a subquery with GROUP BY:
SELECT b.topbid, b.topdate, a.*
FROM auction_items a
LEFT JOIN
( SELECT itemID, MAX(bid) as topbid, MAX(date) as topdate
FROM auction_bids
GROUP BY itemID ) b
ON a.id = b.itemID
ORDER BY a.date DESC
LIMIT 15
Related
Actually I'm working with the following table fsa_areas:
Note that each area has a responsible
Now, what I need to do, is to order the same table as following:
Note that now the results are ordered by the the responsible with more areas and at the end the responsible with less areas.
Is there a way to order them in that way?
You can use a COUNT subquery in the ORDER BY clause:
select a.*
from fsa_areas a
order by (select count(*) from fsa_areas a1 where a1.Responsible = a.Responsible) desc
Another way is to get the count in a derived table and join the base table to it
select a.*
from (
select Responsible, count(*) as cnt
from fsa_areas
group by Responsible
) r
join fsa_areas a using(Responsible)
order by r.cnt desc
In MySQL 8 you can use COUNT() as window function:
select *, count(*) over (partition by Responsible) as cnt
from fsa_areas
order by cnt desc
Im trying to do two queries on the same table to get the Count(*) value.
I have this
SELECT `a`.`name`, `a`.`points` FROM `rank` AS a WHERE `id` = 1
And in the same query I want to do this
SELECT `b`.`Count(*)` FROM `rank` as b WHERE `b`.`points` >= `a`.`points`
I tried searching but did not find how to do a Count(*) in the same query.
Typically you would not intermingle a non aggregate and aggregate query together in MySQL. You might do this in databases which support analytic functions, such as SQL Server, but not in (the current version of) MySQL. That being said, your second query can be handled using a correlated subquery in the select clause the first query. So you may try the following:
SELECT
a.name,
a.points,
(SELECT COUNT(*) FROM rank b WHERE b.points >= a.points) AS cnt
FROM rank a
WHERE a.id = 1;
As I understand from the question, you want to find out in a table for a given id how many rows have the points greater than this row. This can be achieved using full join.
select count(*) from rank a join rank b on(a.id != b.id) where a.id=1 and b.points >= a.points;
This query is MySql
select
a.id,
a.name,
a.vehicle,
a.amt,
a.date
(select id
from tbl1
where date < a.date and vehicle = a.vehicle
LIMIT 0,1) as prv
FROM
root_tbl a
WHERE
amt <> 0
I need this query in SQL Server. Could you please assist me.
LIMIT is not supported by SQL Server.
If you want to get the first x rows of a table, you have to use TOP, as in SELECT TOP (10) * FROM tbl1.
To do an offset equivalent, there are a few techniques, such as a subquery adding ROW_NUMBER() to the result set, and in the parent query filtering by for the desired offset.
SELECT *
FROM (
SELECT
id,
ROW_NUMBER() OVER (ORDER BY id) AS RowNum
FROM tbl1
) T
WHERE RowNum BETWEEN 51 AND 75 -- Similar to LIMIT 50,25
For your specific query, you seem to just need a TOP to be most efficient, so the following should work for your query.
select
a.id,
a.name,
a.vehicle,
a.amt,
a.date
(select top (1) id from tbl1 where date < a.date and vehicle=a.vehicle)as prv
FROM tbl1 a
I have the following data set.
How would the query look if wanted the most occuring show on top and limit the result for 20 shows?
EDIT
I have searched on the web for this and i need to use the GROUP BY method from sql. but when i make the query
SELECT `show` FROM fans GROUP BY `show` LIMIT 20
i do not get the desired result.
SELECT a.*, b.TotalCount
FROM TableName a
INNER JOIN
(
SELECT c.show, COUNT(*) totalCount
FROM TableName c
GROUP BY c.show
) b ON a.show = b.show
ORDER BY b.TotalCount DESC
LIMIT 20
if you want to list one record for every show, you can simply use GROUP BY
SELECT a.show, COUNT(*) TotalCount
FROM TableName a
GROUP BY a.Show
ORDER BY TotalCount DESC
LIMIT 20
I want to find the earliest date in a table. How do I put this into a join statement:
SELECT date FROM table1 WHERE orderno = 222 ORDER BY date LIMIT 1
Orderno 222 can have 1-* many rows in table1 that's why I'm using LIMIT 1
I have table these tables:
Order
OrderLine
ProductionDate
Order can have 1-* ProductionDates so when i do my join of ProductionDate i want to find the earliest date.
So my guess of a sql statement would be something like:
SELECT * FROM Order
LEFT JOIN (
IN SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1)
but this doesn't work. And i would like to know what i should change to get it to work?
I'm not sure if MySQL supports a LIMIT In a derived table, but if it does, the syntax should be:
SELECT ord.*
FROM `Order` ord
LEFT JOIN (SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1) t
ON t.date = ord.date
Note that order is a reserved word, so you will need to quote it properly. Either using MySQL dreaded backticks (see above) or - if you configured your server to be standard's compliant - using double quotest like this: "order"
SELECT `date`
FROM `table1`
INNER JOIN `table1`.`date` ON `table2`.`date`
ORDER BY date ASC LIMIT 1
Is that what you mean?