Using MySQL Case for comparing dates -


i need tabulate data 3 tables report. please see query

select     date( sale.sale_time ) dat,     location.location_name location,     sale.sale_id total_orders,      sum( case sale.is_cancelled when 0 sale.sale_amount   else 0 end ) sales,      sum( case sale.is_cancelled when 0 sale.sale_discount else 0 end ) discounts,     sum( case sale.is_cancelled when 0 sale.sale_tax      else 0 end ) taxes,      count( distinct sale.customer_id ) total_customers,      sum( case when date( person.added_on ) = date( sale.sale_time ) 1 else 0 end ) new_customers,     sum( case sale.is_cancelled when 1 1 else 0 end ) cancelled_orders     sales sale     inner join locations location on location.location_id = sale.location_id     inner join people    person   on     person.person_id = sale.customer_id group     dat,location 

the results new_customers showing wrong, more total_customers. wrong, , how correct this?

the results this:

    dat location    total_orders    sales   discounts   taxes   new_customers   total_customers cancelled_orders 15-03-14    location1   52  1355    0   129.04  4   2   0 16-03-14    location1   56  280 0   30  2   1   0 16-03-14    location2   59  2518    0   212.2   3   6   2 

as might have guessed query, sales table has columns sale_id,sale_time,sale_cost,sale_discount,sale_tax,sale_amount, is_cancelled (tinyint values 0 or 1), , customer_id

people table has columns person_id, first_name, added_on

by comparing date(salessale_time) date(person.added_on), want list customers added on date

i modified query following new customers in result set.

select date(sale.sale_time) dat, location.location_name location, (sale.sale_id) total_orders,  sum(case sale.is_cancelled when 0 sale.sale_amount else 0 end) sales,  sum(case sale.is_cancelled when 0 sale.sale_discount else 0 end) discounts, sum(case sale.is_cancelled when 0 sale.sale_tax else 0 end) taxes,  count(distinct(sale.customer_id)) total_customers,  (select count(person_id) people date(added_on) = date(sale.sale_time) , person_id = sale.customer_id) new_customers, sum(case sale.is_cancelled when 1 1 else 0 end) cancelled_orders sales sale inner join locations location on location.location_id = sale.location_id group dat,location; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -