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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Query Help

Author  Topic 

tooba
Posting Yak Master

224 Posts

Posted - 2013-02-06 : 11:38:09
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

52326 Posts

Posted - 2013-02-06 : 11:41:42
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

224 Posts

Posted - 2013-02-06 : 11:46:11
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

52326 Posts

Posted - 2013-02-06 : 11:48:27
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

224 Posts

Posted - 2013-02-06 : 11:51:56
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 - 2013-02-06 : 13:27:45
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

52326 Posts

Posted - 2013-02-06 : 22:46:17
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

52326 Posts

Posted - 2013-02-06 : 22:46:17
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

224 Posts

Posted - 2013-02-06 : 23:26:13
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

52326 Posts

Posted - 2013-02-07 : 00:02:08
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
   

- Advertisement -