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.
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 studioTried 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 CampaignIDFROM (SELECT CampaignId, SUM(ISNULL(MailActualCost, 0)) AS Final_Mailing_Cost, SUM(ISNULL(FulfillmentActualCost, 0)) AS Final_Fulfillment_CostFROM PortfolioReport.CampaignWHERE (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_IncomeFROM PortfolioReport.Gift INNER JOINPortfolioReport.Source ON PortfolioReport.Gift.SourceId = PortfolioReport.Source.SourceId INNER JOINPortfolioReport.Campaign AS Campaign_1 ON PortfolioReport.Source.CampaignId = Campaign_1.CampaignIdWHERE (PortfolioReport.Source.SourceId <> 0) AND (Campaign_1.CampaignDesc = '1988 Christmas - I') AND (PortfolioReport.Gift.DateDeposited >=(SELECT MIN(g.DateDeposited) AS DateDepositedFROM PortfolioReport.Gift AS g INNER JOINPortfolioReport.Source AS s ON g.SourceId = s.SourceId INNER JOINPortfolioReport.Campaign AS c ON s.CampaignId = c.CampaignIdWHERE (c.CampaignDesc = '1988 Christmas - I') AND (g.LineItemAmount > 0) AND (g.DateDeposited >=(SELECT (MIN(c.DateStart) AS DateStartFROM PortfolioReport.Source AS s INNER JOINPortfolioReport.Campaign AS c ON c.CampaignId = s.CampaignIdWHERE (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 JOINPortfolioReport.Source AS S ON G.SourceId = S.SourceId INNER JOINPortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignIdWHERE (G.LineItemAmount > 0) AND (C.CampaignDesc = '1988 Christmas - I') AND (G.DateDeposited >= (SELECT MIN(C.DateStart) AS DateStartFROM PortfolioReport.Source AS s INNER JOINPortfolioReport.Campaign AS c ON C.CampaignId = s.CampaignIdWHERE (C.CampaignDesc = '1988 Christmas - I') AND (C.DateStart IS NOT NULL)))),(SELECT MAX(G.DateDeposited) AS DateDepositedFROM PortfolioReport.Gift AS G INNER JOINPortfolioReport.Source AS S ON G.SourceId = S.SourceId INNER JOINPortfolioReport.Campaign AS C ON S.CampaignId = C.CampaignIdWHERE (G.LineItemAmount > 0) AND (C.CampaignDesc = '1988 Christmas - I'))) AS 'LFY Last Gift Date')FROM PortfolioReport.Gift g INNER JOINPortfolioReport.Source s ON g.sourceid = s.sourceid INNER JOINPortfolioReport.Campaign c ON s.campaignid = c.campaignidWHERE c.CampaignDesc = '1988 Christmas - I' AND g.LineItemAmount > 0 AND g.DateDeposited >=(SELECT min(c.DateStart)FROM PortfolioReport.Source s INNER JOINPortfolioReport.Campaign c ON c.Campaignid = S.CampaignidWHERE 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" |
|
|
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 |
|
|
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 CampaignIDFROM ( 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_OneLEFT 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" |
|
|
X002548
Not Just a Number
15586 Posts |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-08-23 : 14:15:12
|
Try this oneSELECT '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.CampaignDescFROM ( 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 c1LEFT 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" |
|
|
|
|
|
|
|