Unix Technical Forum

Intersection of Two Queries

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


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:28 AM
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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:28 AM
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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:28 AM
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.
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:28 AM
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.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:23 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com