Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Select Statement Where

Tags:

sql

sql-server

I have an event table that has an author field and a presenter field. A person from my person table can be both an author and a presenter of the same event or they can be either the presenter or the author. I am needing to apply filters to the result set based on their person ID and the type or filter they have selected. The filters I have are:

All: This returns any records in which they are either an author or presenter.

AllPresenter: all records as presenter.

AllAuthor: all records as author.

PresenterOnly: only records as presenter and not author.

AuthorOnly: only records as author and not presenter.

PresenterAndAuthorOnly: all records in which they are the presenter and the author.

I currently have a stored proc that uses outer ifs like the one below and I have tried to find a way to combine all of these similar select statements into one. I've not had much luck finding a better solution and I am wondering if I am missing a technique.

If (@filter = 'PandAOnly' or @filter = 'AllP' or @filter = 'AllA')
begin
    Select * from Event 
    Where 
        PresenterId = Case @personId is null then PresenterId else @personId end
        and 
        AuthorId = Case @personId  is null then AuthorId else @personId end
end
else if (@filter = 'All')
begin
    Select * from Event
    Where
        PresenterId = @personId 
        Or
        AuthorId = @personId 
end
else if (@fitler = 'POnly')
begin
   Select * from Event
   Where 
       PresenterId = @personId 
       and
       AuthorId <> @personId 
end
else
begin
    Select * from Event
    Where 
        AuthorId = @personId 
        and 
        PresenterId <> @personId 
end
like image 649
Jesse Jaime Avatar asked Jun 08 '15 18:06

Jesse Jaime


People also ask

What is SELECT from WHERE in SQL?

The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data manipulation language (DML) command.

Can we use SELECT statement in WHERE clause?

You should use the WHERE clause to filter the records and fetching only the necessary records. The WHERE clause is not only used in the SELECT statement, but it is also used in the UPDATE, DELETE statement, etc., which we would examine in the subsequent chapters.

Can we use WHERE in SQL?

The SQL WHERE ClauseThe WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

How does the WHERE statement work in SQL?

In Structured Query Language (SQL) statements, WHERE clauses limit what rows the given operation will affect. They do this by defining specific criteria, referred to as search conditions, that each row must meet in order for it to be impacted by the operation.


1 Answers

Select * from Event 
Where 
   (
        ((@personId is null) OR (PresenterId =@personId ))
        and 
        ((@personId  is null) OR (AuthorId = @personId))
        AND 
        (@filter = 'PandAOnly' or @filter = 'AllP' or @filter = 'AllA')
    )
OR 
  (
       (PresenterId = @personId 
        Or
        AuthorId = @personId )
    AND (@filter = 'All')
  )
OR 
  (
       PresenterId = @personId 
       and
       AuthorId <> @personId 
       and 
       @fitler = 'POnly'
  )
OR 
 (
        AuthorId = @personId 
        and 
        PresenterId <> @personId 
       and 
       @fitler = 'AOnly'
 )

NOTE

I would rather stick to the stored procedure, the execution plan for the above query will be scary :)

like image 197
M.Ali Avatar answered Oct 19 '22 11:10

M.Ali