Sum and percentage

  • I have table where I am storing survey's result

    CREATE TABLE [tblQuiz_Respuesta] (

    [Qr_Qu_Id] [int] NOT NULL ,

    [Qr_Id] [int] NOT NULL ,

    [Qr_Texto] [nvarchar] (800) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

    [Qr_Respuestas] [int] NULL

    ) ON [PRIMARY]

    GO

    I need to make a view where I could calculate the percent of each Qr_Respuestas per Qr_Id for Qr_Qu_Id (what a way of saying things !

    Example:

    Qr_Qu_Id Qr_Id Qr_Texto Qr_Respuestas

    20031280013Quellón25

    20031280012Castro197

    20031280011Ancud364

    The result should be

    20031280013Quellón4.27%

    20031280012Castro33.62%

    20031280011Ancud62.12%


    Jean-Luc
    www.corobori.com

  • I'm not completely sure what you want, but this query produces the result you are after:

    insert [tblQuiz_Respuesta] select 2003128001, 3, 'Quellón', 25

    insert [tblQuiz_Respuesta] select 2003128001, 2, 'Castro', 197

    insert [tblQuiz_Respuesta] select 2003128001, 1, 'Ancud', 364

    select r.*, r.Qr_Respuestas/dt.total from tblQuiz_Respuesta r

    inner join

    (

    select Qr_Qu_Id, cast(sum(Qr_Respuestas) as decimal(5)) as total from tblQuiz_Respuesta

    group by Qr_Qu_Id

    )

    dt

    on r.Qr_Qu_Id = dt.Qr_Qu_Id

  • Looking fine, thanks


    Jean-Luc
    www.corobori.com

Viewing 3 posts - 1 through 2 (of 2 total)

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