SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Join making conflict....


Join making conflict....

Author
Message
Sgar...
Sgar...
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 666
SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ss.ServerName +'</td>
<td>' + ss.DBName +'</td>
<td>' + Occurrences +'</td>

</tr>
'

FROM DBINVENTORY ss
INNER JOIN
(SELECT ServerName,DBname,
cast(COUNT(dbname) as varchar(5))AS Occurrences
FROM DBINVENTORY d
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )
)sq ON ss.DBName=sq.DBName

this giving me output like

----------------------------------------

Server_Name Dbname Occurences
X ABC 2
X ABC 2
Y HIJ 2
Y HIJ 2
Y NOP 2
Y NOP 2
Z BCD 4
Z BCD 4
Z BCD 4
Z BCD 4
Z JIK 4
Z JIK 4
Z JIK 4
Z JIK 4
Z JIK2 4
Z JIK2 4
Z JIK2 4
Z JIK2 4
A ABC 2
A ABC 2
A TUV 2
A TUV 2
B EFG 2
B EFG 2
D ACD 2
D ACD 2
---------------------------------------

Actual output required is


Server_Name Dbname Occurences
X ABC 2
Y HIJ 2
Y NOP 2
Z BCD 4
Z BCD 4
Z JIK 4
Z JIK 4
Z JIK2 4
Z JIK2 4
A ABC 2
A TUV 2
B EFG 2
D ACD 2


Please advise the join condition.....

Sagar Sonawane
** Every DBA has his day!!Cool
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27473 Visits: 13268
Maybe you can give some sample data for table DBINVENTORY.
Why is it OK for some servername-dbname pairs to have 2 rows as output and for others isn't?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sgar...
Sgar...
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 666
Main objective is to find duplicate db's...... and out put is perfect..... which I am looking for....

Can you please help wi

Sagar Sonawane
** Every DBA has his day!!Cool
rhythmk
rhythmk
SSC Eights!
SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)SSC Eights! (912 reputation)

Group: General Forum Members
Points: 912 Visits: 1078
Saga... (8/27/2012)


Main objective is to find duplicate db's...... and out put is perfect..... which I am looking for....

Can you please help wi


Sorry but it is difficult without the sample data and the actual requirement.Here none of these are not clear Sad

--rhythmk
------------------------------------------------------------------
To post your question use below link

http://www.sqlservercentral.com/articles/Best+Practices/61537/
:-)
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27473 Visits: 13268
Saga... (8/27/2012)


Main objective is to find duplicate db's...... and out put is perfect..... which I am looking for....

Can you please help wi


I asked two questions. You didn't answer any of them.
How much effort do you think I will put in your questions?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sgar...
Sgar...
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 666
SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ss.ServerName +'</td>
<td>' + ss.DBName +'</td>
<td>' + Occurrences +'</td>

</tr>
'

FROM /*DBINVENTORY ss --after removing this commented words...I got required output
INNER JOIN*/

(SELECT ServerName,DBname,
cast(COUNT(dbname) as varchar(5))AS Occurrences
FROM DBINVENTORY d
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )
)sq ON ss.DBName=sq.DBName

Sagar Sonawane
** Every DBA has his day!!Cool
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26193 Visits: 17539
Saga... (8/27/2012)
SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ss.ServerName +'</td>
<td>' + ss.DBName +'</td>
<td>' + Occurrences +'</td>

</tr>
'

FROM /*DBINVENTORY ss --after removing this commented words...I got required output
INNER JOIN*/

(SELECT ServerName,DBname,
cast(COUNT(dbname) as varchar(5))AS Occurrences
FROM DBINVENTORY d
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )
)sq ON ss.DBName=sq.DBName


So does this mean you solved your issue or not?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16625 Visits: 19098
You were joining your grouped results with the original table with duplicates. You can get an even simpler version of your code like this, no need of a subquery:


SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ServerName +'</td>
<td>' + DBName +'</td>
<td>' + cast(COUNT(dbname) as varchar(5)) +'</td>

</tr>
'
FROM DBINVENTORY
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Sgar...
Sgar...
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 666
Luis Cazares (8/27/2012)
You were joining your grouped results with the original table with duplicates. You can get an even simpler version of your code like this, no need of a subquery:


SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ServerName +'</td>
<td>' + DBName +'</td>
<td>' + cast(COUNT(dbname) as varchar(5)) +'</td>

</tr>
'
FROM DBINVENTORY
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )




Thanks luis....

Sagar Sonawane
** Every DBA has his day!!Cool
Sgar...
Sgar...
Old Hand
Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)Old Hand (355 reputation)

Group: General Forum Members
Points: 355 Visits: 666
Sean Lange (8/27/2012)
Saga... (8/27/2012)
SELECT @MSGOUT = @MSGOUT + '
<tr>
<td>' + ss.ServerName +'</td>
<td>' + ss.DBName +'</td>
<td>' + Occurrences +'</td>

</tr>
'

FROM /*DBINVENTORY ss --after removing this commented words...I got required output
INNER JOIN*/

(SELECT ServerName,DBname,
cast(COUNT(dbname) as varchar(5))AS Occurrences
FROM DBINVENTORY d
GROUP BY ServerName,DBName
HAVING ( COUNT(DBName) > 1 )
)sq ON ss.DBName=sq.DBName


So does this mean you solved your issue or not?


Yes Sean...it solved my issue...

Sagar Sonawane
** Every DBA has his day!!Cool
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