Boa tarde,
Estou com um problema na hora de montar uma Query...
Essa é aminha consulta:
SELECT o.cod_oferta, e.cod_curso, nome_curso, o.vagas
FROM dbo.tbl_curso_oferta o, dbo.tbl_curso e, dbo.tbl_tipo_evento t
WHERE o.ativo = 1 and o.status <> 3 AND data_expiracao >= Getdate() AND e.cod_tipo_evento = 2
AND o.cod_curso = e.cod_curso
AND e.cod_tipo_evento = t.cod_tipo_evento
AND o.vagas < (SELECT count(o.cod_oferta) FROM dbo.tbl_matricula WHERE
tbl_matricula.cod_oferta = o.cod_oferta)
ORDER BY nome_curso
Mais está aparecendo esse erro, pois não devo colocar SELECT Count(o.cod_oferta) para comparar a quantidade de registros, mas preciso saber quantos cursos ainda estão disponiveis:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Alguém pode ajudar???
Obrigado.
As tabelas:
CREATE TABLE [dbo].[tbl_curso_oferta] (
[cod_oferta] [int] IDENTITY (1, 1) NOT NULL ,
[cod_curso] [int] NOT NULL ,
[data_inicio] [datetime] NOT NULL ,
[data_final] [datetime] NOT NULL ,
[horario] [varchar] (100) NOT NULL ,
[vagas] [int] NOT NULL ,
[valor] [money] NOT NULL ,
[instrutor] [int] NOT NULL ,
[curriculo_instrutor] [text] NOT NULL ,
[local_curso] [varchar] (250) NOT NULL ,
[obs] [text] NOT NULL ,
[periodo_inscricao] [varchar] (50) NOT NULL ,
[informacoes] [varchar] (1000) NOT NULL ,
[status] [int] NOT NULL ,
[data_expiracao] [datetime] NOT NULL ,
[ativo] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[tbl_matricula] (
[cod_participante] [int] NOT NULL ,
[cod_oferta] [int] NOT NULL ,
[cod_empresa] [int] NOT NULL ,
[cargo] [varchar] (100) NOT NULL ,
[estudante_profissional] [char] (1) NOT NULL ,
[valor_pago] [money] NOT NULL ,
[forma_pagto] [varchar] (100) NOT NULL ,
[obs] [text] NOT NULL ,
[data_matricula] [datetime] NOT NULL ,
[ativo] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[tbl_tipo_evento] (
[cod_tipo_evento] [int] IDENTITY (1, 1) NOT NULL ,
[tipo_evento] [varchar] (50) NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[tbl_curso] (
[cod_curso] [int] IDENTITY (1, 1) NOT NULL ,
[nome_curso] [varchar] (250) NOT NULL ,
[carga_horaria] [varchar] (50) NOT NULL ,
[conteudo] [text] NOT NULL ,
[obs] [text] NOT NULL ,
[data_cadastro] [datetime] NOT NULL ,
[publico_alvo] [varchar] (200) NOT NULL ,
[objetivo] [text] NOT NULL ,
[cod_tipo_evento] [int] NOT NULL ,
[ativo] [int] NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO