Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   MS SQL Server (https://www.clubdelphi.com/foros/forumdisplay.php?f=23)
-   -   Sql (https://www.clubdelphi.com/foros/showthread.php?t=73819)

Richard Lopez G 18-05-2011 18:07:23

Sql
 
Estimada Caro.

Si tienes razon me trae dos datos el cual es ahi es donde se cae el query, necesito su ayuda para resolver este tema gracias.

Urgente

Caro 18-05-2011 18:12:58

Cita:

Empezado por Richard Lopez G (Mensaje 400499)
Si tienes razon me trae dos datos el cual es ahi es donde se cae el query

Entonces ahí esta el error, debes cambiar la consulta para que te devuelva un solo registro.

Cita:

Empezado por Richard Lopez G (Mensaje 400499)
necesito su ayuda para resolver este tema gracias.

Como te dije, has un pequeño ejemplo con las dos tablas, sus respectivos campos, algunos datos y en base a eso que resultado quieres obtener, sería mas facil para nosotros ayudarte de esa forma.

Saluditos

Richard Lopez G 18-05-2011 18:22:39

Sql
 
Estimada Caro.

Este es mi query que e desarrollado pero justo en el select anidado se me cae ya que cuando coapara y por desir el primer select en inicio se da cuenta que el usuario a marcado 2 veces es ahi donde se cae.

Dame una ayudadita sisisis.

select
r.*,convert(char(10),GETDATE(),112) FechaActual ,
(
select [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='Entrada'and [LogDate] ='2011/05/17')Entrada,
(
select [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='InicioAlmuerzo'and [LogDate] ='2011/05/17')InicioAlmuerzo,
(
select [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='FinAlmuerzo'and [LogDate] ='2011/05/17')FinAlmuerzo,
(
select [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='Salida'and [LogDate] ='2011/05/17')Salida
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='Sistemas'
and Estado_per='Activo')r;

Richard Lopez G 18-05-2011 18:24:22

Sql
 
Estimado este es el query que e desarrollado con tu ayuda pero me trae errores que me sale en la fila 5 y 24.

select
r.*,convert(char(10),GETDATE(),112) FechaActual,
(
SelectDistinct T.UserID, T.UserName,CaseWhen h.fKey ='Entrada'then h.LogTime else 0 end)As Entrada,
CaseWhen h.fKey ='InicioAlmuerzo'then h.LogTime else 0 end)As Inicio,
CaseWhen h.fKey ='FinAlmuerzo'then h.LogTime else 0 end)As Fin,
CaseWhen h.fKey ='Salida'then h.LogTime else 0 end)As Salida
From History T
LeftJoin LogTime h on T.UserID = H.UserID
Where H. LogDate ='2011/05/17')
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[NomCargo_per]Cargo
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='sistemas'
and Estado_per='Activo')r;

Casimiro Noteví 18-05-2011 18:32:12

Te recuerdo de nuevo nuestras normas y recuerda poner etiquetas al código.
Gracias por tu colaboración.





.

Richard Lopez G 18-05-2011 18:34:19

Sql
 
Estimado este es el query que e desarrollado con tu ayuda pero me trae errores que me sale en la fila 5 y 24.

select
r.*,convert(char(10),GETDATE(),112) FechaActual,
(
SelectDistinct T.UserID, T.UserName,CaseWhen h.fKey ='Entrada'then h.LogTime else 0 end)As Entrada,
CaseWhen h.fKey ='InicioAlmuerzo'then h.LogTime else 0 end)As Inicio,
CaseWhen h.fKey ='FinAlmuerzo'then h.LogTime else 0 end)As Fin,
CaseWhen h.fKey ='Salida'then h.LogTime else 0 end)As Salida
From History T
LeftJoin LogTime h on T.UserID = H.UserID
Where H. LogDate ='2011/05/17')
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[NomCargo_per]Cargo
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='sistemas'
and Estado_per='Activo')r;

Richard Lopez G 18-05-2011 18:36:44

Sql
 
Amigo este codigo se diseño con tu ayuda pero me sale error en la fila 5 y 24 ayuda
select
r.*,convert(char(10),GETDATE(),112) FechaActual,
(
SelectDistinct T.UserID, T.UserName,CaseWhen h.fKey ='Entrada'then h.LogTime else 0 end)As Entrada,
CaseWhen h.fKey ='InicioAlmuerzo'then h.LogTime else 0 end)As Inicio,
CaseWhen h.fKey ='FinAlmuerzo'then h.LogTime else 0 end)As Fin,
CaseWhen h.fKey ='Salida'then h.LogTime else 0 end)As Salida
From History T
LeftJoin LogTime h on T.UserID = H.UserID
Where H. LogDate ='2011/05/17')
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[NomCargo_per]Cargo
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='sistemas'
and Estado_per='Activo')r;

Richard Lopez G 18-05-2011 18:38:32

Sql
 
Amigo este es el query que e desarrollado con tu ayuda pero me trae errores que me sale en la fila 5 y 24.

select
r.*,convert(char(10),GETDATE(),112) FechaActual,
(
SelectDistinct T.UserID, T.UserName,CaseWhen h.fKey ='Entrada'then h.LogTime else 0 end)As Entrada,
CaseWhen h.fKey ='InicioAlmuerzo'then h.LogTime else 0 end)As Inicio,
CaseWhen h.fKey ='FinAlmuerzo'then h.LogTime else 0 end)As Fin,
CaseWhen h.fKey ='Salida'then h.LogTime else 0 end)As Salida
From History T
LeftJoin LogTime h on T.UserID = H.UserID
Where H. LogDate ='2011/05/17')
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[NomCargo_per]Cargo
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='sistemas'
and Estado_per='Activo')r;

Casimiro Noteví 18-05-2011 18:42:12

Te recuerdo de nuevo nuestras normas y recuerda poner etiquetas al código.
Gracias por tu colaboración.





.

Caro 18-05-2011 18:45:24

Hola de nuevo, de esta forma es un poco dificil ayudarte, no sabemos la estructura de tus tablas, que datos tienes, ni tampoco cuales son esos dos registros que te devuelve la subconsulta, por lo que tampoco sabemos cual de ellos te sirve o si tienes que hacer alguna operación entre ellos.........

Si haces lo que te dije mas arriba de pasarnos un ejemplo, prodriamos inclusive plantear de otra forma la consulta.

Lo que se me ocurre, es que le aplique Top 1 a cada una de tus subconsultas, pero nose si sera el resultado que esperas.

Código SQL [-]
select
r.*,convert(char(10),GETDATE(),112) FechaActual ,
Top 1 (select [LogTime]from History as h 
                 where [UserID]= r.FingerID AND [FKey]='Entrada'and [LogDate] ='2011/05/17') Entrada,
Top 1 (select [LogTime]from History as h 
                 where [UserID]= r.FingerID AND [FKey]='InicioAlmuerzo'and [LogDate] ='2011/05/17') InicioAlmuerzo,
Top 1 (select [LogTime]from History as h 
                 where [UserID]= r.FingerID AND [FKey]='FinAlmuerzo'and [LogDate] ='2011/05/17') FinAlmuerzo,
Top 1 (select [LogTime]from History as h 
                 where [UserID]= r.FingerID AND [FKey]='Salida'and [LogDate] ='2011/05/17') Salida
FROM
(SELECT [Cod_per]CodPersonal ...................................
.............................................................................

Saluditos

Richard Lopez G 18-05-2011 18:49:07

Sql
 
Estimada para que me puedas ayudar que es lo que necesitas pues.

A la espera de tu comentario.

ya que es de suma urgencia sisisisis.

Caro 18-05-2011 18:50:47

Richard, por favor no repitas el mismo mensaje varias veces y lee lo que te ha indicado Casimiro.

Saluditos

Richard Lopez G 18-05-2011 18:53:35

Sql
 
Hice las pruebas con tu ayuda pero me sale error con el top 1.

ayudame gracias es urgente

Caro 18-05-2011 18:57:24

:o me he equipocado donde he puesto el Top 1.

Código SQL [-]
selectr.*,convert(char(10),GETDATE(),112) FechaActual ,
 (select Top 1 [LogTime]from History as h
      where [UserID]= r.FingerID AND [FKey]='Entrada'and [LogDate] ='2011/05/17') Entrada,
 (select Top 1 [LogTime]from History as h       
     where [UserID]= r.FingerID AND [FKey]='InicioAlmuerzo'and [LogDate] ='2011/05/17') InicioAlmuerzo,
 (select Top 1 [LogTime]from History as h         
     where [UserID]= r.FingerID AND [FKey]='FinAlmuerzo'and [LogDate] ='2011/05/17') FinAlmuerzo,
 (select Top 1 [LogTime]from History as h          
     where [UserID]= r.FingerID AND [FKey]='Salida'and [LogDate] ='2011/05/17') Salida
FROM(SELECT [Cod_per]CodPersonal .....................................................
...........................................................

Saluditos

abelg 19-05-2011 19:48:25

Si te esta retornando más de un valor en la sub consulta no dara nunca, creo que a cada una de estas:

Código SQL [-]
(select TOP 1 [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='Entrada'and [LogDate] ='2011/05/17') Entrada

el Top 1 indica que sacara solo una fila lo mismo para Salida, etc y con un order by de la hora sacaras el que quieras el primer intento o el segundo. si tendría la estructura de tus tablas sería más facil para mi explicarte.
salu2

Richard Lopez G 19-05-2011 21:13:47

Sql2
 
Estimadoas
utilice el top 1 y me resulto muhas gracias por la ayuda, ahora si yo quiero ponerle un order by en mi ultima linea me sale que el order by no trabaja en sub consultas ya que quieero ordenarlo por apellido a la espera de su ayuda.

select
r.*,convert(char(10),GETDATE(),112) FechaActual ,
(
select Top 1 [LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='Entrada'and [LogDate] ='2011/05/17')Entrada,
(
select Top 1[LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='InicioAlmuerzo'and [LogDate] ='2011/05/17')InicioAlmuerzo,
(
select Top 1[LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='FinAlmuerzo'and [LogDate] ='2011/05/17')FinAlmuerzo,
(
select Top 1[LogTime]from History as h where [UserID]= r.FingerID AND [FKey]='Salida'and [LogDate] ='2011/05/17')Salida
FROM
(SELECT [Cod_per]CodPersonal
,[ApePat_per]ApellidoPaterno
,[ApeMat_per]ApellidoMaterno
,[Nom_per] NombrePersonal
,[NomProy_per]Proyecto
,[NomArea_per]Area
,[Estado_per]Estado
,[Cod_Finger]FingerID
FROM T_Personal
where [NomProy_per]='BMP'
and NomArea_per='Sistemas'
and Estado_per='Activo')r;


La franja horaria es GMT +2. Ahora son las 22:06:40.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi