Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

Cant retrieve all columns in a group by statement

$
0
0

Hello,

i have this schema :

CREATE DATABASE ANDEB

USE ANDEB
CREATE TABLE TDocHeader
(
  CustName VARCHAR(50) NOT NULL,
  DocNum INT NOT NULL,
  col1 varchar(50),
  col2 varchar(50),
  col3 varchar(50),
  col4 varchar(50),
  col5 varchar(50),
  col6 varchar(50)
  );

i have insert this values :

  INSERT INTO TDocHeader VALUES
('cust1',1,'34','55','hj','col4','col5','col6'),
('cust1',2,'col1','89','col3','col4','col5','col6'),
('cust1',3,'col1','col2','col3','col4','col5','col6'),
('cust2',5,'col1','asd','col3','col4','col5','col6'),
('cust3',7,'col1','ghhj','hjk','dgf','09','56t'),
('cust3',9,'col1','col2','col3','col4','fgfg','col6'),
('cust4',11,'col1','col2','col3','col4','col5','col6'),
('cust4',12,'col1','col2','col3','col4','col5','col6');


i would group by for last docnum for Customer

select CustName as CustomerName,MAX(DocNum) from TDocHeader group by CustName


this works but i would retrieve all colums from table in the group by . 

i have try 

Select CustName,DocNum,col1,col2,col3,col4,col5,col6
FROM  TDocHeader T
INNER JOIN
(select CustName as CustomerName,MAX(DocNum) from TDocHeader group by CustName) TableGroupBy
ON T.CustName=TableGroupBy.CustomerName

but dont works , and also tried to insert all columns in select e in group by :

select CustName,MAX(DocNum),col1,col2,col3,col4,col5,col6 from TDocHeader group by

CustName,col1,col2,col3,col4,col5,col6


How i can have a group by for last docnum for Customer and all columns?

Many Thanks :)


www.Photoballot.net



Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>