Tony Pearce
asked on
Mysql distict with other select items
Hi,
I have this query:
SELECT telephone, store_name, order_status_id, date_added, count(total) AS totalorders
FROM
`order` torder
WHERE
(torder.order_status_id = '2' OR torder.payment_method = 'PayPal') AND
torder.date_added >= DATE_SUB(CURDATE(),INTERVA L 0 DAY)
GROUP BY store_name
Because of the way data has been saved some rows are doubled up, the only row I can use is 'telephone' what I want is to somehow add a 'DISTINCT' on 'telephone, have tried but all rows show,.
What am I missing?
Thanks
I have this query:
SELECT telephone, store_name, order_status_id, date_added, count(total) AS totalorders
FROM
`order` torder
WHERE
(torder.order_status_id = '2' OR torder.payment_method = 'PayPal') AND
torder.date_added >= DATE_SUB(CURDATE(),INTERVA
GROUP BY store_name
Because of the way data has been saved some rows are doubled up, the only row I can use is 'telephone' what I want is to somehow add a 'DISTINCT' on 'telephone, have tried but all rows show,.
What am I missing?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Arg, after thinking more about it the query i posted above wont work as it will pull any record that matches a telephone number from the inner query. ( sorry about that )
What kind of results are you trying to get from this?
Is cleaning up the data in the table a possibility ?
What kind of results are you trying to get from this?
Is cleaning up the data in the table a possibility ?
ASKER
Thanks everyone, do I need to rethink this whole thing?
It seems as though you might consider fixing your tables so that you have a better structure...
( will make your reporting easier in the future )
Assuming your dealing with orders:
-- Maybe have a table with header records that holds the transaction store info, date etc...
-- and a seperate table for line items that are linked to the transaction header record ID
that would contain item info, qty, price, discounts which you could query and sum up easily
( will make your reporting easier in the future )
Assuming your dealing with orders:
-- Maybe have a table with header records that holds the transaction store info, date etc...
-- and a seperate table for line items that are linked to the transaction header record ID
that would contain item info, qty, price, discounts which you could query and sum up easily
ASKER
Yes agree. I am running this against an e-commerce system, so cannot make changes, perhaps I should run a make table to tidy data before, then run the above query against that.
Thanks again everyone
Thanks again everyone
maybe post the sample data of what you have in the table, and what you want as output from that sample data.
it may help us to understand what you really want.
it may help us to understand what you really want.
ASKER
Took my own advise! built a temp table to run query and everything is fine.
Thanks to all of you for assistance.
Thanks to all of you for assistance.
SELECT telephone, store_name, order_status_id, date_added, count(total) AS totalorders
FROM `order` torder
WHERE telephone IN
(SELECT DISTINCT telephone
FROM `order` torder
WHERE
(torder.order_status_id = '2' OR torder.payment_method = 'PayPal') AND
torder.date_added >= DATE_SUB(CURDATE(),INTERVA
)
NOTE:: You should expect to see the same store listed more than once if it has different phone numbers going by this query...