Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Retrieving Values based on Condition

$
0
0
Hello All,

I am in a grave problem. First, following is my table description called csd

Code:

  Column Name        Data Type
  cust_id                bigint
  reading                bigint
  read_date            date

Some data are as follows

Code:

      cust_id          reading            read_date
      111                1505                2013-05-05
      111                1510                2013-05-12
      124                1620                2013-05-05
      124                1700                2013-05-06

From this data i want to retrieve only those whose date matches the current date. If for any cust_id, current date is not there it should retrieve the immediate previous date. In essence, the query will retrieve data for current date if it is there, otherwise it will retrieve data for previous date.

I tried the following query, but it retrieve both the current date and previous date data for all cust ids.

Code:

select distinct t.cust_id,t.reading,t.read_date from csd t where t.read_date=curdate() or exists(select read_date from csd where read_date<curdate());
Please help me in solving this query :eek:

Regards

alimdyusuf

Viewing all articles
Browse latest Browse all 13329

Trending Articles