|
-
Jul 25th, 2008, 10:48 AM
#1
Thread Starter
Hyperactive Member
[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
-
Jul 25th, 2008, 11:00 AM
#2
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 
-
Jul 25th, 2008, 11:23 AM
#3
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)
-
Jul 25th, 2008, 11:25 AM
#4
Thread Starter
Hyperactive Member
Re: Impossible SQL statement?
-
Jul 25th, 2008, 05:08 PM
#5
Re: Impossible SQL statement?
have you tried si's query?
__________________
Rate the posts that helped you 
-
Jul 29th, 2008, 01:56 PM
#6
Thread Starter
Hyperactive Member
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
-
Jul 29th, 2008, 02:39 PM
#7
Thread Starter
Hyperactive Member
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?
-
Jul 29th, 2008, 03:07 PM
#8
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).
-
Jul 29th, 2008, 03:54 PM
#9
Thread Starter
Hyperactive Member
Re: Impossible SQL statement?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|