我在两个表中使用内部连接,就像下面这样
SELECT DISTINCT c.cedula,
c.idclave_nombre,
o.fechaproxima_llamada,
c.ubicacion,
c.notas,
c.nombres,
c.apellidos,
c.sector,
c.empresa,
c.acreedor,
c.analista,
c.sucursal,
o.motivo,
c.ps,
c.fecha_nacimiento,
c.sexo,
DATE_PART(
'YEAR',
AGE(CURRENT_DATE, c.fecha_nacimiento)
) as edad
FROM tbl_clientes c
INNER JOIN tbl_observaciones o ON c.idclave_nombre = o.idclave_nombre
字符串
但我得到重复的,关键字应该是列'c. cedula':
+----------+--------------------+--------------------+---------+-----+--------+---------+---------+--------------+----------+--------+--------+------------------+----+----------------+----+----+
|cedula |idclave_nombre |fechaproxima_llamada|ubicacion|notas|nombres |apellidos|sector |empresa |acreedor |analista|sucursal|motivo |ps |fecha_nacimiento|sexo|edad|
+----------+--------------------+--------------------+---------+-----+--------+---------+---------+--------------+----------+--------+--------+------------------+----+----------------+----+----+
|8-796-2306|AANANKHA 25-05-22-AZ|2024-01-15 |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |OFRECER EXTRAMONEY|null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |? |null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |INTRO |null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |OJO |null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |PAGO |null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |PRELI |null|1986-04-27 |F |37 |
|8-796-2306|AANANKHA 25-05-22-AZ|null |PAGO |null |AANANKHA|QUARLESS |E PRIVADA|BAC CREDOMATIC|PANACREDIT|ROGELIO |null |null |null|1986-04-27 |F |37 |
+----------+--------------------+--------------------+---------+-----+--------+---------+---------+--------------+----------+--------+--------+------------------+----+----------------+----+----+
型
我使用内部联接是因为我需要在表中显示两列。
假设我只想从这7条记录中显示第一条,即o.fechaproxima_llamada不为null的记录,但有时会出现另一个客户端的o.fechaproxima_llamada为null的情况,但它只需要显示这两条记录中的一条。
+---------+-----------------+--------------------+---------+-----+------------+-------------+---------+-------+----------+--------+---------+----------+----+----------------+----+----+
|cedula |idclave_nombre |fechaproxima_llamada|ubicacion|notas|nombres |apellidos |sector |empresa|acreedor |analista|sucursal |motivo |ps |fecha_nacimiento|sexo|edad|
+---------+-----------------+--------------------+---------+-----+------------+-------------+---------+-------+----------+--------+---------+----------+----+----------------+----+----+
|8-984-255| NOEMI 13-09-18-V|null |D |null |NOEMI YOANIS|RAMOS SALDAÑA|E PRIVADA|null |FINANCOMER|RAIZA |LOS ANDES|ATENDIDO |null|1999-07-12 |F |24 |
|8-984-255| NOEMI 13-09-18-V|null |D |null |NOEMI YOANIS|RAMOS SALDAÑA|E PRIVADA|null |FINANCOMER|RAIZA |LOS ANDES|PRELIMINAR|null|1999-07-12 |F |24 |
+---------+-----------------+--------------------+---------+-----+------------+-------------+---------+-------+----------+--------+---------+----------+----+----------------+----+----+
型
在这种情况下,你建议我做什么?
谢谢你
当我使用不带DISCTINCT的查询时,我得到了这个结果,见第一个表CEDINCT = 8-712-1266。
+----------+------------------+--------------------+---------+-----+-------------+-------------+---------+---------------------------------------------+-----------+--------+--------+--------+----+----------------+----+----+
|cedula |idclave_nombre |fechaproxima_llamada|ubicacion|notas|nombres |apellidos |sector |empresa |acreedor |analista|sucursal|motivo |ps |fecha_nacimiento|sexo|edad|
+----------+------------------+--------------------+---------+-----+-------------+-------------+---------+---------------------------------------------+-----------+--------+--------+--------+----+----------------+----+----+
|8-712-1266| ALEXIS 28-10-16-K|null |EE |null |ALEXIS MIGUEL|MARCHADO |E PRIVADA|TRANSPORTE Y MULTISERVICIOS,S.A. ( DON LEE )|null |null |null |ATENDIDO|null|1971-11-12 |M |51 |
|8-766-118 | FRANCISCO 31-8-11|null |CERRADO |null |FRANCISCO |ROMERO CEDEÑO|E PRIVADA|CONALVIA |INSTACREDIT|JHANMARY|AVE PERU|ATENDIDO|S |1983-01-05 |M |40 |
|8-766-118 | FRANCISCO 31-8-11|null |CERRADO |null |FRANCISCO |ROMERO CEDEÑO|E PRIVADA|CONALVIA |INSTACREDIT|JHANMARY|AVE PERU|ATENDIDO|S |1983-01-05 |M |40 |
+----------+------------------+--------------------+---------+-----+-------------+-------------+---------+---------------------------------------------+-----------+--------+--------+--------+----+----------------+----+----+
型
但当我使用
SELECT DISTINCT on (c.cedula)
c.cedula,
c.idclave_nombre,
o.fechaproxima_llamada,
c.ubicacion,
c.notas,
c.nombres,
c.apellidos,
c.sector,
c.empresa,
c.acreedor,
c.analista,
c.sucursal,
o.motivo,
c.ps,
c.fecha_nacimiento,
c.sexo,
DATE_PART(
'YEAR',
AGE(CURRENT_DATE, c.fecha_nacimiento)
) as edad
FROM tbl_clientes c
INNER JOIN tbl_observaciones o ON c.idclave_nombre = o.idclave_nombre
型
列cedula是这样改变的,它就像它删除-并添加0作为第一个字符:
+---------+--------------------+--------------------+---------+-----+-----------------+-------------------+---------+--------------------+----------+--------+---------+------+----+----------------+----+----+
|cedula |idclave_nombre |fechaproxima_llamada|ubicacion|notas|nombres |apellidos |sector |empresa |acreedor |analista|sucursal |motivo|ps |fecha_nacimiento|sexo|edad|
+---------+--------------------+--------------------+---------+-----+-----------------+-------------------+---------+--------------------+----------+--------+---------+------+----+----------------+----+----+
|- |ALFONSO 8-08-07 |null |I |X23- |ALFONSO |CHAN |JUBILADO |null |null |null |null |null |null|1944-04-07 |M |79 |
|008491059|ALEXANDER 17-02-20-V|null |D |null |ALEXANDER ENRIQUE|VILLALOBOS VALENCIA|E PRIVADA|SERVICIOS SIGMA, S.A|LA EXITOSA|null |CALIDONIA|INTRO |null|1972-03-07 |M |51 |
|02225038 |LUIS 21-06-22-L |null |D |null |LUIS ALONSO |ROSALES ALVARADO |E PRIVADA|PPG |null |null |null |INTRO |null|1980-03-09 |M |43 |
+---------+--------------------+--------------------+---------+-----+-----------------+-------------------+---------+--------------------+----------+--------+---------+------+----+----------------+----+----+
型
1条答案
按热度按时间lxkprmvk1#
当您使用DISTINCT时,它将考虑所有检索到的列的组合。
你可以使用DISTINCT ON来强制一些关键点,像这样:
字符串
它将考虑第一次出现重复的情况,您可以通过使用ORDER条件来定义首先选择谁来解决这个问题。