Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:21:57+00:00 2026-05-13T23:21:57+00:00

I want to create the following T-SQL statement: SELECT SUM (sa.Amount) as ‘SumAmount’, SUM(sa.Cost)

  • 0

I want to create the following T-SQL statement:

SELECT  SUM (sa.Amount) as 'SumAmount',
        SUM(sa.Cost) as 'SumCost', 
        gg.[Description] as 'Goodsgroup', Month(sa.[Date]) as 'Month' 
FROM SalesmanArticle sa
INNER JOIN Article a
    ON a.ArticleId = sa.ArticleId
INNER JOIN GoodsGroup gg
    ON gg.GoodsGroupId = a.GoodsGroupId
GROUP BY gg.[Description], Month(sa.[Date])
ORDER BY 'Month', 'Goodsgroup'

Is this possible with NHibernates ICriteria?

How can I use the Month-T-SQL-Function?

Do I have to join manually or does the ICriteria API knows that when I use the propetyName ‘SalesmanArticle.Article.Goodsgroup.Description’ it has to join the Article and the Goodsgroup?

EDIT:

For now I have written this code here:

// typesafe properties
string article = typeof(Article).Name;
string goodsGroup = typeof(GoodsGroup).Name;
string salesmanArticle = typeof(SalesmanArticle).Name;

string amount = Reflector.GetPropertyName<SalesmanArticle>(x => x.Amount);
string cost = Reflector.GetPropertyName<SalesmanArticle>(x => x.Cost);
string description = string.Format("{0}.{1}",
    goodsGroup, Reflector.GetPropertyName<SalesmanArticle>(x => x.Article.GoodsGroup.Description));
string date = Reflector.GetPropertyName<SalesmanArticle>(x => x.Date);

string formatedDate = string.Format("MONTH([{0}])", date);

return GetSession()
    // FROM
    .CreateCriteria(typeof(SalesmanArticle), salesmanArticle)
    // JOIN
        .CreateCriteria(article, article, JoinType.InnerJoin)
        .CreateCriteria(goodsGroup, goodsGroup, JoinType.InnerJoin)
    // SELECT
        .SetProjection(Projections.ProjectionList()
                           .Add(Projections.Sum(amount))
                           .Add(Projections.Sum(cost))
    // GROUP BY
                           .Add(Projections.GroupProperty(description))
                           .Add(Projections.SqlGroupProjection(formatedDate, formatedDate, new[]{"MyDate"} , new[] { NHibernateUtil.Int32 })))
        .List();

But an AdoException is thrown:

could not execute query [ SELECT
sum(this_.Amount) as y0_,
sum(this_.Cost) as y1_,
goodsgroup2_.Description as y2_,
MONTH([Date]) FROM [SalesmanArticle]
this_ inner join [Article] article1_
on this_.ArticleId=article1_.ArticleId
inner join [GoodsGroup] goodsgroup2_
on
article1_.GoodsGroupId=goodsgroup2_.GoodsGroupId
GROUP BY goodsgroup2_.Description,
MONTH([Date]) ]

[SQL: SELECT
sum(this_.Amount) as y0_,
sum(this_.Cost) as y1_,
goodsgroup2_.Description as y2_,
MONTH([Date]) FROM [SalesmanArticle]
this_ inner join [Article] article1_
on this_.ArticleId=article1_.ArticleId
inner join [GoodsGroup] goodsgroup2_
on
article1_.GoodsGroupId=goodsgroup2_.GoodsGroupId
GROUP BY goodsgroup2_.Description,
MONTH([Date])]

The strange thing is that NHibernate tries to create 2 queries?!

AND BOTH of them are correct!

Instead of the codeline

.Add(Projections.SqlGroupProjection(formatedDate, formatedDate, new[]{"MyDate"} , new[] { NHibernateUtil.Int32 })))

I used

.Add(Projections.SqlFunction("MONTH", NHibernateUtil.Int32, Projections.GroupProperty(date))))

The problem with the SqlFunction is that it creates a GROUP BY sa.Date instead of MONTH(sa.Date). But this method worked syntactically correct.

So I switched to the SqlGroupProjection method.

But anyway it does not work.

Can anybody help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:21:58+00:00Added an answer on May 13, 2026 at 11:21 pm

    I solved it. Here is the correct code:

    public class SalesmanArticleRepository : Repository<SalesmanArticle>, ISalesmanArticleRepository
    {
        public IList GetAllAll()
        {
            // typesafe properties
            string article = typeof(Article).Name;
            string goodsGroup = typeof(GoodsGroup).Name;
            string salesmanArticle = typeof(SalesmanArticle).Name;
    
            string amount = Reflector.GetPropertyName<SalesmanArticle>(x => x.Amount);
            string cost = Reflector.GetPropertyName<SalesmanArticle>(x => x.Cost);
            string description = string.Format("{0}.{1}",
                goodsGroup, Reflector.GetPropertyName<SalesmanArticle>(x => x.Article.GoodsGroup.Description));
            string date = Reflector.GetPropertyName<SalesmanArticle>(x => x.Date);
    
            string formatedDateSql = string.Format("month({{alias}}.[{0}]) as mydate", date);
            string formatedDateGroupBy = string.Format("month({{alias}}.[{0}])", date);
    
            return GetSession()
                // FROM
                .CreateCriteria(typeof(SalesmanArticle), salesmanArticle)
                // JOIN
                    .CreateCriteria(article, article, JoinType.InnerJoin)
                    .CreateCriteria(goodsGroup, goodsGroup, JoinType.InnerJoin)
                // SELECT
                    .SetProjection(Projections.ProjectionList()
                                       .Add(Projections.Sum(amount))
                                       .Add(Projections.Sum(cost))
                // GROUP BY
                                       .Add(Projections.GroupProperty(description))
                                       .Add(Projections.SqlGroupProjection(formatedDateSql, formatedDateGroupBy, new[] { "mydate" }, new[] { NHibernateUtil.Int32 })))
                    .List();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create the following element: <exercises xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mySchema.xsd"> If I use something
I want to create an allocator which provides memory with the following attributes: cannot
I want to create an alias for a class name. The following syntax would
I want create a drop shadow around the canvas component in flex. Technically speaking
I want to create a Java application bundle for Mac without using Mac. According
I want to create a client side mail creator web page. I know the
I want to create a function that performs a function passed by parameter on
I want to create a simple http proxy server that does some very basic
I want to create a draggable and resizable window in JavaScript for cross browser
I want to create my Rails application with MySQL, because I like it so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.