Group by Columns in T-SQL

All,

I have some requirement in DW where certain sk's are updated in separate update statements based on the PK of the staging table. 

How can I do a group by (OR) populate the same value across all records.

Some one please shed some light on this query.

I am attaching the screen shot and the data looks like in Table-1 but I would like to make the result set look like Table-2. 

Heads Up: The staging table have 92 million records.

February 15th, 2015 9:54pm

Select Day_SK, Max(Class_SK) As Class_SK, Max(School_SK) As School_SK, Student_SK
From Table1
Group By Day_SK, Student_SK

Tom


Free Windows Admin Tool Kit Click here and download it now
February 15th, 2015 10:53pm

Try below code,

Select	Day_SK , Max(Class_SK) Class_SK, Max(Grade_SK) Grade_SK, Max(School_SK) School_SK, Max(Student_SK) Student_SK 
From	Table-1
Group	By Day_SK

February 15th, 2015 11:09pm


Hi ,

You can also try with distinct ;
Select distinct Day_SK, Class_SK, School_SK, Student_SK
From Table1

Thanks
Free Windows Admin Tool Kit Click here and download it now
February 16th, 2015 1:07am

Hi,

Group by example from the first answer can be used. I just want to give you some explanation how to use the group by.

With a group by it is important to group the rows that give you a distinct output. 

Therefore, use Group by Day_sk, Student_SK. This way al the same days wil be grouped of the particular student number. 

Regards

Reshma

February 16th, 2015 1:37am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics