Unix Technical Forum

Re: Problem with alias/case in query

This is a discussion on Re: Problem with alias/case in query within the pgsql Novice forums, part of the PostgreSQL category; --> On Mon, 7 Feb 2005, T. Steneker wrote: > Hello, > > I'm having a problem with the following ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:29 PM
Stephan Szabo
 
Posts: n/a
Default Re: Problem with alias/case in query

On Mon, 7 Feb 2005, T. Steneker wrote:

> Hello,
>
> I'm having a problem with the following pretty large and complicated (for me
> at least ;-)) query:
>
> SELECT Campaign.ID, Campaign.name,
> CampaignCategory.ID AS categoryID,
> CampaignCategory.name AS categoryName,
> (MAX(CampaignProductCommission.impressionCommissio nFixed) * 1000) AS
> cpmCommissionFixed,
> MAX(CampaignProductCommission.clickCommissionFixed ) AS
> clickCommissionFixed,
> MAX(CampaignProductCommission.leadCommissionFixed) AS
> leadCommissionFixed,
> MAX(CampaignProductCommission.saleCommissionFixed) AS
> saleCommissionFixed,
> MAX(CampaignProductCommission.saleCommissionVariab le) AS
> saleCommissionVariable,
> CASE WHEN
> CampaignSegment_Affiliate.campaignSegment_Affiliat eStatusID >= 2 THEN
> (
> CampaignSegment_Affiliate.campaignSegment.ID
> )
> ELSE
> (
> SELECT CampaignSegment.ID
> FROM CampaignSegment
> WHERE CampaignSegment.isGeneral AND
> CampaignSegment.campaignID = Campaign.ID
> )
> END AS "selectedSegmentID"
> FROM Campaign
> LEFT JOIN CampaignCategory ON (CampaignCategory.ID =
> Campaign.campaignCategoryID)
> LEFT JOIN CampaignSegment ON (CampaignSegment.campaignID = Campaign.ID)
> LEFT JOIN CampaignSegment_Affiliate ON
> (CampaignSegment_Affiliate.campaignSegmentID = "selectedSegmentID" AND
> CampaignSegment_Affiliate.affiliateID = '" . $this->user->ID . "')
> LEFT JOIN CampaignSegment_CampaignProduct ON
> (CampaignSegment_CampaignProduct.campaignSegmentID = "selectedSegmentID")
> LEFT JOIN CampaignProduct ON (CampaignProduct.ID
> IN (
> SELECT campaignProductID
> FROM CampaignSegment_Affiliate
> WHERE campaignSegmentID = "selectedSegmentID"
> ))
> LEFT JOIN CampaignProductCommmission ON
> (CampaignProductCommission.campaignProductID = CampaignProduct.ID)
>
> WHERE Campaign.campaignStatusID = '2'
> GROUP BY Campaign.ID, Campaign.name, Campaign.date, CampaignCategory.ID,
> CampaignCategory.name
> ORDER BY Campaign.date DESC, Campaign.ID DESC
> LIMIT 5 OFFSET 0
>
> So what it should do is this:
>
> Select campaign ID, campaign name, campaign category ID, campaign category
> name and the maximum cpm/click/lead and sale commission for the products in
> the segment the affiliate belongs to.
>
> It is possible that the affiliate doesn't belong to a segment yet
> (CampaignSegment_Affiliate table). In that case it should use the "general"
> segment, identified by the "isGeneral" field in the "CampaignSegment" table.
>
> The problem with this query is that I cannot use "selectedSegmentID" (in the
> CASE) in the LEFT JOIN beneath. Is there any other way I can do this in one
> PGSQL query? Or is there anyone with a hint for me ;-)? I already searched
> google and postgresql's documentation, but didn't find a way to do it yet.


You may want to look at moving the determination of "selectedSegmentID"
into a subquery in the from clause. It looks like you could join with a
subquery that joins CampaignSegment and CampaignSegment_Affiliate which
provides all of their columns plus the selectedSegmentID and then
reference that in the select clause and other joins.

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

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 08:21 AM.


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