Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
Ramit -1 
Vickey - 1
Vivek-1
vikash-1
alok-1
ajay-1


I have two table like -

tbl1
owner_id         owner_name
1                   a
2                   b
3                   c


tbl2
can_id   CAn_Name   owner_id   createddate   
11        aa     1          10/3/2014
12        bb     2          9/3/2014
13        cc     3          8/3/2014 
14        dd     3          7/3/2014
15        ee     2          9/3/2014
16        ff     1          5/3/2014
17        gg     2          4/3/2014


I want result as -

CREATEDATE             a                         b                  c        
1/3/2014               0                        0                   0
2/3/2014               0                        0                   0
3/3/2014               0                        0                   0
4/3/2014               0                        1                   0
5/3/2014               1                        0                   0
6/3/2014               0                        0                   0
7/3/2014               0                        0                   1
8/3/2014               0                        0                   1           
9/3/2014               1                        2                   0
10/3/2014              0                        0                   0
11/3/2014              0                        0                   0



date will pass dynamic 1 to 31st and have to get count records datewise of each employee?
Posted
Updated 28-Apr-14 10:11am
v2
Comments
Corporal Agarn 10-Mar-14 8:26am    
How are your tables related? What does the "Ramit -1" information have to do with the question?
King Fisher 17-May-14 8:58am    
what have you tried?

1 solution

write your script something like this:

SQL
DECLARE @dtStartDate AS DATETIME = '3/11/2014'
DECLARE @dtEndDate AS DATETIME = '3/1/2014'

SELECT
    CREATEDATE,
    CASE WHEN EXISTS(SELECT * FROM tbl2 WHERE createddate  = D.CREATEDATE AND owner_id = 1)  THEN 1 ELSE 0 END AS a,
    CASE WHEN EXISTS(SELECT * FROM tbl2 WHERE createddate  = D.CREATEDATE AND owner_id = 2)  THEN 1 ELSE 0 END AS b,
    CASE WHEN EXISTS(SELECT * FROM tbl2 WHERE createddate  = D.CREATEDATE AND owner_id = 3)  THEN 1 ELSE 0 END AS c
FROM(
    SELECT
        CAST(@dtStartDate - langid AS DATE) CREATEDATE
    FROM sys.syslanguages
    WHERE @dtStartDate -langid  >= @dtEndDate
) D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900