我有这段代码:
select CAST(OPERATOR_NAME AS NVARCHAR(MAX)) as 'Utilizador',
TERMINAL_DESCRIPTION as 'Terminal',
DOCUMENT_DATE as 'Data de Inicio',
PAYMENT_LIMIT_DATE as 'Data de Fim',
ORIGIN_WAREHOUSE_NAME as 'Loja',
DOCUMENT_TYPE_DESCRIPTION as 'Tipos de Documentos'
from MRD_DOCUMENT_HEADER
GROUP BY CAST(OPERATOR_NAME AS NVARCHAR(MAX))
但它给了我这个错误:
列
MRD_DOCUMENT_HEADER.TERMINAL_DESCRIPTION
在选择列表中无效,因为它不包含在聚合函数或GROUP BY
子句中。
我尝试VARCHAR(100)
和MAX
,但这也不起作用。
我怎样才能使这个工作?
更新
SELECT DISTINCT
Utilizador = OPERATOR_NAME
, Terminal = TERMINAL_DESCRIPTION
, [Data de Inicio] = DOCUMENT_DATE
, [Data de Fim] = PAYMENT_LIMIT_DATE
, Loja = ORIGIN_WAREHOUSE_NAME
, [Tipos de Documentos] = DOCUMENT_TYPE_DESCRIPTION
FROM dbo.MRD_DOCUMENT_HEADER
WHERE
MRD_DOCUMENT_HEADER.DOCUMENT_TYPE_DESCRIPTION = (@DOCUMENT_TYPE_DESCRIPTION)
OR MRD_DOCUMENT_HEADER.DOCUMENT_DATE = (@DOCUMENT_DATE)
OR MRD_DOCUMENT_HEADER.PAYMENT_LIMIT_DATE =(@PAYMENT_LIMIT_DATE)
OR MRD_DOCUMENT_HEADER.CORPORATION_ID = (@CORPORATION_ID)
答案 0 :(得分:0)
您必须将这些列放在GROUP BY
子句中。它会解决你的问题。只需使用以下代码:
select CAST(OPERATOR_NAME AS NVARCHAR(MAX)) as 'Utilizador',
TERMINAL_DESCRIPTION as 'Terminal',
DOCUMENT_DATE as 'Data de Inicio',
PAYMENT_LIMIT_DATE as 'Data de Fim',
ORIGIN_WAREHOUSE_NAME as 'Loja',
DOCUMENT_TYPE_DESCRIPTION as 'Tipos de Documentos'
from MRD_DOCUMENT_HEADER
GROUP BY
CAST(OPERATOR_NAME AS NVARCHAR(MAX)),
TERMINAL_DESCRIPTION, DOCUMENT_DATE,
PAYMENT_LIMIT_DATE,
ORIGIN_WAREHOUSE_NAME,
DOCUMENT_TYPE_DESCRIPTION
答案 1 :(得分:0)
试试这个 -
SELECT
Utilizador = OPERATOR_NAME
, Terminal = TERMINAL_DESCRIPTION
, [Data de Inicio] = DOCUMENT_DATE
, [Data de Fim] = PAYMENT_LIMIT_DATE
, Loja = ORIGIN_WAREHOUSE_NAME
, [Tipos de Documentos] = DOCUMENT_TYPE_DESCRIPTION
FROM dbo.MRD_DOCUMENT_HEADER
GROUP BY
OPERATOR_NAME
, TERMINAL_DESCRIPTION
, DOCUMENT_DATE
, PAYMENT_LIMIT_DATE
, ORIGIN_WAREHOUSE_NAME
, DOCUMENT_TYPE_DESCRIPTION
DOCUMENT_TYPE_DESCRIPTION
或尝试更优雅的样本 -
SELECT DISTINCT
Utilizador = OPERATOR_NAME
, Terminal = TERMINAL_DESCRIPTION
, [Data de Inicio] = DOCUMENT_DATE
, [Data de Fim] = PAYMENT_LIMIT_DATE
, Loja = ORIGIN_WAREHOUSE_NAME
, [Tipos de Documentos] = DOCUMENT_TYPE_DESCRIPTION
FROM dbo.MRD_DOCUMENT_HEADER
上次评论的更新:
SELECT
Utilizador = OPERATOR_NAME
, Terminal = MAX(TERMINAL_DESCRIPTION)
, [Data de Inicio] = MAX(DOCUMENT_DATE)
, [Data de Fim] = MAX(PAYMENT_LIMIT_DATE)
, Loja = MAX(ORIGIN_WAREHOUSE_NAME)
, [Tipos de Documentos] = MAX(DOCUMENT_TYPE_DESCRIPTION)
FROM dbo.MRD_DOCUMENT_HEADER
GROUP BY OPERATOR_NAME
更新2以获取最后评论:
SELECT DISTINCT
Utilizador = OPERATOR_NAME
, Terminal = TERMINAL_DESCRIPTION
, [Data de Inicio] = CONVERT(VARCHAR(10), DOCUMENT_DATE, 120)
, [Data de Fim] = PAYMENT_LIMIT_DATE
, Loja = ORIGIN_WAREHOUSE_NAME
, [Tipos de Documentos] = DOCUMENT_TYPE_DESCRIPTION
FROM dbo.MRD_DOCUMENT_HEADER t
WHERE t.DOCUMENT_TYPE_DESCRIPTION = @DOCUMENT_TYPE_DESCRIPTION
OR t.DOCUMENT_DATE = @DOCUMENT_DATE
OR t.PAYMENT_LIMIT_DATE = @PAYMENT_LIMIT_DATE
OR t.CORPORATION_ID = @CORPORATION_ID
答案 2 :(得分:0)
如果在SQL中使用GROUP BY
子句,那么在SELECT列表中,您只能使用GROUP BY
中使用的相同列,而必须的任何其他列包含在聚合函数中,如count()
,min()
,max()
,avg()
等。
这是SQL92 / SQL99的要求。但是,一些SQL引擎非常宽容,例如MySQL和SQLite允许违反此规则。但MS SQL Server,Oracle和PostgreSQL相当严格。
答案 3 :(得分:0)
在此SELECT中,似乎只是字母数字值,因此您可以设置ORDER BY OPERATOR_NAME。
GROUP BY语句与聚合函数结合使用,将结果集分组为一列或多列(MIN,MAX,COUNT)。