Memilih Tanggal Maks Dari Beberapa Tabel

bantuan apa pun akan sangat dihargai. Saya mencoba memilih tanggal aktivitas terakhir dari sekelompok tabel. Tabel tersebut mencakup Tanggal Masuk, Tanggal Catatan, tanggal pembayaran, dan Tanggal Klaim. Saya hanya ingin mengembalikan nilai maksimal dari semua tanggal ini. Selanjutnya saya hanya ingin catatan yang tidak ada aktivitas selama lebih dari 45 hari. Saat ini saya menggunakan SQL berikut untuk memasukkan semua tanggal kemudian menggunakan bidang terhitung di EXCEL untuk mencari tahu sisanya. Apakah mungkin melakukan ini semua dengan SQL?

Terima kasih sebelumnya.

SELECT xrxTrnLgr.PatId, xrxTrnLgr.Balance, 
       Max(xrxPatNotes.NoteDate) AS 'Max of NoteDate', 
       Max(xrxTrnIcf.PostDate) AS 'Max of IcfPostDate', 
       Max(xrxPat.EntryDate) AS 'Entry Date', 
       Max(xrxPat.Coverage) AS 'Coverage', 
       Max(xrxTrnPay.PostDate) AS 'Last Payment'
  FROM  xrxTrnLgr 
  LEFT OUTER JOIN xrxPatNotes ON xrxTrnLgr.PatId = xrxPatNotes.PatId
  LEFT OUTER JOIN  xrxTrnIcf ON xrxTrnLgr.PatId = xrxTrnIcf.PatId
  LEFT OUTER JOIN xrxPat ON xrxTrnLgr.PatId = xrxPat.PatId
  LEFT OUTER JOIN xrxTrnPay ON xrxTrnLgr.PatId = xrxTrnPay.PatId
  GROUP BY xrxTrnLgr.PatId, xrxTrnLgr.Balance
  HAVING (xrxTrnLgr.Balance>$.01)

person Michael Sigman    schedule 05.03.2013    source sumber
comment
Apa masalah dengan pertanyaan yang Anda posting?   -  person Mahmoud Gamal    schedule 05.03.2013
comment
Dialek SQL yang mana? mySQL, Microsoft SQL, Oracle, dll?   -  person Sparky    schedule 05.03.2013


Jawaban (1)


Saya pikir ini mungkin melakukan semuanya dalam SQL:

select t.patid, t.balance,
       max(case when which = 'note' then thedate end) as note,
       max(case when which = 'post' then thedate end) as post,
       max(case when which = 'entry' then thedate end) as entry,
       max(case when which = 'coverage' then thedate end) as coverage,
       max(case when which = 'lastPayment' then thedate end) as lastPayment
from xrxTrnLgr t left join
     ((select patid, notedate as thedate, 'note' as which
       from xrxPatNotes
      ) union all
      (select patid, postdate, 'post'
       from xrxtrnIcf
      ) union all
      (select patid, EntryDate, 'entry'
       from xrxPat
      ) union all
      (select paid, Coverage, 'coverage'
       from xrxPat.Coverage
      ) union all
      (select patid, PostDate, 'LastPayment'
       from xrxTrnPay.PostDate
      ) 
    ) d
    on t.patid = d.patid
group by t.patid, t.balance
having min(now() - thedate) >= 45
person Gordon Linoff    schedule 05.03.2013
comment
Itu adalah Microsoft SQL. Saya menanyakan data dari SQL server 2008. Saya mencoba kode di atas dan tidak berhasil. - person Michael Sigman; 07.03.2013