Skip to main content
adicionou 1 caractere ao conteúdo
Fonte Link
user6026
user6026

Se a sua tabela de Faltas tiver o identificador dessa linha, dá para usar assim:

Modelo da Tabela (Create Table)

CREATE TABLE [dbo].[Faltas](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nome] [nvarchar](50) NULL,
    [Data] [date] NULL,
    [Local] [nvarchar](10) NULL,
    [Situacao] [nvarchar](20) NULL,
 CONSTRAINT [PK_Faltas] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SQL geradaexemplo:

SELECT Nome, 
       Local, 
       [1] as [13/06/2014],
       [2] as [14/06/2014],
       [3] as [16/06/2014]
FROM Faltas
PIVOT (MAX(Situacao) for Id  in ([1],[2],[3])) p order by 1

Exemplo: SQLFiddle

Se a sua tabela de Faltas tiver o identificador dessa linha, dá para usar assim:

Modelo da Tabela (Create Table)

CREATE TABLE [dbo].[Faltas](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nome] [nvarchar](50) NULL,
    [Data] [date] NULL,
    [Local] [nvarchar](10) NULL,
    [Situacao] [nvarchar](20) NULL,
 CONSTRAINT [PK_Faltas] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SQL gerada:

SELECT Nome, 
       Local, 
       [1] as [13/06/2014],
       [2] as [14/06/2014],
       [3] as [16/06/2014]
FROM Faltas
PIVOT (MAX(Situacao) for Id  in ([1],[2],[3])) p order by 1

Exemplo: SQLFiddle

Se a sua tabela de Faltas tiver o identificador dessa linha, dá para usar assim:

Modelo da Tabela (Create Table)

CREATE TABLE [dbo].[Faltas](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nome] [nvarchar](50) NULL,
    [Data] [date] NULL,
    [Local] [nvarchar](10) NULL,
    [Situacao] [nvarchar](20) NULL,
 CONSTRAINT [PK_Faltas] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SQL exemplo:

SELECT Nome, 
       Local, 
       [1] as [13/06/2014],
       [2] as [14/06/2014],
       [3] as [16/06/2014]
FROM Faltas
PIVOT (MAX(Situacao) for Id  in ([1],[2],[3])) p order by 1

Exemplo: SQLFiddle

Fonte Link
user6026
user6026

Se a sua tabela de Faltas tiver o identificador dessa linha, dá para usar assim:

Modelo da Tabela (Create Table)

CREATE TABLE [dbo].[Faltas](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Nome] [nvarchar](50) NULL,
    [Data] [date] NULL,
    [Local] [nvarchar](10) NULL,
    [Situacao] [nvarchar](20) NULL,
 CONSTRAINT [PK_Faltas] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

SQL gerada:

SELECT Nome, 
       Local, 
       [1] as [13/06/2014],
       [2] as [14/06/2014],
       [3] as [16/06/2014]
FROM Faltas
PIVOT (MAX(Situacao) for Id  in ([1],[2],[3])) p order by 1

Exemplo: SQLFiddle