Solved

how to get max record and count from same table?

Posted on 2013-01-25
9
504 Views
Last Modified: 2013-01-29
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
Comment
Question by:Varshini S
  • 3
  • 3
  • 3
9 Comments
 
LVL 35

Expert Comment

by:David Todd
Comment Utility
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
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:David Todd
Comment Utility
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
 

Author Closing Comment

by:Varshini S
Comment Utility
awosome

Thank You
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:Varshini S
Comment Utility
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
 
LVL 11

Expert Comment

by:SANDY_SK
Comment Utility
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
 
LVL 35

Expert Comment

by:David Todd
Comment Utility
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
 
LVL 11

Expert Comment

by:SANDY_SK
Comment Utility
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
 

Author Comment

by:Varshini S
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now