如何获得每个帐户一条记录?

时间:2014-11-05 17:16:51

标签: sql

我有一个问题:

select memo_account ,flags_3,SUFFIX,date
   FROM MEMO
   left join daily on daily.ACCOUNT= memo.memo_account
   where flags_3  = 1 and suffix in (08,00)  
   and memo_number  =3333

我正在获取两个表中的帐户。 我在后缀为00或08的每个帐户基础上得到两行结果。

我怎么能得到它,我可以让帐户只显示一次 和后缀取决于一行是00还是08?

例如:

memo_account | suffix_00 | suffix_08 |日期

我做了http://sqlfiddle.com/#!6/e509b/2

1 个答案:

答案 0 :(得分:0)

select memo_account, 
       max(case when SUFFIX = '00' then '00' else null end) as suff00, 
       max(case when SUFFIX = '08' then '08' else null end) as suff08, 
       date
   FROM MEMO
   left join daily on daily.ACCOUNT= memo.memo_account
   where flag_3  = 1 and suffix in (08,00)  
   and memo_number  =3333
group by memo_account, date

你也有一个日期,所以如果同一个memo_account有不同的日期,你会得到一些额外的行