Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In below query there is a column called lead_id i want to find count of duplicate lead_id
in my result if there are 10 unique lead id i must get 10
but it must be grouped based on created_time ie.. if there are 2 unique lead_id for today's date then the result would be 2 ..


PS: i want only count of the duplicate data but i need to group by created_time only

i tried like this
SQL
SELECT COUNT(*) as repetitions ,lead_id
 FROM mg_lead_suggested_listing
 GROUP BY lead_id
 HAVING repetitions > 1;

but it will not fit to my requirement as i need to group by time_created


SQL
select
    t.created_time,
    t.timecreated,
    sum(t.suggested_pending_cnt),
    sum(t.suggested_dropped_cnt)
from
    (select
        date_format(timecreated, '%d-%b-%Y') created_time,
            timecreated,
            case
                when source = 2 then 1
                else 0
            end suggested_pending_cnt,
            case
                when (source = 2 && directory_status = 4) then 1
                else 0
            end suggested_dropped_cnt
    from
        mg_lead_suggested_listing) t
group by t.created_time
order by t.timecreated desc
limit 10
Posted
Updated 25-Nov-14 1:06am
v2

check this

SQL
SELECT COUNT(lead_id) as repetitions ,Convert(Date,created_time)
FROM mg_lead_suggested_listing
GROUP BY Convert(Date,created_time)
HAVING repetitions > 0;


Additionally you can also try

SQL
select * from
(
select  
   Row_number() Over 
             (Partition by Convert(Date,created_time) Order by   
                          Convert(Date,created_time)) as repetitions 
   ,*
from mg_lead_suggested_listing 
) a where repetitions>1 
 
Share this answer
 
v3
;with cte_dup
as
(
select ROW_NUMBER() over( partition by columnname1 order by columnname1) as rno, * from Tablename
)
select * from cte_dup where rno>=2
 
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