Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query Problem

Posted on 2007-03-30
4
Medium Priority
?
253 Views
Last Modified: 2010-06-21
Hi there,

I am trying to retrieve the last 100 distinct job positions that have attended an event. Right now the query returns the last 100 job positions but they are not distinct, because I have an order by event start date in my query clause. Is there anyway else I can retrieve this information.
I've posted the query below. I've tried a group by and that didn't work either because of the event start date.

Query:
SELECT DISTINCT TOP 100      WebUser.Wuser_Position, Event.Evnt_Start_Date
FROM                     Event INNER JOIN
                  Invoice ON Event.Evnt_ID = Invoice.Evnt_ID INNER JOIN
                  InvoiceItems ON Invoice.Inv_ID = InvoiceItems.Inv_ID INNER JOIN
                  WebUser ON InvoiceItems.Wuser_ID = WebUser.Wuser_ID
WHERE                  (WebUser.Wuser_Position IS NOT NULL) AND (WebUser.Wuser_Enabled = 1) AND
                  (NOT (UPPER(WebUser.Wuser_First_Name) LIKE '%TEST%')) AND
                  (NOT (UPPER(WebUser.Wuser_Last_Name) LIKE '%TEST%'))                  
ORDER BY             Event.Evnt_Start_Date DESC
0
Comment
Question by:quanmac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 18824480
you do want group by
you don't want distinct
you need to select max dates...
and for performance avoid using NOT...

SELECT  TOP 100      WebUser.Wuser_Position,max(Event.Evnt_Start_Date) as start_date
FROM                     Event
 INNER JOIN           Invoice
ON Event.Evnt_ID = Invoice.Evnt_ID
 INNER JOIN           InvoiceItems
ON Invoice.Inv_ID = InvoiceItems.Inv_ID
 INNER JOIN       (select *
                                     ,case when UPPER(Wuser_first_name) like '%TEST%' then 1 else 0 end as FN
         ,case when UPPER(Wuser_last_name) like '%TEST%' then 1 else 0 end as LN
                              from   WebUser
                             where Wuser_Position IS NOT NULL
                                 AND Wuser_Enabled = 1

 ON InvoiceItems.Wuser_ID = WebUser.Wuser_ID
WHERE     FN = 0 and LN=0
group by webuser.wuser_position        
ORDER BY   Event.Evnt_Start_Date DESC
 
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18824534
Is this SQL Server?

Don't use a single query.  Use a stored procedure.

CREATE PROCEDURE get100Positions
AS
CREATE TABLE #tempPos (
position <whatever type Wuser_position is> NOT NULL PRIMARY KEY
)

DECLARE p1 READ-ONLY CURSOR FOR
SELECT
WebUser.Wuser_Position, Event.Evnt_Start_Date
FROM                    
Event
INNER JOIN
Invoice
ON
Event.Evnt_ID = Invoice.Evnt_ID
INNER JOIN
InvoiceItems ON Invoice.Inv_ID = InvoiceItems.Inv_ID
INNER JOIN
WebUser
ON InvoiceItems.Wuser_ID = WebUser.Wuser_ID
WHERE                  
(
WebUser.Wuser_Position IS NOT NULL
)
AND
(
WebUser.Wuser_Enabled = 1
)
AND
(
NOT
(
UPPER(WebUser.Wuser_First_Name) LIKE '%TEST%'
)
)
AND
(
NOT
(
UPPER(WebUser.Wuser_Last_Name) LIKE '%TEST%'
)
)                  
ORDER BY            
Event.Evnt_Start_Date DESC

OPEN p1

DECLARE @position <type of Wuser_Position>
DECLARE @startDate DATETIME

FETCH NEXT FROM p1 INTO @postion, @startDate
WHILE @@FETCH_STATUS = 0 AND (SELECT COUNT COUNT(*) FROM #tempPOS) < 100)
BEGIN
    IF (SELECT COUNT(*) FROM #tempPOS WHERE position = @position) = 0
    BEGIN
        INSERT INTO #tempPos (position) VALUES (@position)
    END
END

CLOSE p1
DEALLOCATE p1

RETURN
GO
0
 
LVL 23

Expert Comment

by:Christopher Kile
ID: 18824541
Whoops!

Add before RETURN:

SELECT * FROM #tempPos
0
 
LVL 3

Author Comment

by:quanmac
ID: 18824552
Thanks Lowfatspread, it worked like a charm
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ready to get certified? Check out some courses that help you prepare for third-party exams.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Viewers will learn how the fundamental information of how to create a table.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question