Solved

how to get max record and count from same table?

Posted on 2013-01-25
9
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 35

Expert Comment

by:David Todd
ID: 38820282
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
ID: 38820384
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
ID: 38820420
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Closing Comment

by:Varshini S
ID: 38821400
awosome

Thank You
0
 

Author Comment

by:Varshini S
ID: 38825678
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
ID: 38825795
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
ID: 38826085
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
ID: 38826614
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
ID: 38830560
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

696 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