SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 GROUP BY function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Tototo1
Starting Member

22 Posts

Posted - 04/19/2012 :  14:36:36  Show Profile  Reply with Quote
I am trying to pull in data that is duplicate in my table to flush out where the duplicates are. I have a statement that works and will get me my duplicates, but I cannot pull in the additional data I need. My query is:

SELECT udvalue, udjoin
FROM udf
WHERE udtype = 'MT' and udfindex = '221' and udjoin like '5%'
GROUP BY udvalue
HAVING COUNT(udvalue) > 1
ORDER BY udvalue

If I remove the udjoin in the select statement it works fine, but I also want to have the udjoin value that corresponds to the udvalue that is duplicate so I know where to make my changes. If I place the udjoin in the GROUP BY clause, I get no data. How can I get the udjoin value to show up?

Thank you!

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 04/19/2012 :  14:42:41  Show Profile  Reply with Quote

SELECT u.*
FROM udf u
INNER JOIN
(
SELECT udvalue
FROM udf 
WHERE udtype = 'MT' and udfindex = '221' and udjoin like '5%'
GROUP BY udvalue
HAVING COUNT(udvalue) > 1
)u1
ON u1.udvalue = u.udvalue
ORDER BY u.udvalue


why are you naming a table as udf by the way?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Tototo1
Starting Member

22 Posts

Posted - 04/19/2012 :  14:48:20  Show Profile  Reply with Quote
That is just the table name that is in the system I am currently using!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 04/19/2012 :  14:50:31  Show Profile  Reply with Quote
ok....not a good convention to name table like that

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Tototo1
Starting Member

22 Posts

Posted - 04/19/2012 :  14:53:28  Show Profile  Reply with Quote
Works beautifully. Thank you. I agree but I have no choice in the matter unfortunately!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 04/19/2012 :  14:54:22  Show Profile  Reply with Quote
no problem..its just a tip you can consider while you do object designs in future

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Tototo1
Starting Member

22 Posts

Posted - 04/19/2012 :  14:58:30  Show Profile  Reply with Quote
If you don;t mind me asking, does doing the

SELECT u.*
FROM udf u

create a temp table u? I guess I have never seen a select on something that didn't exist before the FROM statement. (I'm kinda new at this)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 04/19/2012 :  15:25:49  Show Profile  Reply with Quote
nope...it just fetched data from existing table udf. u is just a short name given for table called alias. Though here its not pronounced, using aliases will make sure you dont have to repeat table names everywhere and also clearly indicates which column comes from which table

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000