473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Summary query in DLinq

DLinq objects:

class Item {
string ItemCode {get;set;} // primary key
}

class Stock { // primary key is (ItemCode,StockId)
string ItemCode {get;set;}
string Quantity { get; set; }
string StockId { get; set; }
}

class ItemsInStock {
string ItemCode {get;set;} // primary key
string SumQuantity { get; set; }
}

How to create IQueryable<ItemsInStockwhich returns item code and sum of
items in all stocks even when there is no items in stock.

In sql I can write

SELECT
Item.ItemCode,
COALESCE(sum(Stock.Quantity),0) as SumQuantity
FROM Item
LEFT JOIN Stock ON Item.ItemCode= Stock.ItemCode
GROUP BY 1
INTO CURSOR ItemsINStock

How to convert this query to IQuearyable<ItemsInStock?

Andrus.

Jul 5 '08 #1
4 2437
No; it executes 1 TSQL statement; I included this single statement (via
LINQ-to-SQL) in my reply. If DbLinq does 101 queries, then that is an
issue for DbLinq.

Marc
Jul 7 '08 #2
It is one SQL statement, but it MAY imply that the table implied in the
sub-query will be touch once for each and every row of the table of the main
clause.
There is a LOGICAL difference between:
--------------------
SELECT a.f1, SUM(b.f2)
FROM a INNER JOIN b ON a.f1=b.f1
-------------------

and

--------------------
SELECT a.f1 (SELECT SUM(b.f2)
FROM b
WHERE b.f1=a.f1)
FROM a
-------------------

mainly if and a.f1 and b.f1 have common dup. Example right here:
a.f1 a.f4
----------------
a hello
a world
b !!!

b.f1 b.f2
----------------
a 10
a 20
b 30
The query you produced (subquery making a sum) return

a 30
b 30
while the sum over the join returns:

a 60
b 30

Surprised? well, since the join produced:

a.f1 a.f4 b.f1 b.f2
a hello a 10
a hello a 20
a world a 10
a world a 20
b !!! b 30
so the sum is as announced.
So, having established that the two queries are not the same, they don't
have the same query plan. Your query, which is probably what is wanted, but
may have to "touch" the table B for EACH and EVERY row of table A.

But.

It may happen that the sum over the join is still ok, given that, somehow,
we are sure there is not common dup on both tables (say, f1 is a primary key
for table A, as example), then, the sum over the join is ok... and MAY be
optimized radically differently (meaning faster) than the sub-query with a
sum, such, as example, by running concurrently over the indexes.
So, to conclude, while your query works, it is not logically equivalent as a
query of a sum over a join. Since the two queries are not equivalent, MS SQL
Server would NOT make them equivalent, so, won't come with the same plan of
execution. (And yes, I lied, since MS SQL Server 2005 MAY translate your
sub-query as a join in some circumstances).

Hoping it may help, ... somehow

Vanderghast, Access MVP
"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
No; it executes 1 TSQL statement; I included this single statement (via
LINQ-to-SQL) in my reply. If DbLinq does 101 queries, then that is an
issue for DbLinq.

Marc

Jul 7 '08 #3
I missed the GROUP BY clause in the first query. Should be:
SELECT a.f1, SUM(b.f2)
FROM a INNER JOIN b ON a.f1=b.f1
GROUP BY a.f1
Jul 7 '08 #4
The second query will output

a 30
a 30
b 30
(was missing one row in the result).

Jul 7 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jane | last post by:
HI, I try to create summary table like following: create table summary (a int, b int, c int) (select a.aa, b.bb, b.cc from table_a a ,table_b b where a.key=b.key) data initially deferred...
9
by: Andrea | last post by:
Hi, I've read the former postings but was not able to solve my problem: I have a Summary Table (or MQT as you like it) and the query optimizer does not seem to use the summary table. I run...
19
by: Andrus | last post by:
I need to repeatedly execute same queries which returns single entity by id, like: Customer cust = (from c in db.Customers where c.CustomerID=="AIRBU" select c).SingleOrDefault(); DLinq...
9
by: Marc Gravell | last post by:
How to fix ? Write it the way that you know works... (i.e. the one you commented out), or write that parses the input string doing a Split on '.', and uses reflection to navigate the child...
11
by: Andrus | last post by:
I created dynamic extension methods for <= and < SQL comparison operators: public static IQueryable<TLessThanOrEqual<T>(this IQueryable<Tsource, string property, object value); public static...
1
by: Michel Walsh | last post by:
In the same spirit, but more LINQ related, you can also use ExecuteQuery: var query = dataContext.ExecuteQuery<className>( @"SELECT ... WHERE ... AND... OR... "); where className is...
3
by: Andrus | last post by:
For paged data access I need to convert result of DLinq query to something (list or array) which can be accessed by integer index. I use for this metod below. Is it best solution to use foreach...
1
by: randyse | last post by:
Hello all, This is my first post, please let me know if I've missed any relevant data or guidelines. I'm using Access 2003 for the database and Excel 2003 for the reporting to avoid having...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.