I'm learning all sorts of fun stuff with this catastrophic customer I'm working with. Today, I learned how to SQL an AS400 table, and get only one occurrence of a certain field per record set. For example, I needed to find out how many different values a certain field was possible of having depending on how other fields were set. Here's the final product:
select distinct field1 from file1 where
(field1,field2,field3) in
(
select field1,field2,field3
from file1 where field2='AAA' and field1 <> 'BBB'
group by
field1,field2,field3
In the above statement, the distinct keyword does the magic. I'm joining the file1 with itself, where field2 is equal to AAA and field1 is equal to BBB. If field 1 is set to AAA 300 times, it will be listed in the output only once. It will also list each other value that is set to in the table one time, so that you can easily see how many different values can occur in the field in the table. In my case, the file had over 700 million records, so it was a fast way to get the information I needed. I hope this information was useful!
No comments:
Post a Comment