How do I generate a random number for each row in a T-SQL select?

I need a different random number for each row in my table. The following seemingly obvious code uses the same random value for each row. SELECT table_name, RAND() magic_number FROM information_schema.tables I’d like to get an INT or a FLOAT out of this. The rest of the story is I’m going to use this random … Read more

How to Join to first row

I’ll use a concrete, but hypothetical, example. Each Order normally has only one line item: Orders: OrderGUID OrderNumber ========= ============ {FFB2…} STL-7442-1 {3EC6…} MPT-9931-8A LineItems: LineItemGUID Order ID Quantity Description ============ ======== ======== ================================= {098FBE3…} 1 7 prefabulated amulite {1609B09…} 2 32 spurving bearing But occasionally there will be an order with two line items: … Read more