Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7817987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:25:14+00:00 2026-06-02T06:25:14+00:00

im using sql server 2005. I have these tables: CREATE TABLE [dbo].[Proyectos2]( [Id] [int]

  • 0

im using sql server 2005.
I have these tables:

CREATE TABLE [dbo].[Proyectos2](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Proyecto] [varchar](255) NULL
)
CREATE TABLE [dbo].[Clientes2](

[Vendedora] [varchar](255) NULL,
[Proyecto] [varchar](255) NULL  )



 insert into proyectos2
values
(
'Proyecto1'
)
insert into proyectos2
values
(
'Proyecto2'
)
insert into clientes2
values
(
'Jessica','Proyecto1'
)
insert into clientes2
values
(
'Jessica','Proyecto1'
)
insert into clientes2
values
(
'Mariel','Proyecto2'
)

then this query:

    Declare @Names As Varchar(Max), @strSQL Varchar(Max);
DECLARE @sum AS varchar(MAX);
Select @Names = Stuff((Select ',' + QuoteName(Proyecto)
From Proyectos2 Group By Proyecto Order by Proyecto For XML Path('')), 1, 1, '');
Select @sum = Stuff(
(
Select ', 1.0 * SUM(' + QuoteName(Proyecto) + ') as ' + QuoteName(Proyecto)
From Proyectos2 
Group By Proyecto 
Order by Proyecto For XML Path('')
), 1, 1, '') + ', SUM(Total) as Total';
Set @strSQL = 'select case when grouping(Vendedora) = 1 then ''Grand Total'' else Vendedora end as Vendedora, ' + @sum + N'
from (
Select Vendedora, ' + @Names +  ', (' + REPLACE(@Names, '],[', '] + [') + ') as Total' + 
' From (Select Vendedora, Proyecto From Clientes2) as P 
 Pivot (Count(Proyecto) For Proyecto in (' + @Names + ')) As Pvt
) as S
group by
    Vendedora
    with rollup';
SET @strSQL = 'WITH CTE AS ( ' + @strSQL + ' ) ' +
              'SELECT Vendedora,cast(floor(Proyecto1) as varchar) Proyecto1,cast(floor(Proyecto2) as varchar)Proyecto2,Total FROM CTE UNION ALL ' +
              'SELECT ''Porcentaje'',cast( ' + REPLACE(@Names, '],[', '] / Total as varchar) Proyecto1, cast([') + ' / Total as varchar), ''100''' +
              '  FROM CTE ' +
              ' WHERE Vendedora = ''Grand Total'';';
print @strSQL;
Execute (@strSQL);
GO

If you execute this code, this is displayed:

Vendedora------Proyecto1--------------Proyecto2-----Total
Jessica-------------2-------------------0--------------2
Mariel--------------0-------------------1--------------1
Grand Total---------2-------------------1--------------3
Percentage----------0.666666666666------0.333333333333---100

I would like something like this:

Vendedora------Proyecto1--------------Proyecto2-----Total
Jessica-------------2-------------------0--------------0.66
Mariel--------------0-------------------1--------------0.33
Grand Total---------2-------------------1--------------1
Percentage----------0.66--------------0.33------------------100

That is, two digits after the decimal for the last row, and also for the total of each row.

Is there a way to do this?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:25:17+00:00Added an answer on June 2, 2026 at 6:25 am

    I’m focusing on your saying that you need the total of each row to be a percentage of the final grand total.

    In order to do that you’re going to need to know what the grand total is before producing each line. You really would be best off doing that in a reporting tool, such as SSRS, ActiveReports, or Crystal Reports.

    But since you asked: this is the approach I would take:

    ETA: rewritten to be more specific. NOTE that this is still an example and needs to be adapted for your situation.

    WITH CTE AS ( <@strSQL> ),
         CTE2 AS (
           SELECT Vendedora, 
                  cast(floor(Proyecto1) as varchar) Proyecto1,
                  cast(floor(Proyecto2) as varchar) Proyecto2,
                  Total 
           FROM   CTE),
           SELECT   Vendedora,
                  Proyecto1,
                  Proyecto2,
                  Total / ( SELECT SUM(Total) FROM CTE2 ) Percentage
           FROM     CTE2    
           UNION  ALL
           SELECT 'Porcentaje',
                   <your two columns>
                   '100'
           FROM   CTE
    

    But this is very messy indeed: your best bet if possible is to just return the data and do the totaling and percentage-calculations in a reporting tool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[using SQL Server 2005] I have a table full of users, I want to
I'm using SQL Server 2005 express I have a datetime field in a table
I am using SQL Server 2005. I have a table with a text column
I'm using SQL Server 2005. I have a table whose row size should be
I'm using SQL SERVER 2005 and have a table which stores challans datewise. It
I am using SQL SERVER 2005. I have a table table1(ID,col1,col2,col3,col4); Now I have
I'm using SQL Server 2005 I have a user. We'll call him User1. He
I'm using SQL Server 2005 and have the following T-SQL statement: DECLARE @MP VARCHAR(500)
I'm using SQL Server 2005. I have a field that must either contain a
I am using SQL Server 2005. I have a requirement to list out all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.