SQL Server query for all columns with group by and having

I'm wondering is there a way to query all columns with group by and having in SQL Server? For example, I have 6 columns, a, b,…,f, and this is something I want to get:

Select *
From table
Group by table.b, table.c
Having max(table.d)=table.d

This works in sybase, since I'm trying to migrate stuff from sybase to SQL Server, I'm not sure what I can do in new environment. Thanks.


Why do you want to group by every column when you don't use any aggragate-functions in your select? Just use the following code to get all columns of the table:

select * from table

Group by only gets used when you have aggragete-functions (e.g. max(), avg(), count(), ...) in your select.

Having limits the aggrageted columns and where the normal columns of the table.

How to select multiple columns but only group by one?, In SQL Server you can only select columns that are part of the GROUP BY clause , or aggregate functions on any of the other columns. I've blogged about this in� HAVING Clause utilized in SQL as a conditional Clause with GROUP BY Clause. This conditional clause returns rows where aggregate function results matched with given conditions only. It added in the SQL because WHERE Clause cannot be combined with aggregate results, so it has a different purpose.


You can use MIN, MAX, AVG, and COUNT functions with the OVER clause to provide aggregated values for each column (to imitate the group by clause for each column) and Common table expression CTE to filter out the results (to imitate the having clause) as:

;With CTE as
(
SELECT  
         MIN(a) OVER (PARTITION BY a) AS MinCol_a  
       , MAX(b) OVER (PARTITION BY b) AS MaxCol_b 
       , AVG(c) OVER (PARTITION BY c) AS AvgCol_c  
       , COUNT(e) OVER (PARTITION BY d) AS Counte_PerCol_d  
FROM Tbl_Test   
)
select MinCol_a,MaxCol_b ,AvgCol_c,Counte_PerCol_d  
from CTE 
Join --here you can join the table Test results with other tables 
where --any filter condition similar to Having clause

GROUP BY and HAVING Clause in SQL, In this tutorial, you will learn about the GROUP BY and HAVING Clause The GROUP BY Clause is utilized in SQL with the SELECT statement to organize count(), and sum() to combine into single or multiple columns. HAVING clause: SQL uses the having clause to filter groups in the result set. ORDER BY clause: Use the ORDER BY clause to order the result set. The GROUP BY clause does not order the result set. NULL values: If a grouping column contains NULL values, all NULL values are considered equal and they are collected into a single group.


If what you want is to get the rows with maximum d for each combination of b and c then use NOT EXISTS:

select t.* from tablename t
where not exists (
  select 1 from tablename
  where b = t.b and c = t.c and d > t.d 
) 

or with rank() window function:

select t.a, t.b, t.c, t.d, t.e, t.f
from (
  select *, 
    rank() over (partition by b, c order by d desc) rn
  from tablename
) t
where t.rn = 1

SQL Server GROUP BY, The GROUP BY clause allows you to arrange the rows of a query in groups. FROM sales.orders WHERE customer_id IN (1, 2) ORDER BY customer_id; want to refer to any column or expression that is not listed in the GROUP BY clause,� GROUP BY and HAVING Clauses – SELECT Statement – SQL Server 2012 The GROUP BY clause defines one or more columns as a group such that all rows within any group have the same values for those columns. Example 6.22 shows the simple use of the GROUP BY clause in a SELECT statement. Example 6.22


Without using having you can get the result which you want. Try below

Select table.b, table.c, max(table.d)
From table
Group by table.b, table.c

How to Group and Summarize your Results (simple , The SQL GROUP BY Clause is used to output a row across specified column This makes it possible to solve problems such as select all orders that have more � Introduction to SQL Server HAVING clause The HAVING clause is often used with the GROUP BY clause to filter groups based on a specified list of conditions. The following illustrates the HAVING clause syntax: SELECT select_list FROM table_name GROUP BY group_list HAVING conditions;


Summarize Your SQL Results with the GROUP BY clause, The SQL GROUP BY Clause is used to output a row across specified column This makes it possible to solve problems such as select all orders that have more � If the query also contains a GROUP BY clause, the clause first arranges the output rows into groups. The ORDER BY clause then sorts the rows within each group. If you have no GROUP BY clause, then the statement considers the entire table as a group, and the ORDER BY clause sorts all its rows according to the column (or columns) that the ORDER BY clause specifies.


GROUP BY (Transact-SQL), GROUP BY ALL: Is not supported in queries that access remote tables if there is also a WHERE clause in the query. Will fail on columns that have� query GROUP BY and HAVING multiple columns on SELECT. – Learn more on the SQLServerCentral forums. Microsoft Certified Master: SQL Server 2008 Author - SQL Server T-SQL Recipes.


SQL GROUP BY: How To Apply It Effectively, It is not mandatory to include an aggregate function in the SELECT clause. However, to filter groups, you use the HAVING clause. If a grouping column contains NULL values, all NULL values are summarized into a single group because� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.