You can use a PIVOT clause. Your query could be something like this:
WITH Source as (
SELECT Name1, Name2, [Value]
FROM mytable
)
SELECT Name2, CASE WHEN A IS NOT NULL THEN A ELSE 'your string' END As A
, CASE WHEN B IS NOT NULL THEN B ELSE 'your string' END As B
FROM (
SELECT Name2, Name1, [Value]
FROM Source
) s
PIVOT
(
MAX([Value]) FOR Name1 IN (A, B) -- any other Name1 would go here
) p
using your sample data above, my results were
P1 1 3
P2 1 1
P3 2 your string
P4 your string 1
EDIT:
Since you have an unknown number of columns, you will need to look at using dynamic SQL and there are several answers here on SO about that with PIVOT.
SQL Server 2005 Pivot on Unknown Number of Columns
Pivot Table and Concatenate Columns