Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Merge count by grouping variables back into source table


Merge count by grouping variables back into source table

Author
Message
Scott In Sydney
Scott In Sydney
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 201
Summary: I need to identify which records are violating a unique index to get the source file fixed.

Details: Apologies, I know this should be simple, and I've Googled before posting. I'm used to a different version of SQL where this approach would work:

select t2.cnt, t1.*
from #t1 t1
left join
(
select caseid, claimnum, linenum, invoicenum, batchnum, count(*) as cnt
from #t1
group by caseid, claimnum, linenum, invoicenum, batchnum
) t2
on t1.caseid=t2.caseid
and t1.claimnum=t2.claimnum
and t1.linenum=t2.linenum
and t1.invoicenum=t2.invoicenum
and t1.batchnum=t2.batchnum
having t2.cnt > 1



I need a count by the grouping (index) variables, then re-join that number into the source data, then filter for count > 1. If my source file is 10K records, the output should be 10K records. I need all source columns in the output to analyze if it really is a duplicate record, or if another column needs to be added to the index.

The above query returns NULL for cnt for all records.

If you want a post with sample data to work with just let me know; I thought the above sufficient to explain the issue.

Thanks...
Hunterwood
Hunterwood
Say Hey Kid
Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)Say Hey Kid (678 reputation)

Group: General Forum Members
Points: 678 Visits: 297
Scott In Sydney (7/15/2014)
Summary: I need to identify which records are violating a unique index to get the source file fixed.

Details: Apologies, I know this should be simple, and I've Googled before posting. I'm used to a different version of SQL where this approach would work:

select t2.cnt, t1.*
from #t1 t1
left join
(
select caseid, claimnum, linenum, invoicenum, batchnum, count(*) as cnt
from #t1
group by caseid, claimnum, linenum, invoicenum, batchnum
) t2
on t1.caseid=t2.caseid
and t1.claimnum=t2.claimnum
and t1.linenum=t2.linenum
and t1.invoicenum=t2.invoicenum
and t1.batchnum=t2.batchnum
having t2.cnt > 1



I need a count by the grouping (index) variables, then re-join that number into the source data, then filter for count > 1. If my source file is 10K records, the output should be 10K records. I need all source columns in the output to analyze if it really is a duplicate record, or if another column needs to be added to the index.

The above query returns NULL for cnt for all records.

If you want a post with sample data to work with just let me know; I thought the above sufficient to explain the issue.

Thanks...


Hi,

your idea is correct; you have just made a "typo".
You perform the grouping in the nested query and not in the main query. Thus you can't use HAVING in the main query.

So: Just change the last row of your query to use a WHERE instead and it will work properly.

Good luck!

/M
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search