SQLTeam.com | Weblogs | Forums

Sort Data based on user selection

Dears,

I have one issue and I am not able to write proper query for this.

I have 2 tables called Employees and Tickets

  1. /****** Object: Table [dbo].[Tickets] Script Date: 3/14/2022 12:30:28 AM ******/
  2. SET ANSI_NULLS ON
  3. GO
  4. SET QUOTED_IDENTIFIER ON
  5. GO
  6. CREATE TABLE [dbo].[Tickets](
  7. [Id] [int] IDENTITY(1,1) NOT NULL,
  8. [TicketDescription] [nvarchar](500) NOT NULL,
  9. [Latitude] [float] NULL,
  10. [Longitude] [float] NULL,
  11. [TypeId] [int] NULL,
  12. [AreaId] [int] NULL,
  13. CONSTRAINT [PK_Tickets] PRIMARY KEY CLUSTERED
  14. (
  15. [Id] ASC
  16. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  17. ) ON [PRIMARY]
  18. GO
  19. CREATE TABLE [dbo].[Employee](
  20. [Id] [int] IDENTITY(1,1) NOT NULL,
  21. [Name] [nvarchar](100) NULL,
  22. [Latitude] [float] NULL,
  23. [Longitude] [float] NULL,
  24. [TypeId] [int] NULL,
  25. [TicketsToday] [int] NULL,
  26. [AreaId] [int] NULL,
  27. CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
  28. (
  29. [Id] ASC
  30. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  31. ) ON [PRIMARY]
  32. GO
  33. SET IDENTITY_INSERT [dbo].[Employee] ON
  34. GO
  35. INSERT [dbo].[Employee] ([Id], [Name], [Latitude], [Longitude], [TypeId], [TicketsToday], [AreaId]) VALUES (1, N'Supervisor', 17.286848067916505, 78.24663132069098, 1, 5, 1)
  36. GO
  37. INSERT [dbo].[Employee] ([Id], [Name], [Latitude], [Longitude], [TypeId], [TicketsToday], [AreaId]) VALUES (2, N'Reader', 17.207415689708519, 78.253739445364374, 2, 5, 2)
  38. GO
  39. INSERT [dbo].[Employee] ([Id], [Name], [Latitude], [Longitude], [TypeId], [TicketsToday], [AreaId]) VALUES (3, N'Writer', 17.36978521081307, 78.200294359834473, 1, 9, 1)
  40. GO
  41. INSERT [dbo].[Employee] ([Id], [Name], [Latitude], [Longitude], [TypeId], [TicketsToday], [AreaId]) VALUES (4, N'New Join', 17.284559068738243, 78.499708338539662, 1, 5, 2)
  42. GO
  43. SET IDENTITY_INSERT [dbo].[Employee] OFF
  44. GO
  45. SET IDENTITY_INSERT [dbo].[Tickets] ON
  46. GO
  47. INSERT [dbo].[Tickets] ([Id], [TicketDescription], [Latitude], [Longitude], [TypeId], [AreaId]) VALUES (2, N'Emergency Ticket', 17.269480945138515, 78.3290484467444, 1, 1)
  48. GO
  49. INSERT [dbo].[Tickets] ([Id], [TicketDescription], [Latitude], [Longitude], [TypeId], [AreaId]) VALUES (3, N'Normal Ticket', 17.367466654347695, 78.206974995104844, 2, 1)
  50. GO
  51. INSERT [dbo].[Tickets] ([Id], [TicketDescription], [Latitude], [Longitude], [TypeId], [AreaId]) VALUES (4, N'Average Ticket', 17.178406092767386, 78.368524928263341, 1, 2)
  52. GO
  53. SET IDENTITY_INSERT [dbo].[Tickets] OFF
  54. GO

When the user load the tickets I have to filter the employee for each ticket means the system should provide suggestions of the employee who is near to ticket raised area

Conditions:

Location
TypeId
AreaId
TodaysTickets

User can select all or some of the above criteria

1 If Location is selected then
. Sort employees location with tickets location. Display the nearest employee to the ticket location

  1. If TypeId is selected then
    TypeId of Employee should match with ticket typeid
  2. If TodaysTickets Selected
    Get the employee who have less number of TodaysTicket Value
  3. AreaId Selcetd then check the Employee Area Id with Tickets
    I could able to sort based on location but after sorting location now I have to filter employee whose AreaId, TypeId matches the ticket records and Whoever have least todaytickets.

So for each ticket the system provides its suggestion based on the above criteria

The final result needed something like this

TicketDescription EmployeeName
Normal Ticket Reader
Emergency Ticket Supervisor

There are 2 options I guess, you can simply filter by:

WHERE
(@TypeID=TypeID OR @TypeID IS NULL)

Another option is Dynamic SQL. You are writing the query based on the parameters and execute it when your finished. Be carefull with Dynamic SQL as you will need to validate the input parameters to prevent a seciurity risk. I think I would use Dynamic SQL here because it's much easier to expand and maintain when the requirements change and no need to filter all the parameters but it all depands on your situation. When you have a small database and few changes option 1 will be fine.