#NEWLINE# def handle( self , * args , **options ) : #NEWLINE# #INDENT# self . cursor = connection . cursor ( ) #NEWLINE# sql = ( "\n        SELECT\n            title,\n            first_name,\n            last_name,\n            suffix,\n            occupation,\n            employer,\n            address1,\n            address2,\n            city,\n            state,\n            zipcode,\n            committee_id,\n            COUNT(*)\n        FROM (\n            SELECT\n                ctrib_namt as title,\n                ctrib_namf as first_name,\n                ctrib_naml as last_name,\n                ctrib_nams as suffix,\n                ctrib_occ as occupation,\n                ctrib_emp as employer,\n                ctrib_adr1 as address1,\n                ctrib_adr2 as address2,\n                ctrib_city as city,\n                ctrib_st as state,\n                ctrib_zip4 as zipcode,\n                cmte_id as committee_id\n            FROM %(rcpt)s\n\n            UNION ALL\n\n            SELECT\n                lndr_namt as title,\n                lndr_namf as first_name,\n                lndr_naml as last_name,\n                lndr_nams as suffix,\n                loan_occ as occupation,\n                loan_emp as employer,\n                loan_adr1 as address1,\n                loan_adr2 as address2,\n                loan_city as city,\n                loan_st as state,\n                loan_zip4 as zipcode,\n                cmte_id as committee_id\n            FROM %(loan)s\n\n            UNION ALL\n\n            SELECT\n                enty_namt as title,\n                enty_namf as first_name,\n                enty_naml as last_name,\n                enty_nams as suffix,\n                ctrib_occ as occupation,\n                ctrib_emp as employer,\n                '' as address1,\n                '' as address2,\n                enty_city as city,\n                enty_st as state,\n                enty_zip4 as zipcode,\n                cmte_id as committee_id\n            FROM %(s497)s\n        ) as t\n        GROUP BY\n            title,\n            first_name,\n            last_name,\n            suffix,\n            occupation,\n            employer,\n            address1,\n            address2,\n            city,\n            state,\n            zipcode,\n            committee_id\n        ORDER BY\n            last_name,\n            first_name,\n            suffix,\n            title,\n            city,\n            state,\n            occupation,\n            employer\n        " % dict ( rcpt = models . RcptCd . _meta . db_table , loan = models . LoanCd . _meta . db_table , s497 = models . S497Cd . _meta . db_table ) ) #NEWLINE# self . cursor . execute ( sql ) #NEWLINE# writer = CSVKitWriter ( open ( './contributors.csv' , 'wb' ) ) #NEWLINE# writer . writerow ( [ 'title' , 'first_name' , 'last_name' , 'suffix' , 'occupation' , 'employer' , 'address1' , 'address2' , 'city' , 'state' , 'zipcode' , 'committee_id' , 'count' ] ) #NEWLINE# writer . writerows ( self . cursor . fetchall ( ) )