Bluehost.com Web Hosting $6.95

Intersection of Two Queries

This is a discussion on Intersection of Two Queries within the MySQL Database forums, part of the Database Forums category; I want to find users who have more than x pages views yesterday and more than y page views during ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-23-2007
cookieplanter
 
Posts: n/a
Default Intersection of Two Queries

I want to find users who have more than x pages views yesterday and
more than y page views during the last 30 days. My two queries work
well independently. But how do I find users that satisfy both
conditions?

SELECT DISTINCT user_id
FROM `pageviews`
WHERE pagevisit LIKE '2007-03-23%'
GROUP BY user_id
HAVING count( id ) > x

SELECT user_id
FROM pageviews
WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
GROUP BY user_id
HAVING count(DISTINCT(date(pagevisit))) > y

The UNION of both these queries also works fine, but that would give
me users that satisfy either condition, right? I only want users that
satisfy *both* conditions.

Any help would be greatly appreciated.

Cheers,
Krishna Srinivasan.

Reply With Quote
  #2 (permalink)  
Old 03-23-2007
Paul Lautman
 
Posts: n/a
Default Re: Intersection of Two Queries

cookieplanter wrote:
> I want to find users who have more than x pages views yesterday and
> more than y page views during the last 30 days. My two queries work
> well independently. But how do I find users that satisfy both
> conditions?
>
> SELECT DISTINCT user_id
> FROM `pageviews`
> WHERE pagevisit LIKE '2007-03-23%'
> GROUP BY user_id
> HAVING count( id ) > x
>
> SELECT user_id
> FROM pageviews
> WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
> GROUP BY user_id
> HAVING count(DISTINCT(date(pagevisit))) > y
>
> The UNION of both these queries also works fine, but that would give
> me users that satisfy either condition, right? I only want users that
> satisfy *both* conditions.
>
> Any help would be greatly appreciated.
>
> Cheers,
> Krishna Srinivasan.


Well you could really do with INTERSECT, but of course MySQL doen't support
it yet.

So you could make one of them a subquery and do a FULL OUTER JOIN, rejecting
all records where either side of the join is null.


Reply With Quote
  #3 (permalink)  
Old 03-23-2007
AlterEgo
 
Posts: n/a
Default Re: Intersection of Two Queries

Paul,

Untested, but try this:

SELECT pv1.userid FROM
(
SELECT DISTINCT user_id
FROM `pageviews`
WHERE pagevisit LIKE '2007-03-23%'
GROUP BY user_id
HAVING count( id ) > x
) pv1
INNER JOIN
(
SELECT user_id
FROM pageviews
WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
GROUP BY user_id
HAVING count(DISTINCT(date(pagevisit))) > y
) pv2
on pv1.user_id = pv2.user_id

-- Bill

"Paul Lautman" <paul.lautman@btinternet.com> wrote in message
news:56ilceF26qukeU1@mid.individual.net...
> cookieplanter wrote:
>> I want to find users who have more than x pages views yesterday and
>> more than y page views during the last 30 days. My two queries work
>> well independently. But how do I find users that satisfy both
>> conditions?
>>
>> SELECT DISTINCT user_id
>> FROM `pageviews`
>> WHERE pagevisit LIKE '2007-03-23%'
>> GROUP BY user_id
>> HAVING count( id ) > x
>>
>> SELECT user_id
>> FROM pageviews
>> WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
>> GROUP BY user_id
>> HAVING count(DISTINCT(date(pagevisit))) > y
>>
>> The UNION of both these queries also works fine, but that would give
>> me users that satisfy either condition, right? I only want users that
>> satisfy *both* conditions.
>>
>> Any help would be greatly appreciated.
>>
>> Cheers,
>> Krishna Srinivasan.

>
> Well you could really do with INTERSECT, but of course MySQL doen't
> support it yet.
>
> So you could make one of them a subquery and do a FULL OUTER JOIN,
> rejecting all records where either side of the join is null.
>



Reply With Quote
  #4 (permalink)  
Old 03-24-2007
cookieplanter
 
Posts: n/a
Default Re: Intersection of Two Queries

On Mar 24, 1:36 am, "AlterEgo" <altereg...@dslextreme.com> wrote:
> Paul,
>
> Untested, but try this:
>
> SELECT pv1.userid FROM
> (
> SELECT DISTINCT user_id
> FROM `pageviews`
> WHERE pagevisit LIKE '2007-03-23%'
> GROUP BY user_id
> HAVING count( id ) > x
> ) pv1
> INNER JOIN
> (
> SELECT user_id
> FROM pageviews
> WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
> GROUP BY user_id
> HAVING count(DISTINCT(date(pagevisit))) > y
> ) pv2
> on pv1.user_id = pv2.user_id
>
> -- Bill
>
> "Paul Lautman" <paul.laut...@btinternet.com> wrote in message
>
> news:56ilceF26qukeU1@mid.individual.net...
>
> > cookieplanter wrote:
> >> I want to find users who have more than x pages views yesterday and
> >> more than y page views during the last 30 days. My two queries work
> >> well independently. But how do I find users that satisfy both
> >> conditions?

>
> >> SELECT DISTINCT user_id
> >> FROM `pageviews`
> >> WHERE pagevisit LIKE '2007-03-23%'
> >> GROUP BY user_id
> >> HAVING count( id ) > x

>
> >> SELECT user_id
> >> FROM pageviews
> >> WHERE DATE_SUB(CURDATE(), INTERVAL 90 DAY) <= pagevisit
> >> GROUP BY user_id
> >> HAVING count(DISTINCT(date(pagevisit))) > y

>
> >> The UNION of both these queries also works fine, but that would give
> >> me users that satisfy either condition, right? I only want users that
> >> satisfy *both* conditions.

>
> >> Any help would be greatly appreciated.

>
> >> Cheers,
> >> Krishna Srinivasan.

>
> > Well you could really do with INTERSECT, but of course MySQL doen't
> > support it yet.

>
> > So you could make one of them a subquery and do a FULL OUTER JOIN,
> > rejecting all records where either side of the join is null.


This works! Thank you so much.

Krishna Srinivasan.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:58 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0