Advertisement
naimul64

DbResultExportToCSV

Apr 30th, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.60 KB | None | 0 0
  1. def saveResultInCSV(db):
  2.     cursor = db.cursor()
  3.     cursor.execute("""
  4.        SELECT
  5.            th1.id tid1,
  6.            CONCAT(th1.name, ',', dt1.name) thana1,
  7.            sc1.school_name,
  8.            sm.first_school_code schoolCode1,
  9.            sm.second_school_code schoolCode2,
  10.            sc2.school_name,
  11.            th2.id tid2,
  12.            CONCAT(th2.name, ',', dt2.name) thana2,
  13.            same_mobile_no_count sameCnt,
  14.            (Case when th1.id = th2.id then "TRUE" else "FALSE" END) sameThana
  15.  
  16.        FROM
  17.            bugs.sameMobieNoCountBetweenDssheetAndDsheetSmaller sm
  18.                INNER JOIN
  19.            pesp_db_p23_live.school sc1 ON sm.first_school_code = sc1.school_code
  20.                INNER JOIN
  21.            pesp_db_p23_live.thana th1 ON sc1.thana_id = th1.id
  22.                INNER JOIN
  23.            pesp_db_p23_live.district dt1 ON th1.district_id = dt1.id
  24.                INNER JOIN
  25.            bugs.schoolRowCountDsSheet_intermediate itd1 ON itd1.school_code = sm.first_school_code
  26.                LEFT JOIN
  27.            bugs.mongoSchoolRowCount mng1 ON sm.first_school_code = mng1.school_code
  28.                LEFT JOIN
  29.            bugs.schoolRowCountReceiveddata_intermediate rdi1 ON sm.first_school_code = rdi1.school_code
  30.                INNER JOIN
  31.            pesp_db_p23_live.school sc2 ON sc2.school_code = sm.second_school_code
  32.                INNER JOIN
  33.            pesp_db_p23_live.thana th2 ON sc2.thana_id = th2.id
  34.                INNER JOIN
  35.            pesp_db_p23_live.district dt2 ON th2.district_id = dt2.id
  36.                INNER JOIN
  37.            bugs.schoolRowCountDsSheet_intermediate itd2 ON itd2.school_code = sm.second_school_code
  38.                LEFT JOIN
  39.            bugs.mongoSchoolRowCount mng2 ON sm.second_school_code = mng2.school_code
  40.                LEFT JOIN
  41.            bugs.schoolRowCountReceiveddata_intermediate rdi2 ON sm.second_school_code = rdi2.school_code
  42.        WHERE
  43.            1 AND sc1.thana_id <= 999
  44.                AND sc2.thana_id <= 999
  45.                AND (100 * same_mobile_no_count / itd1.distinctMobCount >= 20
  46.                OR 100 * same_mobile_no_count / itd2.distinctMobCount >=20)
  47.                And same_mobile_no_count >=15
  48.    """)
  49.     rows = cursor.fetchall()
  50.     description = cursor.description
  51.     header = ()
  52.     for desc in description:
  53.         header= header+(desc[0],)
  54.     cursor.close()
  55.     now = datetime.datetime.now()
  56.     fp = open('duplicateList.csv', 'w')
  57.     myFile = csv.writer(fp)
  58.     myFile.writerow(header)
  59.     myFile.writerows(rows)
  60.     fp.close()
  61.  
  62.     print "\nExported to CSV."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement