tags:

views:

33

answers:

1

Hi,

I'm using subsonic 2.2 with .net 2.0 and I'm having an error when I included "Group By".

Originally I had this code, and it worked perfectly:

    SqlQuery etResourceTitle = new Select(ResTitleOngoing.Columns.ResourceTitleID,
                                          ResTitleOngoing.Columns.ResourceTitle, 
                                          VenVendor.Columns.Title,
                                          ResTitleOngoingStatus.Columns.Status).From(Tables.ResTitleOngoing);

    etResourceTitle.InnerJoin(ResTitleOngoingStatus.ResourceTitleIDColumn, ResTitleOngoing.ResourceTitleIDColumn);
    etResourceTitle.LeftOuterJoin(VenVendor.VendorIDColumn, ResTitleOngoing.VendorIDColumn);

    etResourceTitle.OrderAsc(ResTitleOngoing.Columns.ResourceTitle);

    etResourceTitle.ExecuteDataSet();

Then I added "Group By", so this is my code:

    SqlQuery etResourceTitle = new Select(Aggregate.GroupBy(ResTitleOngoing.Columns.ResourceTitleID),
                                          Aggregate.GroupBy(ResTitleOngoing.Columns.ResourceTitle), 
                                          Aggregate.GroupBy(VenVendor.Columns.Title),
                                          Aggregate.GroupBy(ResTitleOngoingStatus.Columns.Status)).From(Tables.ResTitleOngoing);

    etResourceTitle.InnerJoin(ResTitleOngoingStatus.ResourceTitleIDColumn, ResTitleOngoing.ResourceTitleIDColumn);
    etResourceTitle.LeftOuterJoin(VenVendor.VendorIDColumn, ResTitleOngoing.VendorIDColumn);

    etResourceTitle.OrderAsc(ResTitleOngoing.Columns.ResourceTitle);

    etResourceTitle.ExecuteDataSet();

I got the error: "Ambiguous column name 'ResourceTitleID'."

Executing etResourceTitle.BuildSqlStatement(); i got:

SELECT ResourceTitleID AS 'GroupByOfResourceTitleID', ResourceTitle AS 'GroupByOfResourceTitle', Title AS 'GroupByOfTitle', Status AS 'GroupByOfStatus' FROM [dbo].[RES_TitleOngoing] INNER JOIN [dbo].[RES_TitleOngoingStatus] ON [dbo].[RES_TitleOngoing].[ResourceTitleID] = [dbo].[RES_TitleOngoingStatus].[ResourceTitleID] LEFT OUTER JOIN [dbo].[VEN_Vendor] ON [dbo].[RES_TitleOngoing].[VendorID] = [dbo].[VEN_Vendor].[VendorID] GROUP BY ResourceTitleID, ResourceTitle, Title, Status ORDER BY ResourceTitle ASC

So, someone can help me with this error.

Thanks Alan

+1  A: 

Both your tables RES_TitleOngoing and RES_TitleOngoingStatus contain a column named ResourceTitleID but you specified just ResourceTitleID in your group by part so the sql server does not know which ResourceTitleID you want to group.

Note:

ResTitleOngoing.Columns.ResourceTitleID

just returns a string "ResourceTitleID"

You should try one of these instead:

Aggregate.GroupBy(ResTitleOngoing.ResourceTitleIDColumn)
Aggregate.GroupBy(ResTitleOngoing.Schema.QualifiedName + "."
                  + ResTitleOngoing.Columns.ResourceTitleID)
SchlaWiener
Thanks SchlaWiener.This resolved my problem. I used Aggregate.GroupBy(ResTitleOngoing.ResourceTitleIDColumn) Alan
Alan Ruiz