Reputation: 83
like
select ho from (select sourceaddress,count(sourceaddress) as
src,hour(eventtime) as ho
from default.fullandfinal
where sourceaddress='0.0.0.0' and eventtime between '2019-05-11 00:00:00' and
'2019-05-11 19:59:59'
group by sourceaddress,hour(eventtime) order by sourceaddress,ho) t where
src=28350;
the output of this query is 11 and i want to use this output in my nxt query which is
select sourceaddress,destinationaddress,destinationport,name,count(*) as count
from fullandfinal
where eventtime like "11%" and sourceaddress='0.0.0.0'
group by sourceaddress,destinationaddress,destinationport,name
order by count desc limit 5;
i want to write single query for this is it possible ?
Upvotes: -2
Views: 75
Reputation: 16908
Considering MySQL - Yes possible. You have to use the first query as Sub Query in the second Query. The query structure will be something like this-
SELECT *,
(SELECT ID FROM TABLE_1 WHERE ....) AS [From Other Query] -- In the selection part
FROM TABLE_2
WHERE TABLE_2.ID = (SELECT ID FROM TABLE_1 WHERE ....) -- In Where condition
For both above case, you have to make sure your sub query returns one single value that is like 11 you mentioned.
Try This-
SELECT sourceaddress,
destinationaddress,
destinationport,
[name],
COUNT(*) as [count]
FROM fullandfinal
WHERE
eventtime LIKE
(
SELECT ho FROM
(
SELECT sourceaddress,
COUNT(sourceaddress) AS src,
HOUR(eventtime) AS ho
FROM DEFAULT.fullandfinal
WHERE sourceaddress='0.0.0.0'
AND eventtime BETWEEN '2019-05-11 00:00:00' AND '2019-05-11 19:59:59'
GROUP BY sourceaddress,
HOUR(eventtime)
ORDER BY sourceaddress,ho
) t
WHERE src=28350
) + '%'
AND sourceaddress='0.0.0.0'
GROUP BY sourceaddress,destinationaddress,destinationport,name
ORDER BY COUNT(*) DESC
limit 5;
Upvotes: 0