Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Complex T-SQL Help

$
0
0

Hello, 
I have a table customer with the following fields: id,email,CreditScore,CreditScoreDate,Insertdatetime
The Email is kind of primary identifier for the table 
The Email can be repeated n number of times - 

For Each unique email I need to know the following:
NumOfRecords ( Count of How many times the Email Appeared in the table)
CreditScoreMin C NULL if there is only one record with NO creditScore)
CreditScoreMax C NULL if there is only one record with NO creditScore)
CreditScoreFirst (if there is first record with NULL creditScore, use second or further when first is NULL,NULL if all are NULL)
CreditScoreLast ( if there is last record with NULL creditScore, use previous to last  or further down when last is NULL,NULL if all are NULL)
NumOfMissingCreditScore ( How many  email enteries have NULL credit score)
DaysSinceLastCreditScore ( if the last creditscore is NULL, use previous to last ,NULL if all are NULL)
DaysSinceFirstCreditScore ( if the first creditscore is NULL, use second or further up, NULL if all are NULL )
DaysBetweenLastTwoScores ( use the last two scores with values, NULL if there is only one record with value, NULL if there are more than 1 records and only 1 record with creditscore)
DifferenceLastTwoCreditScores 
NumOfDistinctCreditScore ( Count of Distinct CreditScores)

Sample SQL :

CREATE TABLE #Customer
( id INT IDENTITY (1,1) PRIMARY KEY NOT NULL,
  Email VARCHAR(128) NOT NULL,
  CreditScore INT NULL,
  CreditScoreDate DATETIME NULL ,
  InsertDateTime DATETIME NOT NULL  )
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abcrops@gmail.com',546,'2007-01-25','2007-01-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abcrops@gmail.com',563,'2007-04-25','2007-04-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abcrops@gmail.com',NULL,NULL,'2007-08-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abcrops@gmail.com',630,'2010-10-25','2010-10-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('drums@gmail.com',768,'2011-03-10','2011-03-10')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('drums@gmail.com',790,'2011-08-30','2011-08-30')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('drums@gmail.com',810,'2012-01-14','2012-01-14')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('drums@gmail.com',810,'2013-03-25','2013-03-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abstone@gmail.com',625,'2013-04-26','2013-04-26')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('abstone@gmail.com',790,'2013-06-30','2013-06-30')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('marble@gmail.com',523,'2005-03-14','2005-03-14')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('marble@gmail.com',724,'2007-06-25','2007-06-25')
  INSERT #Customer (Email,CreditScore,CreditScoreDate,InsertDateTime) VALUES ('marble@gmail.com',714,'2009-06-25','2007-06-25')

Please guide as to how can we get the required metrcis. 

Thanks,



EVA05


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>