SELECT
GETDATE() AS CurrentDate,
DATEADD(d, + 30, GETDATE()) AS MonthlyCurrentDate,
r.ntid AS 'Student ID',
A.A_NBR AS '5 Digit Assoc A #',
CASE
WHEN r.ntid LIKE '%s%' OR r.ntid LIKE '%swa%' OR r.ntid LIKE '%sid%' OR r.ntid LIKE '%shi%' OR r.ntid LIKE '%sor%' OR r.ntid LIKE '%sak%' OR r.ntid LIKE '%sco%'
THEN 'LSP'
WHEN r.ntid LIKE '%0A%' OR r.ntid LIKE '%0a%' OR r.ntid LIKE '%a0%' OR r.ntid LIKE '%A0%'
THEN 'Agent'
END AS 'Student Type Dept',
CASE
WHEN WC.[Last Name] IS NOT NULL THEN WC.[Last Name]
ELSE PARSENAME(REPLACE(r.name, ' ', '.'), 2)
END AS 'A Last Name',
CASE
WHEN WC.[First Name] IS NOT NULL THEN WC.[First Name]
ELSE PARSENAME(REPLACE(r.name, ' ', '.'), 1)
END AS 'A First Name',
CASE
WHEN s.LAST_NM IS NOT NULL THEN s.LAST_NM
ELSE PARSENAME(REPLACE(r.name, ' ', '.'), 2)
END AS 'LSP Last Name',
CASE
WHEN s.FIRST_NM IS NOT NULL THEN s.FIRST_NM
ELSE PARSENAME(REPLACE(r.name, ' ', '.'), 1)
END AS 'LSP First Name',
CASE
--WHEN WC.[EMAIL ADDRESS] IS NOT NULL THEN WC.[EMAIL ADDRESS]
WHEN AG.Email IS NOT NULL THEN AG.Email + '@company.COM'
ELSE WC.[A ID]+ '@company.COM'
END AS 'Email',
c.EndDateTimeView,
r.dateTimeRegistered,
c.name AS 'Course Name' ,
CASE
WHEN r.attended = 'true' AND c.Name LIKE '%Associates%' THEN 'Yes'
WHEN r.attended = 'true' AND c.Name LIKE '%Day 3%' OR c.Name LIKE '%Retain Bundle Grow%' THEN 'Yes'
WHEN r.attended = 'true' AND c.Name LIKE '%Property University%' THEN 'Yes'
ELSE 'No'
END AS 'Attended',
CASE
WHEN c.Name LIKE '%Associates%' THEN 'AF Financial University - Associates'
WHEN c.Name LIKE '%Retain Bundle Grow%' OR c.Name LIKE '%Day 3%' THEN 'Trusted Advisor University'
WHEN c.Name LIKE '%Property University%' THEN 'Property University'
ELSE 'No workshops attended'
END AS 'Workshops',
CASE
WHEN c.Name NOT LIKE '%Associates%' THEN 'Need to attend : AF University Associates'
ELSE 'Attended: AF University Associates'
END AS 'Need Workshops AFU',
CASE
WHEN c.Name NOT LIKE '%Day 3%' OR c.Name NOT LIKE '%Retain Bundle Grow%'THEN 'Need to attend: Trusted Advisor University'
ELSE 'Attended: Trusted Advisor University'
END AS 'Need SS Workshop',
CASE
WHEN c.Name NOT LIKE '%Property University%' THEN 'Neet to attend: Advanced Auto and Property University'
ELSE 'Attended: Advanced Auto and Property University'
END AS 'Need PU Workshop',
CASE
WHEN s.StartDate IS NOT NULL THEN s.StartDate
ELSE '2001-01-01 00:00:00.0000000'
END AS 'LSP Start Date'
FROM dbo._registration AS r
INNER JOIN wcr_staff.dbo.Affiliations AS a ON a.REP_NT_ID = r.ntid
INNER JOIN dbo._course AS C ON C.CourseID = r.courseNumber
INNER JOIN wcr_ag.dbo.tblA AS AG ON AG.AGN_A_NBR = a.A_NBR
LEFT OUTER JOIN wcr_staff.dbo.Staff AS s ON s.SALES_PARTY_ID = A.SALES_PARTY_ID
LEFT OUTER JOIN WC_Policy_Universe.dbo.['List2014$'] WC ON AG.AGN_NBR = WC.[A ID]
WHERE (NOT (r.ntid = ' '))
AND(AG.AGN_STATUS_CD = 3)
AND AG.Type = 'Primary'
AND (a.IsActive = 1)
AND (c.EndDateTimeView >= '2012-01-01')
OR c.EndDateTimeView >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())+31,0)
AND c.EndDateTimeView < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())+32,0)
OR c.EndDateTimeView BETWEEN CONVERT(DATETIME, FLOOR(CONVERT(FLOAT,GETDATE())))
AND CONVERT(DATETIME,FLOOR(CONVERT(FLOAT,GETDATE()))+1)
ORDER BY A.AG_NBR ASC
2015-02-23 09:02:19.643 2015-03-25 09:02:19.643 scofvx0e 001937 LSP HOERY ROBERT SPELLMAN AMANDA A001937@Company.COM 2015-02-23 17:00:00.000 2015-02-12 13:01:36.000 Sales University: Day 1 Auto No No workshops attended Need to attend : AF University Associates Need to attend: Trusted Advisor University Neet to attend: Advanced Auto and Property University 2015-01-07 00:00:00.0000000
2015-02-23 09:02:19.643 2015-03-25 09:02:19.643 scofvx0e 001937 LSP HOERY ROBERT SPELLMAN AMANDA A001937@Company.COM 2015-02-23 17:00:00.000 2015-02-12 13:01:36.000 Sales University: Day 1 Auto No No workshops attended Need to attend : AF University Associates Need to attend: Trusted Advisor University Neet to attend: Advanced Auto and Property University 2015-01-07 00:00:00.0000000
2015-02-23 09:02:19.643 2015-03-25 09:02:19.643 scofvx0e 001937 LSP HOERY ROBERT SPELLMAN AMANDA A001937@Company.COM 2015-02-23 17:00:00.000 2015-02-12 13:01:36.000 Sales University: Day 1 Auto No No workshops attended Need to attend : AF University Associates Need to attend: Trusted Advisor University Neet to attend: Advanced Auto and Property University 2015-01-07 00:00:00.0000000
ASKER
2015-02-23 12:57:06.497 2015-03-25 12:57:06.497 sco07y0b 026640 LSP BRAINARD MICHAEL HESSE DONNA boss@company.COM No Attended: AF University Associates Need to attend: Trusted Advisor University Neet to attend: Advanced Auto and Property University 2014-06-06 00:00:00.0000000
2015-02-23 12:57:06.497 2015-03-25 12:57:06.497 sco07y0b 026640 LSP BRAINARD MICHAEL HESSE DONNA boss@company.COM No Need to attend : AF University Associates Need to attend: Trusted Advisor University Neet to attend: Advanced Auto and Property University 2014-06-06 00:00:00.0000000
2015-02-23 12:57:06.497 2015-03-25 12:57:06.497 sco07y0b 026640 LSP BRAINARD MICHAEL HESSE DONNA boss@company.COM Yes Need to attend : AF University Associates Need to attend: Trusted Advisor University Attended: Advanced Auto and Property University 2014-06-06 00:00:00.0000000
WHERE r.ntid <> ' '
AND AG.AGN_STATUS_CD = 3
AND AG.Type = 'Primary'
AND a.IsActive = 1
AND (
c.EndDateTimeView >= '2012-01-01'
OR
( c.EndDateTimeView >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 31, 0)
AND c.EndDateTimeView < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 32, 0)
)
OR
( c.EndDateTimeView >= CONVERT(datetime, FLOOR(CONVERT(float, GETDATE())))
AND c.EndDateTimeView < CONVERT(datetime, FLOOR(CONVERT(float, GETDATE())) + 1)
)
)
I also never use between for date ranges, and I notice that in one case you don't either. so I have applied that to the other.ASKER
SELECT DISTINCT
r.ntid AS 'Student ID'
FROM dbo._registration AS r
INNER JOIN wcr_staff.dbo.Affiliations AS a ON a.REP_NT_ID = r.ntid
INNER JOIN dbo._course AS C ON C.CourseID = r.courseNumber
INNER JOIN wcr_ag.dbo.tblA AS AG ON AG.AGN_A_NBR = a.A_NBR
LEFT OUTER JOIN wcr_staff.dbo.Staff AS s ON s.SALES_PARTY_ID = A.SALES_PARTY_ID
LEFT OUTER JOIN WC_Policy_Universe.dbo.['List2014$'] WC ON AG.AGN_NBR = WC.[A ID]
WHERE r.ntid <> ' '
AND AG.AGN_STATUS_CD = 3
AND AG.Type = 'Primary'
AND a.IsActive = 1
AND (
c.EndDateTimeView >= '2012-01-01'
OR
( c.EndDateTimeView >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 31, 0)
AND c.EndDateTimeView < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()) + 32, 0)
)
OR
( c.EndDateTimeView >= CONVERT(datetime, FLOOR(CONVERT(float, GETDATE())))
AND c.EndDateTimeView < CONVERT(datetime, FLOOR(CONVERT(float, GETDATE())) + 1)
)
)
ASKER
ASKER
SELECT
CASE
WHEN a.IsActive = 1 OR AG.AGN_STATUS_CD = 3 THEN r.ntid
END AS 'Student ID',
CASE
WHEN a.IsActive = 1 AND r.ntid LIKE '%s%' OR r.ntid LIKE '%swa%' OR r.ntid LIKE '%sid%' OR r.ntid LIKE '%shi%' OR r.ntid LIKE '%sor%' OR r.ntid LIKE '%sak%' OR r.ntid LIKE '%sco%'
THEN 'L'
END AS 'Student Type Dept',
CASE
WHEN AG.AGN_STATUS_CD = 3 AND WC.[Last Name] IS NOT NULL THEN WC.[Last Name]
END AS 'A Last Name',
CASE
WHEN AG.AGN_STATUS_CD = 3 AND WC.[First Name] IS NOT NULL THEN WC.[First Name]
END AS 'A First Name',
CASE
WHEN a.IsActive = 1 AND s.LAST_NM IS NOT NULL THEN s.LAST_NM
END AS 'L Last Name',
CASE
WHEN a.IsActive = 1 AND s.FIRST_NM IS NOT NULL THEN s.FIRST_NM
END AS 'L First Name',
CASE
WHEN AG.Email IS NOT NULL THEN AG.Email + '@company.COM'
END AS 'Email',
c.EndDateTimeView,
r.dateTimeRegistered,
CASE
WHEN a.IsActive = 1 AND r.ntid LIKE '%s%' OR r.ntid LIKE '%swa%' OR r.ntid LIKE '%sid%' OR r.ntid LIKE '%shi%'
OR r.ntid LIKE '%sor%' OR r.ntid LIKE '%sak%' OR r.ntid LIKE '%sco%'
THEN
CASE
WHEN r.attended = 'true' AND c.Name LIKE '%Associates%'
OR c.Name LIKE '%Day 3%'
OR c.Name LIKE '%Property University%'
THEN 'Attended ' + c.Name
ELSE 'Need s to attended ' + c.Name
END
END AS 'Attended',
CASE
WHEN a.IsActive = 1 AND s.StartDate IS NOT NULL THEN s.StartDate
ELSE '2001-01-01 00:00:00.0000000'
END AS 'Start Date'
FROM (
select distinct ntid, attended, dateTimeRegistered, courseNumber from dbo._registration) AS r
LEFT OUTER JOIN (
select distinct REP_NT_ID, IsActive, A_NBR, SALES_PARTY_ID from wcr_staff.dbo.Affiliations) AS a ON a.REP_NT_ID = r.ntid
LEFT OUTER JOIN (
select distinct EndDateTimeView, Name, CourseID, StartDateTimeView from dbo._course) AS C ON r.courseNumber = C.CourseID
LEFT OUTER JOIN (
select distinct Email, AGN_A_NBR, AGN_STATUS_CD from wcr_agents.dbo.tblAgents) AS AG ON AG.AGN_AGENT_NBR = a.A_NBR
LEFT OUTER JOIN (
select distinct LAST_NM , FIRST_NM, SALES_PARTY_ID, StartDate from wcr_staff.dbo.Staff) AS s ON A.SALES_PARTY_ID = s.SALES_PARTY_ID
LEFT OUTER JOIN (
select distinct [A ID], [First Name], [Last Name] from WC_Policy_Universe.dbo.['List2014$']
) WC ON AG.AGN_AGENT_NBR = WC.[A ID]
WHERE (C.Name LIKE '%Bachelors%' OR C.Name LIKE '%Associates%'
OR C.Name LIKE '%Property University%' OR C.Name LIKE '%Day 3%')
AND r.ntid <> ' '
AND a.IsActive = 1
AND (
c.StartDateTimeView >= DATEADD(yyyy, -2, GETDATE())
)
ORDER BY a.A_NBR ASC
I am still getting duplicate data seebelow:scofvx0e L H ROBERT S AMANDA A001937@allstate.COM 2015-04-16 17:00:00.000 2015-02-12 13:20:01.000 Attended Advanced University 2015-01-07 00:00:00.0000000
scofvx0e L H ROBERT S AMANDA A001937@allstate.COM 2015-05-19 17:00:00.000 2015-02-12 13:26:32.000 Need s to attended Financial University - Associates 2015-01-07 00:00:00.0000000
scofvx0e L H ROBERT S AMANDA A001937@allstate.COM 2015-02-25 17:00:00.000 2015-02-12 13:01:55.000 Attended Sales University: Day 3 Sales 2015-01-07 00:00:00.0000000
Still not sure how to stop duplicate data.....
FROM (
select distinct ntid, attended, dateTimeRegistered, courseNumber from dbo._registration) AS r
LEFT OUTER JOIN (
select distinct REP_NT_ID, IsActive, A_NBR, SALES_PARTY_ID from wcr_staff.dbo.Affiliations) AS a ON a.REP_NT_ID = r.ntid
LEFT OUTER JOIN (
select distinct EndDateTimeView, Name, CourseID, StartDateTimeView from dbo._course) AS C ON r.courseNumber = C.CourseID
LEFT OUTER JOIN (
select distinct Email, AGN_A_NBR, AGN_STATUS_CD from wcr_agents.dbo.tblAgents) AS AG ON AG.AGN_AGENT_NBR = a.A_NBR
LEFT OUTER JOIN (
select distinct LAST_NM , FIRST_NM, SALES_PARTY_ID, StartDate from wcr_staff.dbo.Staff) AS s ON A.SALES_PARTY_ID = s.SALES_PARTY_ID
LEFT OUTER JOIN (
select distinct [A ID], [First Name], [Last Name] from WC_Policy_Universe.dbo.['List2014$']
) WC ON AG.AGN_AGENT_NBR = WC.[A ID]
SQL does repeat information, this is normal (and necessary)
scofvx0e L H ROBERT Attended Advanced University
scofvx0e L H ROBERT Need s to attended Financial University - Associates
scofvx0e L H ROBERT Attended Sales University: Day 3 Sales
SQL does NOT do the following
scofvx0e L H ROBERT Attended Advanced University
Need s to attended Financial University - Associates
Attended Sales University: Day 3 Sales
ASKER
ASKER
ASKER
Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.
TRUSTED BY
what if you first insert into a temp table and then select distinct from that:
Open in new window