David Croushore

A Man in Progress

Day 1: Using Group By and Order By in SQL

To satisfy my 30 day skill development mission for day 1, I learned how to use the Group By and Order By clauses in SQL, specifically, when calling PROC SQL in SAS. 

Order By was fairly straightforward and probably shouldn’t count, but Group By presented a unique challenge that did take some effort to figure out.

My problem with Group By

The first time I tried playing with the Group By clause, SAS converted it to an Order By because I didn’t include a summary function.  That was easy enough to figure out and I just changed my select statement from select A.Amount to select sum(A.Amount), but that didn’t quite work since I still needed to name the new variable.

The proper command “Select sum(A.Amount) as Total_Amount fixed that.

Finally, I got the group by to summarize the amount field as I was hoping, but it printed a line to my new table for every input line, which wasn’t at all what I was going for, since I was expecting more of a “proc summary” kind of output.  I figured out that by adding “Unique” to my select clause I was able to eliminate the duplicates.

So, not moving mountains, but this is certainly an essential skill that I’ll need once I start querying the Atmo data to examine our user statistics.

1 day down, 29 to go

  1. 30daysatatime posted this
Comments
blog comments powered by Disqus