• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

how to get max record and count from same table?

i have employee table

Employeeid   name

A1111          AAA

A1112         AA3

 

and   i have empIN  table

Employeeid    Attemptdate                                             SQTY    

A1111            2012-02-06 10:26:22.673                            1

A1111            2012-02-06 10:26:22.674                            2

A1111            2013-01-18  08:26:122.550                         8

 

A1112            2013-01-21 10:42:47.720                             7

A1112            2013-01-21 10:42:47.721                             8

A1112            2012-10-28 12:42:48.621                             1

A1112             2012-10-28 12:42:48.622                           8

A1112             2012-10-28 12:42:48.623                            5

A1112              2012-12-30  12:42:48.622                          8

A1112                2012-10-30 12:42:48.623                         5

 

using this 2 tables i need the following output- how can i achieve this using sql qurey

LastAttempt= his last attempt   max(date)

NoOfAttempts= NO  of  attempts  (A1111 made only two attempts, one is on 2012-02-06 & 2013-01-18 ) - take only one record per day , do not consider time )

OutPut:

Employeeid    code         LastAttempt                               NoOfAttempts        

A1111             AAA      2013-01-18 08:26:122.550              2

A1112            AA3        2013-01-21 10:42:47.721                3
0
Varshini S
Asked:
Varshini S
  • 3
  • 3
  • 3
1 Solution
 
David ToddSenior DBACommented:
Hi,

select
    EmployeeID
    , code
    , max( AttemptDate )
    , sum( SQTY )
from table
group by
    EmployeeID
    , code
;

let me know if you want a fuller & tested example

HTH
  David
0
 
SANDY_SKCommented:
Try this
select b.Employeeid , name ,max(datein) as lastAttempt,count(*) as NoOfAttempts from(
select distinct convert(varchar, Attemptdate, 105) datein,Employeeid from empIN
) as tab inner join employee b on tab.Employeeid = b.Employeeid group by b.Employeeid,name

Open in new window

0
 
David ToddSenior DBACommented:
Hi SANDY_SK

count( * ) doesn't match the supplied sample data, where SQTY column has a quantity - I used sum( sqty ) to return this value.

I could have misread things as I commonly do

Regards
  David
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Varshini SAuthor Commented:
awosome

Thank You
0
 
Varshini SAuthor Commented:
select b.Employeeid , name ,max(datein) as lastAttempt,count(*) as NoOfAttempts from(
select distinct convert(varchar, Attemptdate, 105) datein,Employeeid from empIN
) as tab inner join employee b on tab.Employeeid = b.Employeeid group by b.Employeeid,name where where tab.NoOfAttempts =0

where condition returns wrong result in the following scenario

where tab.NoOfAttempts =0

When i give the above condition the NoOfAttempts  column shows value 1. I am not able to find the customer those who are not yet  made any attempt. What's wrong in the script ?
0
 
SANDY_SKCommented:
The above query will return only the employees who have logged in, you change the inner join to  a right join, it will include all the employees from the employee table, but then because of the group by, the count will always return 1 hence add a is null on the time it will work.

Use this...

select b.Employeeid , name ,max(datein) as lastAttempt,count(*) as NoOfAttempts from(
select distinct convert(varchar, Attemptdate, 105) datein,Employeeid from empIN
) as tab right join employee b on tab.Employeeid = b.Employeeid where datein is null
group by b.Employeeid,name 

Open in new window

0
 
David ToddSenior DBACommented:
Hi,

Can you post some sample data.

I fear that Sandy's latest attempt will also not give you what you are looking for 0 count( * ) will almost never ever return 0. That is, if the row doesn't exist - a necessary condition for count to be zero - then the row wont be in the result set - with the query the way it is written.

HTH
  David

PS My guess
select 
    b.Employeeid 
    , name 
    , max( Attemptdate ) as lastAttempt
    , count( Attemptdate ) as NoOfAttempts 
from(
    select 
        Attemptdate
        ,Employeeid 
    from empIN
) as tab 
right join employee b 
    on tab.Employeeid = b.Employeeid 
where 
    datein is null
group by 
    b.Employeeid
    ,name  
;

Open in new window

0
 
SANDY_SKCommented:
Hi dtodd,

I am aware the count will always return a value >= 1, if i understand it correctly, his only intention is to find list of users who has not logged in, nothing to do with the count.

Pls correct me if i am wrong.
0
 
Varshini SAuthor Commented:
dtodd- You are correct, If the user does not have record in empIN table - it needs to show like
Employeeid    code         LastAttempt                               NoOfAttempts        
A1112                AA3          NULL                                                  0
A1111               AAA       2013-01-18 08:26:122.550                  2
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now