Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Removing Duplicates from Query

Author  Topic 

velnias2010
Posting Yak Master

125 Posts

Posted - 2011-06-16 : 07:22:39
So I have the following query

SELECT r.userId, r.programmeId, u.email, u.firstName, u.lastName, u.profileId, tblProgrammes.name, u.storeId, r.dateEnded AS lastPass, r.dateExpired
FROM tblUserRepeatHistory AS r INNER JOIN
tblUsers AS u ON r.userId = u.id INNER JOIN
tblProgrammes ON r.programmeId = tblProgrammes.id
WHERE (r.dateExpired IS NOT NULL) AND (u.storeId = @storeId)
GROUP BY r.userId, r.programmeId, u.email, u.firstName, u.lastName, u.profileId, tblProgrammes.name, u.storeId, r.dateEnded, r.dateExpired, r.id
HAVING (DATEDIFF(D, MAX(r.dateExpired), GETDATE() + 31) >= 0)

Which returns the following data

11 22 asdf@asdf.com store-mgr jamie2 1 Deli Food Service 1 5 04/02/2011 09:36:11 10/05/2011 09:36:11
11 22 asdf@asdf.com store-mgr jamie2 1 Deli Food Service 1 5 04/02/2011 09:36:11 10/05/2011 09:36:11
11 22 asdf@asdf.com store-mgr jamie2 1 Deli Food Service 1 5 11/05/2011 09:44:36 10/07/2011 09:44:36
11 22 asdf@asdf.com store-mgr jamie2 1 Deli Food Service 1 5 11/05/2011 09:44:36 10/07/2011 09:44:36
11 22 asdf@asdf.com store-mgr jamie2 1 Deli Food Service 1 5 11/05/2011 09:44:36 10/07/2011 09:44:36

I basically only 1 record returned for each user and programme, where the most recently added one should be returned so i guess by MAX(dateExpired)

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-06-16 : 15:21:46
Would using the row_number function work for you? What I mean is this:

;WITH CTE AS
(
SELECT
ROW_NUMBER() OVER (PARTITION BY r.userId,r.programmeId ORDER BY r.dateExpired DESC) AS RN,
r.userId,
r.programmeId,
u.email,
u.firstName,
u.lastName,
u.profileId,
tblProgrammes.name,
u.storeId,
r.dateEnded AS lastPass,
r.dateExpired
FROM
tblUserRepeatHistory AS r
INNER JOIN tblUsers AS u
ON r.userId = u.id
INNER JOIN tblProgrammes
ON r.programmeId = tblProgrammes.id
WHERE
(r.dateExpired IS NOT NULL)
AND (u.storeId = @storeId)
)
SELECT * FROM CTE WHERE RN = 1;
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-06-16 : 18:47:11
People cannot read your mind, so post your code and clear specs if you really want help. Please post real DDL and not narrative or your own personal programming language. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. Please tell us what SQL product and release you are using. Tell us if you can change the DDL or if you are stuck with it.

What you posted was pretty bad. There is a magical,universal “id” that can identify anything in the universe. Wow! Khabalah numbers in SQL! And tables that have that silly “tbl-”; when the pros see that, we know that we have a sick schema. Another give-away is calling a row a record, like you did. Using Pascal or camelCase instead of the ISO format is also bad; they don't work because of eye movement.

SELECT X.*
FROM (SELECT R.user_id, R.program_id, U.user_email,
U.user_first_name, U.user_last_name, U.profile_id,
P.program_name, U.store_id, extermination_date, R.expiry_date,
MAX(R.expiry_date) OVER (PARTITION BY R.user_id, R.program_id)
AS expiry_date_max
FROM User_Repeat_History AS R,
Users AS U,
Programmes AS P
WHERE R.user_id = U.user_id
AND R.program_id = P.program_id
AND R.expiry_date IS NOT NULL
AND U.store_id = @in_store_id)
AS X
WHERE X.expiry_date = X.expiry_date_max;

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -