Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 Reporting Services Development
 Help me with this please ...it's urgent

Author  Topic 

swenri
Yak Posting Veteran

72 Posts

Posted - 2007-08-23 : 12:32:26
I'm getting an error in Visual Studio 2005, when I run this query. It says Group by error. The same query works in management studio
Tried all ways .. no success. Please help. Thanks,
------------------------------------------------------------------
SELECT DISTINCT
'0' AS Cpgn_Tot_MailedQty,
ISNULL(GIFTS.Final_No_Of_Gifts, 0) AS Final_No_of_Gifts,
ISNULL(GIFTS.Final_Total_Income, 0) AS Final_Total_Income,
ISNULL(Campaign_One.Final_Mailing_Cost, 0) AS Final_Mailing_Cost, ISNULL(Campaign_One.Final_Fulfillment_Cost, 0) AS Final_Fulfillment_Cost,
ISNULL(Campaign_One.CampaignId, 0) AS CampaignID
FROM (SELECT CampaignId, SUM(ISNULL(MailActualCost, 0)) AS Final_Mailing_Cost, SUM(ISNULL(FulfillmentActualCost, 0)) AS Final_Fulfillment_Cost
FROM PortfolioReport.Campaign
WHERE (CampaignDesc = '1988 Christmas - I')
GROUP BY CampaignId) AS Campaign_One LEFT OUTER JOIN
(SELECT PortfolioReport.Source.CampaignId, COUNT(ISNULL (PortfolioReport.Gift.GiftId, 0)) AS Final_No_Of_Gifts,
SUM(ISNULL(PortfolioReport.Gift.LineItemAmount, 0)) AS Final_Total_Income
FROM PortfolioReport.Gift INNER JOIN
PortfolioReport.Source ON PortfolioReport.Gift.SourceId = PortfolioReport.Source.SourceId INNER JOIN
PortfolioReport.Campaign AS Campaign_1 ON PortfolioReport.Source.CampaignId = Campaign_1.CampaignId
WHERE (PortfolioReport.Source.SourceId <> 0) AND (Campaign_1.CampaignDesc = '1988 Christmas - I') AND
(PortfolioReport.Gift.DateDeposited >=
(SELECT MIN(g.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS g INNER JOIN
PortfolioReport.Source AS s ON g.SourceId = s.SourceId INNER JOIN
PortfolioReport.Campaign AS c ON s.CampaignId = c.CampaignId
WHERE (c.CampaignDesc = '1988 Christmas - I') AND (g.LineItemAmount > 0) AND (g.DateDeposited >=
(SELECT (MIN(c.DateStart) AS DateStart
FROM PortfolioReport.Source AS s INNER JOIN
PortfolioReport.Campaign AS c ON c.CampaignId = s.CampaignId
WHERE (c.CampaignDesc = '1988 Christmas - I') AND (c.DateStart IS NOT NULL))))) AND
(PortfolioReport.Gift.DateDeposited <=
(SELECT MIN(g.DateDeposited) +
(SELECT DATEDIFF(day,
(SELECT MIN(G.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS G INNER JOIN
PortfolioReport.Source AS S ON G.SourceId = S.SourceId INNER JOIN
PortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignId
WHERE (G.LineItemAmount > 0) AND (C.CampaignDesc = '1988 Christmas - I') AND (G.DateDeposited >= (SELECT MIN(C.DateStart) AS DateStart
FROM PortfolioReport.Source AS s INNER JOIN
PortfolioReport.Campaign AS c ON C.CampaignId = s.CampaignId
WHERE (C.CampaignDesc = '1988 Christmas - I') AND (C.DateStart IS NOT NULL)))),
(SELECT MAX(G.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS G INNER JOIN
PortfolioReport.Source AS S ON G.SourceId = S.SourceId INNER JOIN
PortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignId
WHERE (G.LineItemAmount > 0) AND (C.CampaignDesc = '1988 Christmas - I'))) AS 'LFY Last Gift Date')
FROM PortfolioReport.Gift g INNER JOIN
PortfolioReport.Source s ON g.sourceid = s.sourceid INNER JOIN
PortfolioReport.Campaign c ON s.campaignid = c.campaignid
WHERE c.CampaignDesc = '1988 Christmas - I' AND g.LineItemAmount > 0 AND g.DateDeposited >=
(SELECT min(c.DateStart)
FROM PortfolioReport.Source s INNER JOIN
PortfolioReport.Campaign c ON c.Campaignid = S.Campaignid
WHERE c.CampaignDesc = @Comparison_Campaign_Desc AND c.DateStart IS NOT NULL))
GROUP BY PortfolioReport.Source.CampaignId) AS GIFTS ON Campaign_One.CampaignId = GIFTS.CampaignId

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-23 : 13:05:51
Are you running SQL Server 2005 too?
If that's the case, you can remove several derived tables with GROUP BY,
and replace them with ROW_NUMBER() to get last row of something.

See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=86010



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

swenri
Yak Posting Veteran

72 Posts

Posted - 2007-08-23 : 13:38:37
I've used ROW_NUMBER() yet that doesn't work. Yes, I'm using Sql Server 2005 and VS 2005 to build reports. Any solution please ????? Thanks
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-23 : 13:45:13
This gotta be one of the worst written queries I have ever seen!
SELECT DISTINCT	'0' AS Cpgn_Tot_MailedQty,
ISNULL(GIFTS.Final_No_Of_Gifts, 0) AS Final_No_of_Gifts,
ISNULL(GIFTS.Final_Total_Income, 0) AS Final_Total_Income,
ISNULL(Campaign_One.Final_Mailing_Cost, 0) AS Final_Mailing_Cost,
ISNULL(Campaign_One.Final_Fulfillment_Cost, 0) AS Final_Fulfillment_Cost,
ISNULL(Campaign_One.CampaignId, 0) AS CampaignID
FROM (
SELECT CampaignId,
SUM(ISNULL(MailActualCost, 0)) AS Final_Mailing_Cost,
SUM(ISNULL(FulfillmentActualCost, 0)) AS Final_Fulfillment_Cost
FROM PortfolioReport.Campaign
WHERE CampaignDesc = '1988 Christmas - I'
GROUP BY CampaignId
) AS Campaign_One
LEFT JOIN (
SELECT PortfolioReport.Source.CampaignId,
COUNT(*) AS Final_No_Of_Gifts,
SUM(ISNULL(PortfolioReport.Gift.LineItemAmount, 0)) AS Final_Total_Income
FROM PortfolioReport.Gift
INNER JOIN PortfolioReport.Source ON PortfolioReport.Gift.SourceId = PortfolioReport.Source.SourceId
INNER JOIN PortfolioReport.Campaign AS Campaign_1 ON PortfolioReport.Source.CampaignId = Campaign_1.CampaignId
WHERE PortfolioReport.Source.SourceId <> 0
AND Campaign_1.CampaignDesc = '1988 Christmas - I'
AND PortfolioReport.Gift.DateDeposited >= (
SELECT MIN(g.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS g
INNER JOIN PortfolioReport.Source AS s ON g.SourceId = s.SourceId
INNER JOIN PortfolioReport.Campaign AS c ON s.CampaignId = c.CampaignId
WHERE c.CampaignDesc = '1988 Christmas - I'
AND g.LineItemAmount > 0
AND g.DateDeposited >= (
SELECT MIN(c.DateStart) AS DateStart
FROM PortfolioReport.Source AS s
INNER JOIN PortfolioReport.Campaign AS c ON c.CampaignId = s.CampaignId
WHERE c.CampaignDesc = '1988 Christmas - I'
AND c.DateStart IS NOT NULL
)
)
AND PortfolioReport.Gift.DateDeposited <= (
SELECT MIN(g.DateDeposited) + (
SELECT DATEDIFF(day, (
SELECT MIN(G.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS G
INNER JOIN PortfolioReport.Source AS S ON G.SourceId = S.SourceId
INNER JOIN PortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignId
WHERE G.LineItemAmount > 0
AND C.CampaignDesc = '1988 Christmas - I'
AND G.DateDeposited >= (
SELECT MIN(C.DateStart) AS DateStart
FROM PortfolioReport.Source AS s
INNER JOIN PortfolioReport.Campaign AS c ON C.CampaignId = s.CampaignId
WHERE C.CampaignDesc = '1988 Christmas - I'
AND C.DateStart IS NOT NULL
)
), (
SELECT MAX(G.DateDeposited) AS DateDeposited
FROM PortfolioReport.Gift AS G
INNER JOIN PortfolioReport.Source AS S ON G.SourceId = S.SourceId
INNER JOIN PortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignId
WHERE G.LineItemAmount > 0
AND C.CampaignDesc = '1988 Christmas - I'
)
) AS 'LFY Last Gift Date'
)
FROM PortfolioReport.Gift g
INNER JOIN PortfolioReport.Source s ON g.sourceid = s.sourceid
INNER JOIN PortfolioReport.Campaign c ON s.campaignid = c.campaignid
WHERE c.CampaignDesc = '1988 Christmas - I'
AND g.LineItemAmount > 0
AND g.DateDeposited >= (
SELECT min(c.DateStart)
FROM PortfolioReport.Source s
INNER JOIN PortfolioReport.Campaign c ON c.Campaignid = S.Campaignid
WHERE c.CampaignDesc = @Comparison_Campaign_Desc
AND c.DateStart IS NOT NULL
)
GROUP BY PortfolioReport.Source.CampaignId
) AS GIFTS ON Campaign_One.CampaignId = GIFTS.CampaignId


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2007-08-23 : 13:50:46
CROSS POST

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-08-23 : 14:15:12
Try this one
SELECT		'0' AS Cpgn_Tot_MailedQty,
g.Final_No_Of_Gifts AS Final_No_of_Gifts,
g.Final_Total_Income AS Final_Total_Income,
ISNULL(c1.Final_Mailing_Cost, 0) AS Final_Mailing_Cost,
ISNULL(c1.Final_Fulfillment_Cost, 0) AS Final_Fulfillment_Cost,
c1.CampaignId,
c1.CampaignDesc
FROM (
SELECT CampaignId,
CampaignDesc,
SUM(ISNULL(MailActualCost, 0)) AS Final_Mailing_Cost,
SUM(ISNULL(FulfillmentActualCost, 0)) AS Final_Fulfillment_Cost
FROM PortfolioReport.Campaign
GROUP BY CampaignId,
CampaignDesc
) AS c1
LEFT JOIN (
SELECT c.CampaignId
MIN(CASE WHEN g.LineItemAmount > 0 THEN g.DateDeposited ELSE NULL END) AS MinDateDeposited,
MAX(CASE WHEN g.LineItemAmount > 0 THEN g.DateDeposited ELSE NULL END) AS MaxDateDeposited,
MIN(c.DateStart) AS DateStart,
SUM(CASE WHEN s.SourceId <> 0 THEN 1 ELSE 0 END) AS Final_No_Of_Gifts,
SUM(CASE WHEN s.SourceId <> 0 THEN ISNULL(g.LineItemAmount, 0) ELSE 0 END) AS Final_Total_Income
FROM PortfolioReport.Gift AS g
INNER JOIN PortfolioReport.Source AS s ON s.SourceId = g.SourceId
INNER JOIN PortfolioReport.Campaign AS c ON c.CampaignId = s.CampaignId
GROUP BY c.CampaignId
HAVING MIN(CASE WHEN g.LineItemAmount > 0 THEN g.DateDeposited ELSE NULL END) >= MIN(c.DateStart)
) AS g ON g.CampaignId = c1.CampaignId



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -