Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

last 2 months records in SQL

$
0
0
I try this SQL query to get current total and also last 3 months records..

   
 Select distinct
    tblRv.Owner,
    (Select Count(*) as total from tblvv WHERE MasterID =tblRv.ID and Name <> '')
    as currentdata
    from tblRe
    inner join tblRv
    On tblRe.RID = tblRv.RID
    WHERE
    tblRe.StartDate >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -2, current_timestamp)), 0) and
    tblRe.StartDate >= '2016-07-01 00:00:00' AND
    tblRe.EndDate <= '2016-07-08 23:59:59' and tblRe.Region = 'uk' and
    tblRv.Owner='Roh'
    order by tblRv.Owner



so when i check indiviually write query and check 2016-06-01 and 2016-06-08 dates then this give data also for 5 and 4 months
...

but when i run above this query this show null
like this

    owner currentdata


and i want data like this

    owner currentdata(july) june May
    Roh     1                3    4



also when there will be current month i.e. aug then data must display last 2 months i.e.june july

Viewing all articles
Browse latest Browse all 7129

Trending Articles