vendredi 26 février 2016

How to pivot tables with out aggregate functions and with boolean fields from three tables

    How to pivot tables with out aggregate functions and with boolean fields from three tables

    CREATE TABLE [dbo].[TmpData](
        [TmpDataID] [int] IDENTITY(1,1) NOT NULL, 
        [TmpDataName] [nvarchar](255) NOT NULL,
        [StatusID] [int] NOT NULL,
        [SignOffDate] [datetime] NULL,
        [SignOffUserID] [int] NULL,
        [SignOffComments] [nvarchar](max) NULL,
        [IsClosed] [bit] NOT NULL,
        [ClosedDate] [datetime] NULL,
    CONSTRAINT [PK_dbo.TmpData] PRIMARY KEY CLUSTERED 
    (
        [TmpDataID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

here is the sample data TmpDataID | TmpDataName |StatusID |SignOffDate |SignOffUserID |SignOffComments |IsClosed |ClosedDate 1 |TmpData |1 |00:00.0 |25 |xthda |1 |00:00.0
2 |TmpDtata2 |1 |00:00.0 |22 |eeeee |1 |00:00.0

    CREATE TABLE [dbo].[TmpSectionData](
        [TmpSectionDataID] [int] IDENTITY(1,1) NOT NULL,
        [TmpDataID] [int] NOT NULL,--foreign key
        [TmpSection] [nvarchar](255) NULL,
        [TmpSectionOverall] [bit] NOT NULL,
        [TmpSectionCusID] [int] NULL,
        [TmpSectionOutcome] [bit] NULL,
        [TmpSectionHeading] [bit] NOT NULL,
        [TmpSectionActive] [bit] NOT NULL,
        [TmpSectionHasOutcome] [bit] NOT NULL,
     CONSTRAINT [PK_dbo.TmpSectionData] PRIMARY KEY CLUSTERED 
    (
        [TmpSectionDataID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

here is the sample data

TmpSectionDataID |TmpDataID |TmpSection |TmpSectionOverall |TmpSectionCusID |TmpSectionOutcome |TmpSectionHeading |TmpSectionActive |TmpSectionHasOutcome| 1 |1 |TmpSection1 |1 |1 |1 |1 |0 |0 2 |1 |TmpSection2 |1 |5 |1 |0 |1 |1 3 |1 |TmpSection3 |0 |6 |0 |0 |1 |1 CREATE TABLE [dbo].[TmpSampleData]( [TmpSampleDataID] [int] IDENTITY(1,1) NOT NULL, [TmpSectionDataID] [int] NOT NULL, --foreighn key [TmpSampleText] nvarchar NULL, [TmpSampleValidation] [bit] NOT NULL, [TmpSampleCusConID] [int] NULL, [TmpSampleQPRID] [int] NOT NULL, [TmpSampleHasOutcome] [bit] NOT NULL, CONSTRAINT [PK_dbo.TmpSampleData] PRIMARY KEY CLUSTERED ( [TmpSampleDataID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

And the data needed in the format:

    TmpDataID   TmpDataName StatusID    SignOffDate SignOffUserID   SignOffComments IsClosed    ClosedDate  TmpSection1+TmpSectionOverall   TmpSection1+TmpSectionCusID TmpSection1+TmpSectionOutcome   TmpSection1+TmpSectionHeading   TmpSection1+TmpSectionActive    TmpSection1+TmpSectionHasOutcome    TmpSection2+TmpSectionOverall   TmpSection2+TmpSectionCusID TmpSection2+TmpSectionOutcome   TmpSection2+TmpSectionHeading   TmpSection2+TmpSectionActive    TmpSection2+TmpSectionHasOutcome    TmpSection3+TmpSectionOverall   TmpSection3+TmpSectionCusID TmpSection3+TmpSectionOutcome   TmpSection3+TmpSectionHeading   TmpSection3+TmpSectionActive    TmpSection3+TmpSectionHasOutcome
    1   TmpData 1   00:00.0 25  xthda   1   00:00.0 1   1   1   1   0   0   1   5   1   0   1   1   0   6   0   0   1   1

All the column data from the first table along with the data from second table TmpSectionData based on TmpSection which should come as header and the related data one after another in a single row. If i use joins i will get multiple columns and i have seen some of the question with PIVOT where they use aggregate function. here the columns i wanted to pivot is bool fields. 

I have gone through some of the answers by few of stackoverflow collegues with pivot single column and there all use a amount field and made use of aggregate funstions.those didnt give me a clear idea on how i can work with more tables and more columns with one column as base for all the pivot. I tried to implement http://ift.tt/1QkZfsV . There also i am not getting the requeired format. Hope some one can help me with it.I have seen some of bluefeet answers and realized i may be able to achive the same with pivot in sql.I could manage to get one transpose one column

Aucun commentaire:

Enregistrer un commentaire