Friday, 3 August 2012

Updating a T-SQL table from rows in the same table

I had to do a very simple Microsoft SQL query in which I wanted to update some of the values in a row from a row in the same table. Actually, the query was already there, but was using two local variables to store the information, then make the update. Something like this:
DECLARE @Var1 INT
DECLARE @Var2 INT
SELECT @Var1=Column1,@Var2=Column2 FROM MyTable WHERE ID=1
UPDATE MyTable SET Column1=@Var1,Column2=@Var2 WHERE ID=2
I really hated that I was using two SQL statements and all that declaring to do a simple update, so I looked up the syntax for the UPDATE statement. It said that if I want to update a table from a source I need to use the FROM keyword, like this:
UPDATE MyTable 
SET Column1=Alias.Column1,Column2=Alias.Column2
FROM MyOtherTable AS Alias
WHERE ID=2
AND Alias.ID=1
As you can see, we use an alias to name another table or query, we use the Alias name for all the conditions for that table and nothing for the conditions on the table we update. Easy, no? I even tested it and it worked. So I tried this:
UPDATE MyTable 
SET Column1=Alias.Column1,Column2=Alias.Column2
FROM MyTable AS Alias
WHERE ID=2
AND Alias.ID=1
I used the same table to update and to alias and it seemed to work. However, the number of updated columns was always 0. Remarkable how difficult it is to find on the net a straight answer about a simple situation like this.

It turns out that even with the alias, MSSql is confusing some things. The solution is to use a query from your table, rather than the name of the table itself. Here is how you do it:
UPDATE MyTable 
SET Column1=Alias.Column1,Column2=Alias.Column2
FROM (SELECT * FROM MyTable) AS Alias
WHERE ID=2
AND Alias.ID=1


SQL 2005 also introduced Common Table Expressions, which can be used to clarify a query. In this case, using a CTE results in the same execution plan and makes the entire query even more convoluted:
WITH Alias(Column1,Column2)
AS (
SELECT Column1, Column2 FROM MyTable
)
UPDATE MyTable
SET Column1=Alias.Column1,Column2=Alias.Column2
FROM Alias
WHERE ID=2
AND Alias.ID=1

Even if the documentation says you can specify a CTE without declaring the column names, I couldn't do it in this situation, I don't know why. I admit I only tried the CTE solution for a minute before discarding it as too verbose.

Thursday, 26 July 2012

YouTube Playlist Autoplay not working

Update: The bug was fixed and today, 28th of July 2012, YouTube's playlist autoplay is working again. I can't help think maybe it was an experiment to see how much of the traffic was playlists. Whether intentional or not, the statistical information this incident created should be very interesting.

Today I opened a YouTube page, loaded one of my music playlists and started listening to the music in the background. After the first song played, the next started, only with the Autoplay button off, so when it ended, the entire playlist play stopped.

No, this is not you, it's not from your computer settings, Flash updates or anything like that, it is a YouTube issue that keeps turning off the autoplay feature. I don't know if it is an attempt to force people to look at the pages with ads on them or simply an accident - my guess is that it is a bug - but this annoying behaviour started today. The funny thing is that if you press the "Next video" button, the Autoplay button gets back to on, another argument for this being a bug.

I've found a Google topic about this problem, you can find it here, also a Yahoo Answers question, that you can find here.

I don't have a solution for this, but I will update this post if I find it.

Wednesday, 18 July 2012

The Stranger, by Albert Camus

I started reading The Stranger on a recommendation. A guy told me the main character was a bit like me. Having read the book there is only one question I want to ask: How did Albert Camus know about me in 1942?

Joke aside, this a rather short book detailing a bit of the life of a Frenchman that doesn't feel much, is a rational person and doesn't believe in God. Therefore everybody around him thought him a stranger, hence the title of the book. Camus has a way of writing this in the first person, but in a way that seems both introvert and totally outside. The character describes everything he sees in neutral terms, proving he is both very observant and completely indifferent. When other people talk, he quotes them, when he describes himself talking, the character is summarising to the minimum, with no quotes, giving the impression he is a stranger to himself, hence the title of the book.

In the end, it felt like the last part of the movie The Mist. The same irrational fear of the different, coming from a shapeless mass of mindless drones, blindly destroying everything. In my view, the character manages to maintain his decency by rejecting the smallest gesture of resistance. But what do I know?

Would I recommend the book? It certainly teaches the reader something. It describes a way of thinking and of being that is different from most people. It explores several philosophical schools of thought, but you wouldn't know about it unless you read Wikipedia :). It is short enough to not bother anyone, though. Why wouldn't you read it?

Saturday, 14 July 2012

My first game on chess.com

I have seen there is a marked difference between me, a casual chess player that has become interested in the theory of chess, and other people of my level that do not try to understand the theory. I usually beat them with the help of some of the information that lingers in my head. At the same time, there is an even greater difference between me and people that actively play chess online, not only when a colleague becomes interested in playing. They crush me immediately.

It seems to me that in order to grasp the chess theory I must also back it up with practice. So I have decided (after quite a while of fearing it would either become addictive or that I would suck terribly) I've made the first step and played a game there. And I won! Woo hoo! Unfortunately I played horribly and only luck can be attributed to my victory. As always, I've analysed the PGN and here it is. You will understand the shame of my victory (yes, it can happen) when you get to the end. Enjoy!

[Event "Siderite vs Mar09 2012.07.14"]
[Site "Chess.com"]
[Date "2012.07.14"]
[Round "1"]
[White "Siderite"]
[Black "Mar09"]
[Result "1-0"]
[BlackElo "2400"]
[ECO "C41"]
[Opening "Philidor"]
[Variation "Hanham, Steiner Variation"]
[WhiteElo "2400"]
[TimeControl "0+300"]
[Termination "normal"]
[PlyCount "77"]
[WhiteType "human"]
[BlackType "human"]

1. e4 {+0.11/16 10} e5 {-0.09/16 10} 2. Nf3 {+0.13/15 10} d6 {-0.31/15 10}
3. h3 {+0.15/15 10} Be7 {-0.15/15 10} 4. d4 {+0.16/16 10 Didn't know
anything about the Philidor defence that Black employed, except that it is
rather passive. So I decide to attack.} Nd7 {-0.26/15 10} 5. dxe5 {+0.26/16
10} Nxe5 {-0.24/16 10} 6. Nxe5 {+0.23/17 10} dxe5 {-0.23/17 10} 7. Qxd8+
{+0.23/17 10} Bxd8 {-0.15/17 10} 8. Bc4 {+0.04/17 10 computer thinks that
defending the pawn with the knight would have been better. As such, Black
has equality.} Ne7 {-0.23/16 10} 9. O-O {+0.27/16 10} O-O {-0.26/17 10} 10.
Nd2 {+0.20/18 10} Kh8 {-0.38/16 10 Analysis says this is a bad move and
gives White 0.2 because the pawn at f7 is defended only by the rook, but
Black intends to move it forward anyway.} 11. Nf3 {+0.35/16 10} f6
{-0.30/16 10} 12. Bd2 {+0.27/17 10 at this moment I am lost: I have no
strategy, no obvious attack and so I decide to improve my position a little
bit.} a6 {-0.28/15 10} 13. Bb4 {+0.24/17 10} b5 {-0.88/17 10 I thought
about the computer suggested move at the time: Bc4-d5, but dismissed it
because I felt it did not do anything.} 14. Bb3 {+0.46/17 10} a5 {-0.48/18
10 I almost always fall for pawn pushes. I tend to dismiss their
importance, you see, until it is too late.} 15. Bc5 {+0.48/19 10} a4
{-0.57/18 10} 16. Bd5 {+0.71/17 10 Bd5 is no longer as effective as it
would have been.} Ra6 {-0.54/17 10} 17. a3 {+0.54/19 10 The only move to
save my poor light bishop from getting trapped after c6.} c6 {-0.86/19 10}
18. Ba2 {+0.88/19 10} h6 {-1.97/17 10 You see what I should have done here?
Ra1-d1 would have threatened the bishop on d8, the only defender of the
pinned knight on e7.} 19. c4 {+0.59/17 10 Instead I tried to exchange a
weak pawn with a strong one, breaking the menacing pawn chain c6, b5, a4}
(19. Rad1 Kh7 20. Rxd8 Rxd8 21. Bxe7 Rd7) 19. .. Re8 {-0.72/18 10} 20. Rad1
{+0.43/18 10} Ng6 {-5.97/17 10 too late for Rd1, but do you see the winning
move for White here?} 21. g3 {+0.33/17 10 instead I get spooked by the
knight and try to block it and leaving my h3 pawn undefended.} (21. cxb5
cxb5 22. Bf7 {nice pin, one might think, but look closer: the rook at e8
has nowhere to go, the bishop on d8 is defended by the rook alone and the
knight on g6 is also under attack. This gives White a staggering 6 point
advantage.} Rg8 23. Bxg8 Ba5 24. Bf7 Nf4 25. Be8 {b5: completely
undefendable} Ne6 26. Be3 b4 27. Bb5 Ra8 28. Bc6 Ra6 29. Rc1 bxa3 30. bxa3
Nf4 31. Bxf4 exf4 32. Rfd1 Be6 33. Bxa4 Ra8 34. Bc6 Ra7) 21. .. Bb6
{-2.12/18 10 at this moment I still had the trap for the rook on e8, but it
was invisible to me.} 22. Bxb6 {+0.22/17 10} (22. cxb5 Bxc5 {this is the
best move in the situation} (22. .. cxb5 23. Bxb6 Bxh3 24. Rfe1 Rxb6 25.
Bf7 Reb8 26. Bxg6 {White has more than 2 points advantage here.}) 23. bxa6)
22. .. Rxb6 {-0.17/18 10} 23. Kh2 {-0.05/18 10 Yay, I saved the pawn!, I
thought at the moment... by doing so I have missed every opportunity and
reached equality with Black.} c5 {-0.38/17 10} 24. Rd5 {-0.15/17 10} Bb7
{+0.14/18 10} 25. Rxc5 {-0.17/19 10} Bxe4 {-0.07/18 10} 26. Nd2 {+0.09/19
10} Bd3 {-0.11/18 10} 27. Re1 {-0.04/17 10 the computer advises to take the
soon to be open c file. I miss that, too. Little did I know what incredible
edge it would give me at the end. Goooo, luck!} b4 {0.00/16 10} 28. Re3
{-0.07/16 10} Bc2 {+0.13/16 10 again, I decided to play aggressively. If I
could exchange the rooks, I would have a powerful passed pawn.} 29. Rb5
{-0.41/18 10} Rd6 {+0.45/19 10} 30. Ne4 {-1.51/16 10} Bxe4 {+1.64/17 10}
31. Rxe4 {-1.55/19 10} b3 {+1.62/17 10} 32. Bb1 {-2.36/16 10 my light
bishop is trapped. All one has to do is move the rook on d1.} Rd2 {+0.83/19
10 fortunately Black misses it.} 33. c5 {-2.11/17 10 and I ignore that f2
is undefended and, more, it would place me in check.} Rxf2+ {+2.10/19 10}
34. Kg1 {-2.13/20 10} Rxb2 {+2.19/20 10} 35. Re1 {-2.24/19 10 the situation
is dire. Black has 2 pawns ahead and if he sees that I attacked his knight
with the bishop while defending it with the rook, I am a goner.} Ne7
{+2.36/18 10} 36. Rb4 {-2.40/18 10} Nc6 {+1.73/15 10} 37. Rxa4 {-2.18/16
10} Nd4 {0.00/18 10 He saw the knight being attacked, but didn't notice the
pawn on e5 is pinned. It is not all lost for Black, since after a few
exchanges we reach equality. But check out the next moves!} 38. Rxd4
{+0.03/19 10} exd4 {-M1/69 10} (38. .. Rxb1 39. Rxb1 exd4 40. Rxb3
{complete equality and probably a loss for me, as I don't know much about
endgames.}) 39. Rxe8# {Yup. Mate. Neither of us has seen it. When I took
the rook I thought he resigned, as the game ended so suddenly. But no, it
was an accidental mate. How embarrassing.} 1-0

Friday, 13 July 2012

Javascript maps

This will be a short blog post that shows my error in understanding what Javascript maps or objects are. I don't mean Google Maps, I mean dynamic objects that have properties that can be accessed via a key, not an index. Let's me exemplify:
var obj={
property1:"value1",
property2:2,
property3: new Date()
};
obj["property 4"]="value 4";
obj.property5=new MyCustomObject();
obj[6]='value 6';
console.log(obj.property1);
console.log(obj['property2']);
console.log(obj["property3"]);
console.log(obj['property 4']);
console.log(obj.property5);
console.log(obj[6]);
In this example, obj is an instance of object that had three properties and others are added. First the declaration notation is JSON like, then any object can be assigned to a property via two notations: the '.'(dot) and the square brackets. Note that the value of 'property 4' and of '6' can only be accessed via square brackets, there is no dot notation to escape that space and obj.6 is invalid.

Now, the gotcha is that, coming from the C# world, I've immediately associated this with a Hashtable class: something that can have any object as key and any object as value, but instead, a map is more like a Dictionary<string,object>.

Let me show you why that may be confusing. This is perfectly usable:
obj[new Date()]=true;
In this example I've used a Date object as a key. Or have I? In Javascript any object can be turned into a string with the toString() function. In fact, our Javascript map uses a key much like 'Sat Jul 14 2012 00:07:00 GMT+0300 (GTB Daylight Time)'. The translations from one type to another are seamless (and can generate quite a bit of righteous anger, too).

My point is that you can also use something like
obj[new MyObject()]=true;
only to see it blow in your face. The key will most likely be '[Object object]'. Not at all what was expected.


So remember: javascript properties can be any string, no matter how strange, but not other types. obj[6] will return the value you have set in obj[6] because in both cases that 6 is first turned into a string '6' and then used. It has nothing to do with the '6th value' or '6th property'. Those are arrays. The same for a Date or some custom object that has a toString() function that returns something unique for that object. I wouldn't use that, though, as you would probably want to use objects as keys and compare them by reference, not string value.

Thursday, 12 July 2012

Blood Music, by Greg Bear

book cover Greg Bear is the type of writer that I just have to love: he writes hard SciFi, but easy to read, he doesn't bullshit the reader with too much technobabble and, even if his writing is by no means simplistic, he doesn't bore you with how some person or another feels unless it propels the subject forward. Blood Music is also of a subgenre that I love: global pandemic, so I have to also love the book. So take that into account when you read my review.

The book starts with a preposterous idea: that cells can become intelligent. A brilliant scientist, but one that has always cut corners to get around, discovers this almost by accident and... well... cuts a few corners. The result is something that feels like John Saul's The God Project, but soon leaves it and reaches for the stratosphere. The end is typical Greg Bear, astounding and megalomaniac.

Blood Music has some flaws. One of them is that it is really a very short book, an extension of the original 1983 short story that won the Nebula. The other is that it is written in 1983 (similar to The God Project) and so the science background is both grandiose and a bit obsolete. But it is a good book and one that ponders on the significance of identity, thought, society and ultimately: the essence of reality. I've read it in about three days, so you have no possible excuse for not trying it.

Saturday, 7 July 2012

Origin, by J. A. Konrath

Book coverI was in the mood for horror, something really disturbing and hopefully on the sci-fi side, and so I went scouring the net for good horror books. Origin, by J.A. Konrath, was suggested by some guy or another, so I started reading it.

The problem is that the book is not much more than an easy thriller. The writing doesn't suck, but neither does it shine; the subject is intriguing, but treated in a screenplay fashion, with not much depth and only a few characters; the book is easy to read and really short; the plot has numerous holes in it. But worst of all, it doesn't really scare! While some of the scenes were - let's say - gory, they were not horror, but rather expected consequences of previous actions.

What is it about? The United States government (who else, tsk, tsk, tsk) has found a strange sarcophagus while excavating the Panama canal. For a century the secret is kept and the subject examined in a specially built facility underground. In the sarcophagus is a massive creature with red skin, hooves, horns and wings. After 100 years of slumber, it awakens. What is it? A devil, THE devil, an alien, a strange prehistoric creature? You will have to read the book in order to get an answer to that question.

Bottom line: I read it in less than a week, didn't hate it, didn't love it, it was good for passing the time, but I would not recommend it for anything but a vacation, to read on the way or in other boring moments.