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

  • Home
  • SEARCH
  • 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 7615069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:33:51+00:00 2026-05-31T02:33:51+00:00

I’ve been banging my head on this one for a while now. I want

  • 0

I’ve been banging my head on this one for a while now. I want to achieve this:

SELECT [t2].[nArtKey], [t2].[value] AS [nQty]
FROM (
    SELECT SUM([t0].[nQty]) AS [value], [t1].[nArtKey]
        FROM [vdatStockTransactions] AS [t0]
        INNER JOIN [regArtSKU] AS [t1] ON [t0].[nSKU] = [t1].[nSKU]
        GROUP BY [t1].[nArtKey]
        ) AS [t2]
    INNER JOIN [regArticles] AS [t3] ON [t2].[nArtKey] = [t3].[nArtKey]
    INNER JOIN [regGroupConnector] AS [t4] ON [t2].[nArtKey] = [t4].[nArtKey]
WHERE [t2].[value] > @p0

What I have so far with LINQ gives me pretty much exactly what I want, exept for the quantity…

from trans in context.vdatStockTransactions
join sku in context.regArtSKUs on trans.nSKU equals sku.nSKU
group trans by new { sku.nArtKey } into grp
where grp.Sum(g => g.nQty) > 0
join art in context.regArticles on grp.Key.nArtKey equals art.nArtKey       
join ca in context.regGroupConnectors on grp.Key.nArtKey equals ca.nArtKey
select new
{
    nArtKey = grp.Key.nArtKey,
    //nQty = grp.Sum(g => g.nQty)
};

However, if I uncomment nQty I get this:

SELECT [t7].[nArtKey], [t7].[value] AS [nQty]
FROM (
    SELECT [t3].[nArtKey], (
        SELECT SUM([t5].[nQty])
        FROM [vdatStockTransactions] AS [t5]
        INNER JOIN [regArtSKU] AS [t6] ON [t5].[nSKU] = [t6].[nSKU]
        WHERE [t2].[nArtKey] = [t6].[nArtKey]
        ) AS [value], [t2].[value] AS [value2]
    FROM (
        SELECT SUM([t0].[nQty]) AS [value], [t1].[nArtKey]
        FROM [vdatStockTransactions] AS [t0]
        INNER JOIN [regArtSKU] AS [t1] ON [t0].[nSKU] = [t1].[nSKU]
        GROUP BY [t1].[nArtKey]
        ) AS [t2]
    INNER JOIN [regArticles] AS [t3] ON [t2].[nArtKey] = [t3].[nArtKey]
    INNER JOIN [regGroupConnector] AS [t4] ON [t2].[nArtKey] = [t4].[nArtKey]
    ) AS [t7]
WHERE [t7].[value2] > @p0

Why does it create that extra subquery and is there any way that I could avoid it? It makes quite a big difference in performance so I really would like to figure this out. All I want is to use the quantity ([t2].[value]) and get it into the SELECT statement.

That extra subquery only exists when there are more than one join after group by so if I remove either then the query generates the expected SQL.

NOTE: I stripped down the original query to just keep the parts that are generating this behavior so it would be easier to follow.

  • 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-31T02:33:53+00:00Added an answer on May 31, 2026 at 2:33 am

    I haven’t tested this, but this might do the trick:

    var quantities =
        from trans in context.vdatStockTransactions
        group trans by trans.SKU.nArtKey into grp
        select new
        {
            nQty = grp.Sum(g => g.nQty), 
            nArtKey = grp.Key 
        };
    
    var results =
        from quantity in quantities
        join art in context.regArticles
            on quantity.nArtKey equals art.nArtKey       
        join ca in context.regGroupConnectors 
            on quantity.nArtKey equals ca.nArtKey
        where quantity.nQty > 0
        select quantity;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.