def test_column_functions():
    "Tests various functions that operate on specified column."
    # Create an order table to test various functions on.
    order = Table(('O_Id', int), ('OrderDate', date),
                  ('OrderPrice', int), ('Customer', str))
    order.insert(1, date(2008, 11, 12), 1000, 'Hansen')
    order.insert(2, date(2008, 10, 23), 1600, 'Nilsen')
    order.insert(3, date(2008, 9, 2), 700, 'Hansen')
    order.insert(4, date(2008, 9, 3), 300, 'Hansen')
    order.insert(5, date(2008, 9, 30), 2000, 'Jensen')
    order.insert(6, date(2008, 10, 4), 100, 'Nilsen')
    # Test the "avg" function.
    order_average = order.avg('OrderPrice')
    print('OrderAverage =', order_average, '\n')
    order.where(ROW.OrderPrice > order_average).select('Customer').print()
    # Test the "count" function.
    print('CustomerNilsen =', order.where(
        ROW.Customer == 'Nilsen').count('Customer'))
    print('NumberOfOrders =', order.count())
    print('NumberOfCustomers =', order.select('Customer') \
          .distinct().count('Customer'))
    # Test the "first" function.
    print('FirstOrderPrice =', order.first('OrderPrice'))
    # Test the "last" function.
    print('LastOrderPrice =', order.last('OrderPrice'))
    # Test the "max_" function.
    print('LargestOrderPrice =', order.max_('OrderPrice'))
    # Test the "min_" function.
    print('SmallestOrderPrice =', order.min_('OrderPrice'))
    # Test the "sum_" function.
    print('OrderTotal =', order.sum_('OrderPrice'), '\n')
    # Test the "group_by" statement.
    result = Table(('Customer', str), ('OrderPrice', int))
    for table in order.group_by('Customer'):
        result.insert(table.first('Customer'), table.sum_('OrderPrice'))
    result.print()
    # Add some more orders to the table.
    order.insert(7, date(2008, 11, 12), 950, 'Hansen')
    order.insert(8, date(2008, 10, 23), 1900, 'Nilsen')
    order.insert(9, date(2008, 9, 2), 2850, 'Hansen')
    order.insert(10, date(2008, 9, 3), 3800, 'Hansen')
    order.insert(11, date(2008, 9, 30), 4750, 'Jensen')
    order.insert(12, date(2008, 10, 4), 5700, 'Nilsen')
    # Test ability to group by several columns.
    result.truncate().alter_add('OrderDate', date)
    for table in order.group_by('Customer', 'OrderDate'):
        result.insert(table.first('Customer'),
                      table.sum_('OrderPrice'),
                      table.first('OrderDate'))
    result.print()
Tests various functions that operate on specified column.
recipes/Python/577825_Rudimentary_Database_Engine/recipe-577825.py