2

I use the below query, but it shows some repeated items. So I use the group function, but it does not work.

SELECT p.productId, p.productName, p.catId, p.subCatId, p.productType,
       p.modelNo, p.picUrl, p.color, p.theme, p.productPrice, p.discountedPrice,
       p.quantity, p.details, p.mainPageDisplay, p.productPageDisplay,
       s.subCatId AS Expr1,
       s.subCatName, s.catId AS Expr2,
       s.rank, s.subCatName AS Expr3
FROM (products p INNER JOIN
      subCategories s ON p.catId = s.catId)
WHERE (p.color = 'red') group By p.productName

This query is working fine, but when I add group by then it's not working.

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Amarpreet Kaur
  • 39
  • 1
  • 1
  • 6
  • 2
    use ORDER BY clause GROUP BY FOR FUNCITONS MIN MAX etc – realnumber3012 Apr 18 '13 at 09:56
  • group by is work with only aggregate function or column by which its grouuped by.. – Amit Singh Apr 18 '13 at 09:58
  • 6
    When you use `GROUP BY`, all columns need to be either in the `GROUP BY` or contained in an aggregate - so you need to tell us/SQL Server what you want to do with each column that isn't `productName` - should we take the `MAX()` or `MIN()` value for each of those columns? – Damien_The_Unbeliever Apr 18 '13 at 10:06
  • Can you give an example of repeated items, please? – Aleksandr Fedorenko Apr 18 '13 at 10:13
  • it's showing 3 time Product name like pendrive then next value 100 in 3 time,after that it will pick nxt item ,laptop – Amarpreet Kaur Apr 18 '13 at 10:21
  • 1
    So, as I said in my first comment - you need to tell us how to select an appropriate value for each column that *isn't* `productName`. You either need to define an aggregate, or, if you want to say "I want the first/last row's values", you need to tell us what ordering to use to define the first or last row. – Damien_The_Unbeliever Apr 19 '13 at 06:42
  • Have you tried something like this? http://stackoverflow.com/questions/38495465/how-to-group-by-multiple-columns-in-sql-server – AndreFeijo Jul 21 '16 at 05:48
  • As @Damien_The_Unbeliever has explained above. The engine can only display a single row for each group. You're selecting other rows (that aren't part of GROUP BY clause). For each [ProductName] there can be different values for other columns. Hence the query fails. Because those rows can't be determined to be displayed with your group name column. – Arpit Chinmay Aug 11 '21 at 20:30

4 Answers4

5

You don't need a GROUP BY for selecting distinct rows, you need DISTINCT:

SELECT DISTINCT p.productId, p.productName, p.catId, p.subCatId, p.productType,
       p.modelNo, p.picUrl, p.color, p.theme, p.productPrice, p.discountedPrice,
       p.quantity, p.details, p.mainPageDisplay, p.productPageDisplay,
       s.subCatId AS Expr1,
       s.subCatName, s.catId AS Expr2,
       s.rank, s.subCatName AS Expr3
FROM (products p INNER JOIN
      subCategories s ON p.catId = s.catId)
WHERE (p.color = 'red')
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
1

Your SELECT statement doesn't contain any aggregate functions, so a GROUP BY statement is not appropriate.

cms_mgr
  • 1,977
  • 2
  • 17
  • 31
0

You need to use all the columns in the SELECT clause in GROUP BY Clause or otherwise use DISTINCT keyword after SELECT Keyword.

Santhosh
  • 1,771
  • 1
  • 15
  • 25
0

SQL GROUP BY aggregates (consolidates and calculates) column values into a single record value. GROUP BY requires a list of table columns on which to run the calculations.

This link has an example to facilitate your understanding of the concept .

Community
  • 1
  • 1
misguided
  • 3,699
  • 21
  • 54
  • 96