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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tooba
Posting Yak Master

174 Posts

Posted - 02/06/2013 :  11:38:09  Show Profile  Reply with Quote
Hi guys,

I really appreciate if someone reply back ASAP.

Here is my sample data

Table Name = AddPr
ID,NOTES
1, ADD PR
2, DELETE PR
1, ADD PR
3, ABC
1, ADD Pr
3,Add Pr

Here is the query that i am using

Select ID, COUNT(DISTINCT ID) 'ADDPr into #temp1 from AddPr
where notes like '%ADD PR%'
GROUP BY ID


i am getting

ID,AddPr
1,1
2,1
3,1

However i want

ID,AddPr
1,3
3,1

Please guide me where i am wrong. Thank You.

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/06/2013 :  11:41:42  Show Profile  Reply with Quote
this is enough

Select ID, COUNT(*) 'ADDPr into #temp1 
from AddPr
where notes like '%ADD PR%'
GROUP BY ID


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

Go to Top of Page

tooba
Posting Yak Master

174 Posts

Posted - 02/06/2013 :  11:46:11  Show Profile  Reply with Quote
Awesome it will work, However i am getting other weired problem. When i am updating this result in other table using Updat statment. I am only getting * value not other value. Do you have any idea why?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/06/2013 :  11:48:27  Show Profile  Reply with Quote
hmm? can you show your full update query please? also whats the datatype of field on which you're doing the update?

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

Go to Top of Page

tooba
Posting Yak Master

174 Posts

Posted - 02/06/2013 :  11:51:56  Show Profile  Reply with Quote
Here is my Simple Update Statement

UPDATE #TEMP1
SET #TEMP1.AddPr = P.ADDPr
FROM #TempProcedure P
WHERE #TEMP1.ID = P.ID
Go to Top of Page

tm
Posting Yak Master

160 Posts

Posted - 02/06/2013 :  13:27:45  Show Profile  Reply with Quote
Try ..

UPDATE TE
SET #TEMP1.AddPr = P.ADDPr
FROM #TEMP1 TE
INNER JOIN #TempProcedure P
ON TE.ID = P.IDtry
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/06/2013 :  22:46:17  Show Profile  Reply with Quote
quote:
Originally posted by tooba

Here is my Simple Update Statement

UPDATE #TEMP1
SET #TEMP1.AddPr = P.ADDPr
FROM #TempProcedure P
WHERE #TEMP1.ID = P.ID


this update wont work.
But I guess error you're getting is something unrelated

can you post exact error message you got?

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

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/06/2013 :  22:46:17  Show Profile  Reply with Quote
quote:
Originally posted by tooba

Here is my Simple Update Statement

UPDATE #TEMP1
SET #TEMP1.AddPr = P.ADDPr
FROM #TempProcedure P
WHERE #TEMP1.ID = P.ID


this update wont work.
But I guess error you're getting is something unrelated

can you post exact error message you got?

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

Go to Top of Page

tooba
Posting Yak Master

174 Posts

Posted - 02/06/2013 :  23:26:13  Show Profile  Reply with Quote
Thanks all of you guys. I solved this issue to create a @Variable Instead of #Temp Table and its working fine.
Note:- It was not any error. When i use above Update statement Instead of any value it insert *.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 02/07/2013 :  00:02:08  Show Profile  Reply with Quote
thats because of truncation error i guess. if your destination field doesnt have range to display converted value, it will show a *

see below part from books online which speaks on this


Truncating and Rounding Results
------------------------------------------------------
When you convert character or binary expressions (char, nchar, nvarchar, varchar, binary, or varbinary) to an expression of a different data type, 
data can be truncated, only partially displayed, or an error is returned because the result is too short to display. 
Conversions to char, varchar, nchar, nvarchar, binary, and varbinary are truncated, except for the conversions shown in the following table.

From data type                           To data type                                     Result
----------------------------------------------------------------------------------------------------------
int , smallint, or tinyint               char                                             *
                                         varchar                                          *



------------------------------------------------------------------------------------------------------
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.16 seconds. Powered By: Snitz Forums 2000