Invalid SQL Generated for CTE with Count and Distinct
See original GitHub issueWhen running the following test (setup in CteTests
) against sql server and SQLite Invalid SQL is generated
[Test]
public void Test5([CteContextSource] string context)
{
using (var db = GetDataContext(context))
{
var cte1 = db.GetTable<Child>()
.Where(c => c.ParentID > 1)
.Select(child => new
{
child.ParentID,
child.ChildID
}).Distinct()
.AsCte();
var query = from p in db.Parent
join c in cte1 on p.ParentID equals c.ParentID
join c2 in cte1 on p.ParentID equals c2.ParentID
select p;
var cte1_ = db.GetTable<Child>().Where(c => c.ParentID > 1).Select(child => new
{
child.ParentID,
child.ChildID
}).Distinct();
var expected =
from p in db.Parent
join c in cte1_ on p.ParentID equals c.ParentID
join c2 in cte1_ on p.ParentID equals c2.ParentID
select p;
Assert.AreEqual(expected.Count(), query.Count());
}
}
the SQL generated for the CTE query is as follows
-- SqlServer.2012
WITH [CTE_1] ([ParentID])
AS
(
SELECT DISTINCT
[t1].[ParentID],
[t1].[ChildID]
FROM
[Child] [t1]
WHERE
[t1].[ParentID] > 1
)
SELECT
Count(*) as [cnt]
FROM
[Parent] [p]
INNER JOIN [CTE_1] [c1] ON [p].[ParentID] = [c1].[ParentID]
INNER JOIN [CTE_1] [c2] ON [p].[ParentID] = [c2].[ParentID]
As you can see, CTE_1 only declares ParentID
as CTE columns, but ChildID
is also selected because it has the DISTINCT
clause, it looks like the CTE isn’t respecting the changes caused by the DISTINCT
clause.
As a work around I can not use a CTE for now.
Exception message:
System.Data.SqlClient.SqlException : 'CTE_1' has more columns than were specified in the column list.
Steps to reproduce
Copy paste the snippet above into CteTests
and run.
Environment details
linq2db version: 2.5.0 Database Server: SqlServer
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Multiple COUNT(DISTINCT) from CTE - sql server
Just use conditional aggregation: SELECT Employee, COUNT(CASE WHEN OrderType = 953 THEN 1 END) AS [953_Order_Count], COUNT(CASE WHEN ...
Read more >New to CTE - How to do a count after making table unique
I have the below table in SQL. I'm trying to write a CTE to give me one line of data per day for...
Read more >Overview of the SQL Count Distinct Function
This article explores SQL Count Distinct operator for eliminating the duplicate rows in the result set. A developer needs to get data from...
Read more >WITH common_table_expression (Transact-SQL)
Specifies a column name in the common table expression. Duplicate names within a single CTE definition aren't allowed. The number of column ...
Read more >Efficient way to count distinct
I am trying to get a query to run which counts distinct values for two columns. I have a temp table named #Paid...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Will check. I’ll fix this today. @MaceWindu, I think we can prepare bug-fix release after merging this PR.
Testing out release 2.6.0-rc2852 and my issue is resolved when using a CTE. Thanks!