SELECT distinct
a.entity_id orders_id,
coalesce(c.entity_id,a.entity_id) customers_id, 
concat(sfqa.firstname,' ',sfqa.lastname) customers_name, 
sfqa.company customers_company, 
sfqa.street customers_street_address, 
sfqa.region customers_suburb, 
sfqa.city customers_city, 
sfqa.postcode customers_postcode, 
sfqa.region customers_state, 
(case when sfqa.country_id = 'NL' then 'Nederland' when 'BE' then 'Belgie' end) customers_country, 
sfqa.telephone customers_telephone, 
sfqa.email customers_email_address, 
0 customers_address_format_id, 
concat(sfqa.firstname,' ',sfqa.lastname) delivery_name, 
sfqa.company delivery_company, 
sfqa2.street  delivery_street_address, 
sfqa2.region delivery_suburb, 
sfqa2.city delivery_city, 
sfqa2.postcode delivery_postcode, 
sfqa2.region delivery_state, 
(case when sfqa2.country_id = 'NL' then 'Nederland' when 'BE' then 'Belgie' end) delivery_country, 
0 delivery_address_format_id, 
concat(sfqa.firstname,' ',sfqa.lastname) billing_name, 
sfqa.company billing_company, 
sfqa.street billing_street_address, 
sfqa.region billing_suburb, 
sfqa.city billing_city, 
sfqa.postcode billing_postcode, 
sfqa.region billing_state, 
(case when sfqa.country_id = 'NL' then 'Nederland' when 'BE' then 'Belgie' end) billing_country, 
0 billing_address_format_id, 
'' payment_method, 
'' cc_type, 
'' cc_owner, 
'' cc_number, 
'' cc_expires, 
a.updated_at last_modified, 
a.created_at date_purchased, 
a.status orders_status, 
null orders_date_finished, 
'EURO' currency, 
1 currency_value,
concat('Uw kenmerk       : ', sfqp.po_number) remark1,
concat('Uw ordernummer : ', a.increment_id) remark2,
concat('Uw orderdatum    : ', a.created_at) remark3

FROM 
sales_flat_order a left JOIN 
customer_entity c ON ( c.entity_id = a.customer_id ) JOIN 
sales_flat_order_address sfqa ON ( a.entity_id = sfqa.parent_id ) JOIN 
sales_flat_order_address sfqa2 ON ( a.entity_id = sfqa2.parent_id ) JOIN 
sales_flat_order_payment sfqp ON ( a.entity_id = sfqp.parent_id ) 

where 
a.store_id = <@PREFIX> and
sfqa.address_type = 'billing' and 
sfqa2.address_type = 'shipping' and 
a.status in ('processing','pending_paypal','pending_payment','pending','payment_review','holded') and
(a.created_at >= <@FROMDATE@> and a.created_at <= <@TODATE@>  )
order by a.created_at