Results 1 to 9 of 9

Thread: [RESOLVED] Impossible SQL statement?

  1. #1

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Resolved [RESOLVED] Impossible SQL statement?

    I have to come up with a convoluted SQL statement.

    Here are the tables:

    cfpurchases(purchaseid, storeid, sectionid, itemid, cost)
    stores(storeid, sale)
    sections(sectionid, olditemid, newitemid)
    purchaseindex(account, purchaseid)

    This is the output I need:
    account, purchaseid, cost(if old), cost(if new)

    I'm having trouble with the cost portions of the output.
    I have to come up with a way to check the itemid vs. the olditemid. If they match, then the cost(old) column will hold a value and the cost(new) column will not (= NULL). And vice-versa.

    Totally convoluted, I know. But I'm stuck with this problem.

    Regards,
    MizPippz

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Impossible SQL statement?

    i think you need to use Case statement with Inline query or better make one function and pass itemid to it and this will return the Cost.something like this


    Code:
    CREATE FUNCTION UDF_Get_COST
    (
    	@ItemId int,
            @IsOld bit
    )
    returns  decimal
    AS
    BEGIN
           DECLARE @SectionID int
    	
           if (@IsOld = 1) 
           SELECT @SectionID = sectionid from section where olditemid  = @ItemId
           else
           SELECT @SectionID = sectionid from section where newitemid  = @ItemId       
    
           if  @SectionID IS NULL
           RETURN 0
           ELSE
           RETURN (select cost from cfpurchases where  sectionid = @SectionID)
    END
    
    GO
    
    SELECT account, purchaseid, dbo.UDF_Get_COST(itemid,1),dbo.UDF_Get_COST(itemid,0)
    __________________
    Rate the posts that helped you

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Impossible SQL statement?

    You haven't told us which database system you are using, so what we are posting could be wrong for you.

    Here is an alternative to ritesh's version (which will be easier to convert for use with a different DBMS):
    Code:
    SELECT p.account, c.purchaseid, 
           CASE WHEN s.sectionid Is Not Null THEN c.cost ELSE Null END as OldCost, 
           CASE WHEN s.sectionid Is Null     THEN c.cost ELSE Null END as NewCost
    
    FROM       cfpurchases c 
    INNER JOIN purchaseindex p  ON (c.purchaseid = p.purchaseid)
    LEFT  JOIN section s        ON (s.sectionid = c.sectionid AND s.olditemid = s.newitemid)

  4. #4

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Re: Impossible SQL statement?

    My bad.
    SQL Server 2005

  5. #5
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Impossible SQL statement?

    have you tried si's query?
    __________________
    Rate the posts that helped you

  6. #6

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Re: Impossible SQL statement?

    After some fiddling to tweak Si's query to fit my needs exact... we have success. I'm using the query pretty much as is.

    No UDF function required. Perfect solution.

    Many thanks,
    MizPippz

  7. #7

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Re: Impossible SQL statement?

    At present, my output is as follows(each row):

    account, purchaseid, oldCost, newCost

    So, any single purchase can output as multiple lines.

    I'd like to be able (don't ask me why) to write a query that delivers the entire purchase as one row.

    e.g. - account, purchaseid, oldCost1, newCost1, oldCost2, newCost2, oldCost3, newCost3 ...etc

    Possible?

    Or should I just make the necessary changes in code-behind by creating a second datatable and making the necessary manipulations there?

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Impossible SQL statement?

    I'm not aware of a way to do that, and don't think I would ever want to do it either - to me that seems very hard to read, and I would go for a different output option instead (like perhaps keeping multiple rows, but hiding the first two columns when repeated).

  9. #9

    Thread Starter
    Hyperactive Member Ms.Longstocking's Avatar
    Join Date
    Oct 2006
    Posts
    399

    Re: Impossible SQL statement?

    ok. thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width