Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7805229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:09:32+00:00 2026-06-02T02:09:32+00:00

I have a simple query like so: SELECT * FROM orders that returns all

  • 0

I have a simple query like so:

SELECT * FROM orders

that returns all orders, simple.

I would like to add optional parameters to my query.

DECLARE @group INT;
SET @group = 1 --IT1

SELECT * FROM orders WHERE group = isnull(@group, group)

This also works fine.

My problem is that i need a second parameter like so:

DECLARE @group INT;
SET @group = 1 --IT1
DECLARE @department VARCHAR(8);
SET @department = 'IT'

SELECT * FROM orders WHERE /*and here add limitation with department*/ group = isnull(@group, group)

My orders table has column GROUP, but no column DEPARTMENT.

Situation looks like so:
I have 2 comboboxes. One for departments, second for group.
In first I have options:

  • All
  • IT
  • CALL CENTRE

Second is populated when user select something from combo1.
If he selects IT the group combo box will have options:

  • All
  • IT1
  • IT2

If user select from first combo department and then group it is easy because I then check only group.
Problem is when he wants all orders from department IT.

Here is how groups are assigned to departments

  • IT – 1,2,5
  • CALL CENTRE – 4,6
  • PRINT – 3

Any ideas?

ADDED:

I have created something like that:

DECLARE @group INT;
SET @group = 1 --IT1
DECLARE @department VARCHAR(8);
SET @department = 'IT'

SELECT * FROM orders WHERE
(@department IS NULL OR (@department='IT' AND group IN (1,2,5)) OR (@department='PRINT' AND group =3))
     AND
     (@group IS NULL OR group = @group)

I don’t know if it is corrent, it somehow works 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:09:33+00:00Added an answer on June 2, 2026 at 2:09 am

    You might pretend you have a table by using CTE populated with records via union all. Then perform a join and a check. If you prefer CTE might be replaced with derived table.

    ; WITH departments (DepartmentID, GroupID) as (
      select 'IT', 1
      union all
      select 'IT', 2
      union all
      select 'IT', 5
      union all
      select 'CALL CENTRE', 4
      union all
      select 'CALL CENTRE', 6
      union all
      select 'PRINT', 3
    )
    SELECT * 
    FROM orders 
      INNER JOIN departments
         ON orders.[group] = departments.GroupID
    WHERE (@Group is null OR [group] = @group)
      AND (@department is null OR DepartmentID = @department)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simply query like this: SELECT users.id, COUNT(others.id) as c FROM
If you have a simple Linq query like: var result = from record in
I have this simple query: SELECT xObjectID, xObjectName FROM dbo.xObject where CONTAINS( xObjectRef, '1838
I have a simple query in php on oracle: $query='select u.username,u.lastname,u.firstname,c.event,c.reason from users u,
Here is the simple sql query that will return all tables that have the
We have three tables that we would like to query to find out the
I have a simple query: $query = new WP_Query('showposts=5'); that will obviously display 5
I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of
I have a query like below that has groups (COL1) and that group's values
I have a sql function that does a simple sql select statement: CREATE OR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.