• Hi! As far as I understand you need the opposite, Category and random sample project from each one. Then, just change places Project and ProjectCategory in ChrisM@Work example.

    Also, consider the following. What do you need, "random" or "any of...".

    /*

    SELECT p.*, x.*

    FROM ProjectCategory pc

    CROSS APPLY (

    SELECT TOP 1 *

    FROM Project p

    WHERE pc.ProjectID = p.ProjectID

    ORDER BY NEWID() -- p.ProjectID

    ) x

    */

    use AdventureWorks2008

    go

    set statistics io on

    go

    --"any of", for example ordered by ProductID

    SELECT

    CategoryName = x.Name,

    SampleProductName = pc.Name

    FROM Production.ProductSubcategory pc

    CROSS APPLY (

    SELECT TOP 1 *

    FROM Production.Product p

    WHERE pc.ProductSubcategoryID = p.ProductSubcategoryID

    ORDER BY p.ProductID

    ) x

    /*

    Table 'Product'. Scan count 1, logical reads 395, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    Table 'ProductSubcategory'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    */

    go

    -- pseudo random

    SELECT

    CategoryName = x.Name,

    SampleProductName = pc.Name

    FROM Production.ProductSubcategory pc

    CROSS APPLY (

    SELECT TOP 1 *

    FROM Production.Product p

    WHERE pc.ProductSubcategoryID = p.ProductSubcategoryID

    ORDER BY newid()

    ) x

    /*

    Table 'Product'. Scan count 37, logical reads 555, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    Table 'ProductSubcategory'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

    */

    go

    set statistics io off

    Subquery executes for each of 37 rows.


    I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
    Blog: http://somewheresomehow.ru[/url]
    Twitter: @SomewereSomehow