Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-08-13 : 13:14:04
|
Is there an aggregate function (I can't find one) or other method for calculating the percentage of duplication across tables?Scenario:Table 1 is populated with distinct IDs restricted by one set of various criteria.Table 1 is populated with distinct IDs restricted by another, differing set.select distinct idinto table1where <criteria>join <tableX>insert into table1select distinct idwhere <different criteria>join <tableY> I want to return the percentage of IDs which have been added to table1 which were already added in the first iteration, if that makes sense. Yes, the table holds duplicates throughout the query, then is 'dumped' into another table, where IDs are made distinct for no duplication.In other words, I'm trying to calculate the waste involved. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-14 : 02:44:00
|
that you need to do before second insert. likeselect count(distinct id)*100.0/cntwhere <different criteria>join <tableY> ycross join (select count(distinct id) as cntwhere <different criteria>join <tableY>)where exists(select 1 from table1 where somefield= y.somefield) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-08-16 : 12:20:24
|
Thanks, but I don't understand this syntax. It's possible to join without an ON condition? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 12:32:11
|
select distinct idinto table1from <tableY>join <tableX>where <criteria>select count(distinct id)*100.0/cntfrom <tableX>join <tableY>cross join (select count(distinct id) as cntfrom <tableX>join <tableY>where <different criteria>)where <different criteria>------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-08-16 : 13:49:36
|
Thanks, but I am baffled. Msg 156, Level 15, State 1, Line 26Incorrect syntax near the keyword 'where'.This is the last where clause. I'm using SQL Server 2005. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-16 : 13:53:57
|
give derived table a nameselect distinct idinto table1from <tableY>join <tableX>where <criteria>select count(distinct id)*100.0/cntfrom <tableX>join <tableY>cross join (select count(distinct id) as cntfrom <tableX>join <tableY>where <different criteria>)twhere <different criteria> ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
dmilam
Posting Yak Master
185 Posts |
Posted - 2010-08-16 : 15:38:27
|
Thanks; giving the derived table a name, plus adding a group by clause, works now. Thanks again. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-08-17 : 10:33:27
|
welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|