question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid SQL Generated for CTE with Count and Distinct

See original GitHub issue

When 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:closed
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Nov 2, 2018

Will check. I’ll fix this today. @MaceWindu, I think we can prepare bug-fix release after merging this PR.

0reactions
hahn-kevcommented, Nov 5, 2018

Testing out release 2.6.0-rc2852 and my issue is resolved when using a CTE. Thanks!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found