code_junkie

LINQ to SQL using GROUP BY and COUNT(DISTINCT)

Nov 13th, 2011
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. select answer_nbr, count(distinct user_nbr)
  2. from tpoll_answer
  3. where poll_nbr = 16
  4. group by answer_nbr
  5.  
  6. from a in tpoll_answer
  7. where a.poll_nbr = 16 select a.answer_nbr, a.user_nbr distinct
  8.  
  9. select distinct answer_nbr, distinct user_nbr
  10. from tpoll_answer
  11. where poll_nbr = 16
  12.  
  13. from answer in tpoll_answer where answer.poll_nbr = 16 _
  14. group by a_id = answer.answer_nbr into votes = count(answer.user_nbr)
  15.  
  16. SELECT [t1].[answer_nbr] AS [a_id], (
  17. SELECT COUNT(*)
  18. FROM (
  19. SELECT CONVERT(Bit,[t2].[user_nbr]) AS [value], [t2].[answer_nbr], [t2].[poll_nbr]
  20. FROM [TPOLL_ANSWER] AS [t2]
  21. ) AS [t3]
  22. WHERE ([t3].[value] = 1) AND ([t1].[answer_nbr] = [t3].[answer_nbr]) AND ([t3].[poll_nbr] = @p0)
  23. ) AS [votes]
  24. FROM (
  25. SELECT [t0].[answer_nbr]
  26. FROM [TPOLL_ANSWER] AS [t0]
  27. WHERE [t0].[poll_nbr] = @p0
  28. GROUP BY [t0].[answer_nbr]
  29. ) AS [t1]
  30. -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [16]
  31. -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
  32.  
  33. select new
  34. {
  35. Foo= grp.Key,
  36. Bar= grp.Select(x => x.SomeField).Distinct().Count()
  37. };
  38.  
  39. using(var ctx = new DataClasses1DataContext())
  40. {
  41. ctx.Log = Console.Out; // log TSQL to console
  42. var qry = from cust in ctx.Customers
  43. where cust.CustomerID != ""
  44. group cust by cust.Country
  45. into grp
  46. select new
  47. {
  48. Country = grp.Key,
  49. Count = grp.Select(x => x.City).Distinct().Count()
  50. };
  51.  
  52. foreach(var row in qry.OrderBy(x=>x.Country))
  53. {
  54. Console.WriteLine("{0}: {1}", row.Country, row.Count);
  55. }
  56. }
  57.  
  58. SELECT [t1].[Country], (
  59. SELECT COUNT(*)
  60. FROM (
  61. SELECT DISTINCT [t2].[City]
  62. FROM [dbo].[Customers] AS [t2]
  63. WHERE ((([t1].[Country] IS NULL) AND ([t2].[Country] IS NULL)) OR (([t1]
  64. .[Country] IS NOT NULL) AND ([t2].[Country] IS NOT NULL) AND ([t1].[Country] = [
  65. t2].[Country]))) AND ([t2].[CustomerID] <> @p0)
  66. ) AS [t3]
  67. ) AS [Count]
  68. FROM (
  69. SELECT [t0].[Country]
  70. FROM [dbo].[Customers] AS [t0]
  71. WHERE [t0].[CustomerID] <> @p0
  72. GROUP BY [t0].[Country]
  73. ) AS [t1]
  74. -- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
  75. -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1
  76.  
  77. const string sql = @"
  78. SELECT c.Country, COUNT(DISTINCT c.City) AS [Count]
  79. FROM Customers c
  80. WHERE c.CustomerID != ''
  81. GROUP BY c.Country
  82. ORDER BY c.Country";
  83. var qry2 = ctx.ExecuteQuery<QueryResult>(sql);
  84. foreach(var row in qry2)
  85. {
  86. Console.WriteLine("{0}: {1}", row.Country, row.Count);
  87. }
  88.  
  89. class QueryResult
  90. {
  91. public string Country { get; set; }
  92. public int Count { get; set; }
  93. }
  94.  
  95. var useranswercount = (from a in tpoll_answer
  96. where user_nbr != null && answer_nbr != null
  97. select user_nbr).Distinct().Count();
  98.  
  99. from cust in ctx.Customers
  100. where cust.CustomerID != ""
  101. group cust.City /*here*/ by cust.Country
  102. into grp
  103. select new
  104. {
  105. Country = grp.Key,
  106. Count = grp.Distinct().Count()
  107. };
Add Comment
Please, Sign In to add comment