0

クエリからこれらの結果を取得します。

Jon Doe 63741 | 26.11.2012 11:32:09
Joh Doe 63741 | 06.12.2012 10:30:03

ご覧のとおり、1列目は同じですが、2列目の日付は等しくありません。SQLは次のようになります。

select name, date from (
    select 
      concat(sfoa.firstname, ' ', sfoa.lastname, ' ', sfoa.postcode) as name,
      sfo.created_at as "date"
          from sales_flat_order sfo join sales_flat_order_address sfoa on sfo.entity_id = sfoa.parent_id where sfoa.address_type = 'shipping' and sfoa.lastname = 'Doe'
  ) d

次に、次のように名前でグループ化します。

group by name having count(name) > 1

2つの日付の差分を取得します。それは可能ですか?結果は次のようになります。

Jon Doe 63741 | 10

2行以上になる可能性があります。常に、結果の最初と最後の行が必要です。

ありがとう!

4

2 に答える 2

2

はい、可能です。

select name, MAX(date) - MIN(date) as date_diff from (
select 
  concat(sfoa.firstname, ' ', sfoa.lastname, ' ', sfoa.postcode) as name,
  sfo.created_at as "date"
      from sales_flat_order sfo 
      join sales_flat_order_address sfoa 
      on sfo.entity_id = sfoa.parent_id 
      where sfoa.address_type = 'shipping' 
      and sfoa.lastname = 'Doe'
) d
group by name
having count(name) > 1

その後、必要に応じて日付をフォーマットできます。

于 2013-02-20T12:55:15.817 に答える
0

以下のように試すことができます:

select name, date1,date2, datediff(date1,date2) as diff from (
    select 
    concat(sfoa.firstname, ' ', sfoa.lastname, ' ', sfoa.postcode) as name,
    sfo.created_at as "date1"
    from sales_flat_order sfo join sales_flat_order_address sfoa on sfo.entity_id = sfoa.parent_id where sfoa.address_type = 'shipping' and sfoa.lastname = 'Doe'
    ORDER BY sfo.created_at LIMIT 1

    UNION

    select 
    concat(sfoa.firstname, ' ', sfoa.lastname, ' ', sfoa.postcode) as name,
    sfo.created_at as "date2"
    from sales_flat_order sfo join sales_flat_order_address sfoa on sfo.entity_id = sfoa.parent_id where sfoa.address_type = 'shipping' and sfoa.lastname = 'Doe'
    ORDER BY sfo.created_at DESC LIMIT 1
) d

GROUP BY name 
HAVING count(name) > 1

私はそれをテストしませんでした。

于 2013-02-20T12:58:28.050 に答える