Home All Groups Group Topic Archive Search About

Query with dinamic columns

Author
7 Dec 2008 10:11 PM
dandi
I've a table with the details (rows) of each invoice

CREATE TABLE [dbo].[DocumentiPassiviDettaglio](
     [IDDocumentoPassivoDettaglio] [int] IDENTITY(1,1) NOT NULL,
     [IDDocumentoPassivo] [int] NULL,
     [IDPianoDeiConti] [int] NULL,
     [Imponibile] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_Imponibile]  DEFAULT ((0)),
     [ImponibileNonSoggettoIVA] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_ImponibileNonSoggettoIVA]  DEFAULT ((0)),
     [CassaPrevidenziale] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_CassaPrevidenziale]  DEFAULT ((0)),
     [IvaDetraibile] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_IvaDetraibile]  DEFAULT ((0)),
     [IvaNonDetraibile] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_IvaNonDetraibile]  DEFAULT ((0)),
     [Ritenuta] [decimal](18, 2) NULL CONSTRAINT
[DF_DocumentiPassiviDettaglio_Ritenuta]  DEFAULT ((0)),
     [DataVersamentoIva] [datetime] NULL,
     [DataVersamentoRitenuta] [datetime] NULL,
     [Descrizione] [varchar](max) COLLATE Latin1_General_CI_AS NULL,
  CONSTRAINT [PK_DocumentiPassiviDettaglio] PRIMARY KEY CLUSTERED
(
     [IDDocumentoPassivoDettaglio] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]



and a second table, linked to  DocumentiPassiviDettaglio with
DocumentiPassiviDettaglio.IDDocumentoPassivoDettaglio =
DocumentiPassiviDettaglioLdb.IDDocumentoPassivoDettaglio

in this second table I've one or many rows for each record of
DocumentiPassiviDettaglio, this for allow me to split each value of
DocumentiPassiviDettaglio in more records

CREATE TABLE [dbo].[DocumentiPassiviDettaglioLdb](
     [IDDocumentoPassivoDettaglioLdb] [int] IDENTITY(1,1) NOT NULL,
     [IDDocumentoPassivoDettaglio] [int] NULL,
     [IDLdB] [int] NULL,
     [Valore] [decimal](10, 2) NULL,
  CONSTRAINT [PK_DocumentiPassiviDettaglioLdb] PRIMARY KEY CLUSTERED
(
     [IDDocumentoPassivoDettaglioLdb] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

I've a third table named LineeDiBusiness that allow me to know the name
of each record that i find in DocumentiPassiviDettaglioLdb

with  LineeDiBusiness.IDLdB = DocumentiPassiviDettaglioLdb.IDLdB

this join allow to know the name (LineeDiBusiness.DescrizioneLdB) of
each record in DocumentiPassiviDettaglioLdb

CREATE TABLE [dbo].[LineeDiBusiness](
     [IDLdB] [int] IDENTITY(1,1) NOT NULL,
     [DescrizioneLdB] [varchar](200) COLLATE Latin1_General_CI_AS NULL,
  CONSTRAINT [PK_LineeDiBusiness] PRIMARY KEY CLUSTERED
(
     [IDLdB] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


now....

I've to use a sum of items (values) of  DocumentiPassiviDettaglio
table... I've to create a SUM of this fields

  (DocumentiPassiviDettaglio.Imponibile +
DocumentiPassiviDettaglio.ImponibileNonSoggettoIVA +
DocumentiPassiviDettaglio.IvaDetraibile +
DocumentiPassiviDettaglio.IvaNonDetraibile) AS Total



and...

using  the Total (the SUM of fields) of DocumentiPassiviDettaglioLdb
table I have to spilt the value in % using the content of
DocumentiPassiviDettaglioLdb



for example, I suppose to have  (for a specific
DocumentiPassiviDettaglio table record)

   (DocumentiPassiviDettaglio.Imponibile +
DocumentiPassiviDettaglio.ImponibileNonSoggettoIVA +
DocumentiPassiviDettaglio.IvaDetraibile +
DocumentiPassiviDettaglio.IvaNonDetraibile) AS Total

  = 1500



  and in the DocumentiPassiviDettaglioLdb with the
IDDocumentoPassivoDettaglio that has the same value of my
DocumentiPassiviDettaglio.IDDocumentoPassivoDettaglio these records

IDX - IDDocumentoPassivoDettaglioY - 1 - 20
IDZ - IDDocumentoPassivoDettaglioY - 3 - 50
IDW - IDDocumentoPassivoDettaglioY - 5 - 30

(with 20, 50 30 that are used as % numbers)
(with 1, 3, 5 that are the IDLdB of  LineeDiBusiness table)



  I want to see these columns in my query

  DocumentiPassiviDettaglio.Descrizione - Total (the sum of fields) -
LineeDiBusiness.DescrizioneLdB (of Ldb 1) -
LineeDiBusiness.DescrizioneLdB (of Ldb 2) -
LineeDiBusiness.DescrizioneLdB (of Ldb 3) -
LineeDiBusiness.DescrizioneLdB (of Ldb 4) -
LineeDiBusiness.DescrizioneLdB (of Ldb 5) ... all
LineeDiBusiness.DescrizioneLdB that I've in LineeDiBusiness table



the values

  1500 * 20 / 100

  1500 * 50 / 100

  1500 * 30 / 100

under the columns of LineeDiBusiness.DescrizioneLdB (with IDLdB = 1),
LineeDiBusiness.DescrizioneLdB (with IDLdB = 3),
LineeDiBusiness.DescrizioneLdB (with IDLdB = 5)


Thanks for help me!!!!

Author
8 Dec 2008 4:10 AM
Plamen Ratchev
You simply need to join the three tables and preform the calculation:

SELECT C.DescrizioneLdB,
       (A.Imponibile +
        A.ImponibileNonSoggettoIVA +
        A.IvaDetraibile +
        A.IvaNonDetraibile) * B.Valore / 100 AS perc
FROM DocumentiPassiviDettaglio AS A
JOIN DocumentiPassiviDettaglioLdb AS B
   ON A.IDDocumentoPassivoDettaglio = B.IDDocumentoPassivoDettaglio
JOIN LineeDiBusiness AS C
   ON B.IDLdB = C.IDLdB;

--
Plamen Ratchev
http://www.SQLStudio.com
Are all your drivers up to date? click for free checkup

Author
8 Dec 2008 12:34 PM
dandi
Plamen Ratchev ha scritto:
Show quoteHide quote
> You simply need to join the three tables and preform the calculation:
>
> SELECT C.DescrizioneLdB,
>       (A.Imponibile +
>        A.ImponibileNonSoggettoIVA +
>        A.IvaDetraibile +
>        A.IvaNonDetraibile) * B.Valore / 100 AS perc
> FROM DocumentiPassiviDettaglio AS A
> JOIN DocumentiPassiviDettaglioLdb AS B
>   ON A.IDDocumentoPassivoDettaglio = B.IDDocumentoPassivoDettaglio
> JOIN LineeDiBusiness AS C
>   ON B.IDLdB = C.IDLdB;
>


Thanks for help me ... but I would like in column:


DocumentiPassiviDettaglio.Descrizione
Total (the sum of fields)
LineeDiBusiness.DescrizioneLdB (of Ldb 1)
LineeDiBusiness.DescrizioneLdB (of Ldb 2)
LineeDiBusiness.DescrizioneLdB (of Ldb 3)
LineeDiBusiness.DescrizioneLdB (of Ldb 4)
LineeDiBusiness.DescrizioneLdB (of Ldb 5)
....
all LineeDiBusiness.DescrizioneLdB that I've in LineeDiBusiness table

I wanto to have 2 (Descrizione + Total) columns + X columns for all
records of LineeDiBusiness table

in you example I see "DescrizioneLdB" fields in rows

Thanks for help me!!!
Author
8 Dec 2008 2:59 PM
Plamen Ratchev
Seems you need some form of pivoting. Here is one method (requires SQL
server 2005/2008). It will pivot on 3 values, you can extend this to the
max possible values from the LineeDiBusiness tables.

SELECT IDDocumentoPassivoDettaglio,
        MAX(CASE WHEN rk = 1 THEN DescrizioneLdB END) AS desc1,
        MAX(CASE WHEN rk = 1 THEN perc END) AS perc1,
        MAX(CASE WHEN rk = 2 THEN DescrizioneLdB END) AS desc2,
        MAX(CASE WHEN rk = 2 THEN perc END) AS perc2,
        MAX(CASE WHEN rk = 3 THEN DescrizioneLdB END) AS desc2,
        MAX(CASE WHEN rk = 3 THEN perc END) AS perc3
FROM (
SELECT A.IDDocumentoPassivoDettaglio,
        C.DescrizioneLdB,
       (A.Imponibile +
        A.ImponibileNonSoggettoIVA +
        A.IvaDetraibile +
        A.IvaNonDetraibile) * B.Valore / 100 AS perc,
        ROW_NUMBER() OVER(PARTITION BY A.IDDocumentoPassivoDettaglio
                          ORDER BY C.DescrizioneLdB, C.IDLdB) AS rk
FROM DocumentiPassiviDettaglio AS A
JOIN DocumentiPassiviDettaglioLdb AS B
   ON A.IDDocumentoPassivoDettaglio = B.IDDocumentoPassivoDettaglio
JOIN LineeDiBusiness AS C
   ON B.IDLdB = C.IDLdB) AS T
GROUP BY IDDocumentoPassivoDettaglio;

--
Plamen Ratchev
http://www.SQLStudio.com
Author
8 Dec 2008 5:34 PM
dandi
Excuse me...

In column I want to see the name of the LineeDiBusiness (are fixed...)

an in rows all values ... under each column

Thanks!
Author
8 Dec 2008 6:54 PM
Plamen Ratchev
Then you probably need dynamic pivoting. Here is one way to do it (SQL
Server 2005/2008):

DECLARE @cols NVARCHAR(2000);
SELECT @cols =
        STUFF((SELECT DISTINCT '],[' + DescrizioneLdB
               FROM LineeDiBusiness
               FOR XML PATH('')
               ), 1, 2, '') + ']';

DECLARE @query NVARCHAR(4000);
SET @query =
N'SELECT IDDocumentoPassivoDettaglio, ' + @cols +
N'FROM (SELECT A.IDDocumentoPassivoDettaglio, ' +
              N'C.DescrizioneLdB, ' +
              N'(A.Imponibile + ' +
              N' A.ImponibileNonSoggettoIVA + ' +
              N' A.IvaDetraibile + ' +
              N' A.IvaNonDetraibile) * B.Valore / 100 AS perc ' +
       N'FROM DocumentiPassiviDettaglio AS A ' +
       N'JOIN DocumentiPassiviDettaglioLdb AS B ' +
       N'  ON A.IDDocumentoPassivoDettaglio =
B.IDDocumentoPassivoDettaglio ' +
       N'JOIN LineeDiBusiness AS C ' +
       N'  ON B.IDLdB = C.IDLdB) AS F ' +
N'PIVOT ' +
N'(MAX(perc) FOR DescrizioneLdB IN (' + @cols + N')) AS P;';

EXEC(@query);

--
Plamen Ratchev
http://www.SQLStudio.com
Author
9 Dec 2008 10:14 PM
dandi
Thanks Thanks Thanks now I try!!
Author
9 Dec 2008 11:28 PM
steve dassin
Check out the RAC utility for dynamic crosstabs.
www.rac4sql.net

www.beyondsql.blogspot.com

Show quoteHide quote
"dandi" <nom***@nomail.it> wrote in message
news:u$OCOukWJHA.4168@TK2MSFTNGP06.phx.gbl...
> Thanks Thanks Thanks now I try!!
Author
12 Dec 2008 9:47 PM
dandi
Plamen Ratchev ha scritto:
> Then you probably need dynamic pivoting. Here is one way to do it (SQL
> Server 2005/2008):
>
> DECLARE @cols NVARCHAR(2000);
> SELECT @cols =
>        STUFF((SELECT DISTINCT '],[' + DescrizioneLdB
....

Fantastic!!!! You are a Guru!!!

It works!!!

Thanks Thanks Thanks


Now I'm studing your solution! Greeeeat

Bookmark and Share