0

Cordial Saludo, estoy intentando realizar esta subconsulta en SQL SERVER, siguiendo las reglas de la subconsulta, pero no se por que no me arroja el resultado que espero.

la subconsulta es la siguiente:

Select  t.YearlyIncome,ISNULL(t.YearlyIncome,0),
 (
 Select  r.FirstName, r.LastName, r.BirthDate, r.Gender,  r.EmailAddress
   From DimCustomer r where r.YearlyIncome=t.YearlyIncome 
 ) as newIncome
from 
DimCustomer t  where t.YearlyIncome <= 50000 and t.YearlyIncome <>0 and DATEDIFF(year,t.BirthDate,GetDate())<=60

alguna colaboracion para enmendar esto?, estoy utilizando el Motor Sql Server Version 2014.

2
  • 1
    ¿qué resultado esperas y cuál es el que te devuelve?
    – BetaM
    el 24 jul. 2019 a las 0:51
  • Por qué haces una subconsulta a la misma tabla usando la misma columna para relacionarlo? Las subconsultas en el listado de columnas sólo pueden regresar un valor. el 24 jul. 2019 a las 13:16

3 respuestas 3

0

Intente Hacer el Subquery como una expresión pero me genera error, así que mejor incluí el in para que me devuelva los valores de una lista

Select  ISNULL(YearlyIncome,0), FirstName,Lastname, BirthDate,MaritalStatus, Phone,
    SpanishEducation,EnglishEducation,
    DATEDIFF(Year,BirthDate,Getdate()) As Age
       From DimCustomer 
        where Birthdate in (Select Birthdate From DimCustomer Where DATEDIFF(Year,BirthDate,Getdate()) <=35) and 
        YearlyIncome <>0 and YearlyIncome <=50000 and  DATEDIFF(year,BirthDate,GetDate())<=60 and MaritalStatus ='S'
2
  • Sigue sin tener sentido que hagas una subconsulta cuando puedes tener todo en la misma consulta. el 25 jul. 2019 a las 13:15
  • Además tienes 2 condiciones para la edad. el 25 jul. 2019 a las 13:18
0

Cuál es el problema de tener una consulta de forma directa?

SELECT  
    t.YearlyIncome,
    ISNULL(t.YearlyIncome,0),
    t.FirstName, 
    t.LastName, 
    t.BirthDate, 
    t.Gender,  
    t.EmailAddress
FROM DimCustomer t  
WHERE t.YearlyIncome <= 50000 
AND   t.YearlyIncome <> 0 
AND   t.BirthDate    >= DATEADD(year, -60, GetDate())
--AND   t.BirthDate  >= DATEADD(year, -35, GetDate()) --Incluida en la segunda consulta que publicaste
--AND MaritalStatus   ='S'                            --Incluida en la segunda consulta que publicaste

;

0

Cuatro maneras Distintas de realiza la subconsulta.

Select  ISNULL(YearlyIncome,0), FirstName,Lastname, BirthDate,MaritalStatus, Phone,
SpanishEducation,EnglishEducation,
 DATEDIFF(Year,BirthDate,Getdate()) As Age
   From DimCustomer 
    where Birthdate in (Select Birthdate From DimCustomer Where DATEDIFF(Year,BirthDate,Getdate()) <=35) and 
    YearlyIncome <>0 and YearlyIncome <=50000 and  DATEDIFF(year,BirthDate,GetDate())<=60 and MaritalStatus ='S'


Select  ISNULL(YearlyIncome,0), r.FirstName,r.Lastname, r.BirthDate,r.MaritalStatus, r.Phone,
r.SpanishEducation,r.EnglishEducation,
DATEDIFF(Year,r.BirthDate,Getdate()) As Age
  From DimCustomer r
     inner join (Select distinct BirthDate From DimCustomer Where DATEDIFF(Year,BirthDate,Getdate()) <=35) as x
       on  r.BirthDate = x.Birthdate
        Where r.YearlyIncome <>0 and r.YearlyIncome <=50000 and  DATEDIFF(year,r.BirthDate,GetDate())<=60 and r.MaritalStatus ='S'



Select  ISNULL(YearlyIncome,0), FirstName,Lastname, r.BirthDate,MaritalStatus, Phone,
SpanishEducation,EnglishEducation,
DATEDIFF(Year,r.BirthDate,Getdate()) As Age
   From
    DimCustomer r ,
    (Select Distinct BirthDate From DimCustomer Where  DATEDIFF(Year,BirthDate,Getdate()) <=35 ) sq1
     where YearlyIncome <>0 and YearlyIncome <=50000 
      and  DATEDIFF(year,r.BirthDate,GetDate())<=60 and MaritalStatus ='S' and r.BirthDate=sq1.BirthDate




Select ISNULL(y.YearlyIncome,0), y.FirstName,y.Lastname, y.BirthDate,y.MaritalStatus, y.Phone,
y.SpanishEducation,y.EnglishEducation,
DATEDIFF(Year,y.BirthDate,Getdate()) As Age,
(Select distinct r.BirthDate From DimCustomer  r Where r.BirthDate=y.BirthDate   and DATEDIFF(Year, r.BirthDate,Getdate())<=60 ) sq1
   From
    DimCustomer y
     where YearlyIncome <>0 and YearlyIncome <=50000 
      and  DATEDIFF(year,y.BirthDate,GetDate())<=35 and MaritalStatus ='S'

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.