0

My python3 script is not generating the same result as MySQL. My query returns those rows whose value have changed over the week.

Python script:

query = "SELECT cw.opportunityid, cw.probability, pw.probability, cw.stage, pw.stage, cw.amount, pw.amount, " \
    "cw.closedate, pw.closedate " \
    "FROM opty_data cw " \
    "LEFT JOIN opty_data pw ON cw.opportunityid = pw.opportunityid " \
    "AND pw.Week = \"{0}\" " \
    "WHERE cw.Week = \"{1}\" " \
    "AND IF(pw.opportunityid IS NULL, TRUE, ((cw.probability <> pw.probability) OR (cw.stage <> pw.stage) " \
    "OR (cw.Amount<>pw.Amount) OR (cw.CloseDate <> pw.CloseDate)))".format(Prev_Week,Curr_Week)
cursor.execute(query)
results = dictfetchall(cursor)
print(results)

Output:

[{
        'opportunityid' : '1',
        'probability' : '50',
        'amount' : Decimal('30.35'),
        'closedate' : datetime.date(2016, 8, 22),
        'stage' : 'Proposal'
    }, {
        'opportunityid' : '2',
        'probability' : '50',
        'amount' : Decimal('115.00'),
        'closedate' : datetime.date(2016, 6, 30),
        'stage' : 'Proposal'
    }, {
        'opportunityid' : '3',
        'probability' : '50',
        'amount' : Decimal('200.00'),
        'closedate' : datetime.date(2016, 8, 29),
        'stage' : 'Proposal'
    }
]

Query:

SELECT cw.opportunityid, cw.probability, pw.probability, cw.stage, pw.stage, cw.amount, pw.amount, cw.closedate, pw.closedate FROM opty_data cw 
LEFT JOIN opty_data pw 
ON cw.opportunityid = pw.opportunityid AND pw.Week = "2016-35" WHERE cw.Week = "2016-36" 
AND IF(pw.opportunityid IS NULL, TRUE, ((cw.probability <> pw.probability) OR (cw.stage <> pw.stage) OR (cw.Amount<>pw.Amount) OR (cw.CloseDate <> pw.CloseDate)))

Expected Output shown correctly by MySQL:

+-----------------+-------------+-------------+----------------+----------------
+------------+-------------+------------+------------+
| opportunityid   | probability | probability | stage          | stage
| amount     | amount      | closedate  | closedate  |
+-----------------+-------------+-------------+----------------+----------------
+------------+-------------+------------+------------+
| 1 | 50          | 50          | Proposal       | Proposal
|   20.35 |    30.35 | 2016-08-22 | 2016-08-22 |
| 2 | 50          | 50          | Proposal       | Proposal
|  113.00 |    115.00 | 2016-09-06 | 2016-06-30 |
| 3 | 0           | 50          | Drop           | Proposal
|  200.00 |   200.00 | 2016-08-29 | 2016-08-29 |
5
  • Can you post your SQL query you ran? Commented Sep 8, 2016 at 19:45
  • 1
    the data are the same. It's just that python returns the result as a python dictionary. You need to format it according to your needs. Commented Sep 8, 2016 at 19:47
  • @MooingRawr: Added query in the original post. Commented Sep 8, 2016 at 19:56
  • @Jean-FrançoisFabre: How do I get results from both tables in the output if script is fetching complete output? Commented Sep 8, 2016 at 19:58
  • Formatting values to SQL query strings can be error prone and outright dangerous. Have a look at how to use placeholders with your DB-API. Those will handle quoting, escaping and converting python values to SQL constructs for you. Commented Sep 8, 2016 at 19:58

1 Answer 1

1

A Python dictionary consists of unique key value pairs, so one key can just appear once in a dictionary. As your raw SQL query returns two distinct values from the same column in a single row, the second occurrence of the column overwrites the first in the dictionary. However, you can easily fix this by specifying aliases for the columns using the AS keyword. Check out the following example:

SELECT 
    p1.name AS p1name, 
    p2.name AS p2name 
FROM 
    p1, p2 
WHERE 
    p1.id != p2.id
Sign up to request clarification or add additional context in comments.

2 Comments

Wow. How can I forget that. Thanks for your help.
If this works for you, consider accepting the answer. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.