Help with SELECT and COUNT query

  • Thanks for the table structure and sample data.

    SETUP

    DECLARE @PropDet TABLE

    (

    PropId INT PRIMARY KEY,

    UserId UNIQUEIDENTIFIER,

    PropName NVARCHAR(50),

    Resort NVARCHAR(50),

    Col5 NVARCHAR(50),

    Col6 NVARCHAR(50),

    Col7 NVARCHAR(50),

    Col8 NVARCHAR(50),

    Col9 NVARCHAR(50),

    Col10 NVARCHAR(50),

    Col11 NVARCHAR(50),

    Col12 NVARCHAR(50)

    )

    INSERT INTO @PropDet

    (PropId, UserId, PropName, Resort)

    SELECT '6','6EC16A5D-1257-49FA-AA1B-8AC745AF41E5','Villa Marky','Courchevel 1850' UNION ALL

    SELECT '9','E3EEAE25-622E-4C6B-B782-FDA576C9B10B','Villa Mummypenny Elly ','Meribel' UNION ALL

    SELECT '17','6EC16A5D-1257-49FA-AA1B-8AC745AF41E5','Chateaux La La','Meribel'

    DECLARE @Rates TABLE

    (

    RowId INT PRIMARY KEY,

    PropId INT,

    col1 NVARCHAR(50)

    )

    INSERT INTO @Rates

    (RowId, PropId, Col1)

    SELECT '4','6','March' UNION ALL

    SELECT '5','6','April' UNION ALL

    SELECT '6','6','January' UNION ALL

    SELECT '9','17','January' UNION ALL

    SELECT '10','17','February' UNION ALL

    SELECT '11','17','March' UNION ALL

    SELECT '12','17','April' UNION ALL

    SELECT '18','17','May' UNION ALL

    SELECT '19','17','June' UNION ALL

    SELECT '20','17','July' UNION ALL

    SELECT '21','17','August'

    This is Jack's query slightly modified and with the last piece added to it. (And all his formatting screwed up)

    [font="Courier New"]SELECT

           -- P.Col1,

       P.PropID,

            P.UserId,

            --T.Col3,

            --T.Col4,

            DT.count_sum,

       R.RateCount

    FROM

            @PropDet P JOIN

            (

            SELECT

                    --Col1,

           PropID,

                    --COUNT(Col3)+COUNT(Col4)+

           COUNT(Col5)+COUNT(Col6)+COUNT(Col7)+

                    COUNT(Col8)+COUNT(Col9)+COUNT(Col10)+COUNT(Col11)+COUNT(Col12) AS count_sum

            FROM

                    @PropDet

           GROUP BY

                  --  Col1

           PropID

            ) AS DT ON

                    P.PropID= DT.PropID

       -- Added This line for your second query:

       LEFT JOIN (SELECT PropID, COUNT(*) RateCount FROM @Rates GROUP BY PropID) R ON P.PropID = R.PropID

    --WHERE

            --(T.UserId = @userid)

    ORDER BY

            P.PropID, P.UserID

    [/font]

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Well I've got to hand it to you....

    I'm very grateful for your effort with this. It would have taken me forever to figure this out.

    I hope you're always this helpful!

    Once again thanks.

  • Hi Seth,

    Just one more thing i've just come across:

    It looks like if the number of rows retrieved in RateCount is 0 it returns NULL. Is it possible to have the query return "0" if this is the case?

    I tried this but it didn't like the syntax:

    SELECT

    P.PropertyId,

    P.UserId,

    P.PropertyName,

    P.Resort,

    P.GoLiveStatus,

    P.RenewalDate,

    DT.count_ai,

    ISNULL (R.RateCount,'0') as R.RateCount

    FROM

    PropertyDetails2 P JOIN

    ......

    Can you help with this?

    Thanks

  • ISNULL (R.RateCount,'0') as R.RateCount should be ISNULL (R.RateCount, 0) as RateCount

  • It looks like if the number of rows retrieved in RateCount is 0 it returns NULL. Is it possible to have the query return "0" if this is the case?

    I had an ISNULL in there, then removed it again before I posted it. Guess I should have left it! 😎

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

Viewing 5 posts - 16 through 19 (of 19 total)

You must be logged in to reply to this topic. Login to reply