Search code examples
sqlsql-serverdatagrip

Trying to create a SQL query for a repair database


I am trying to create a query that returns the list of works performed by ewere or Daria in the period from 01.01.2014 to 01.02.2014 or in the period from 01.05.2014 to 01.06.2014 or basically any date period.

The list must contain a description of the work, the name of the worker, the date of the work. The list must be sorted by ascending date of work.

Operations table

worker table

This is what I have tried, as I am new to SQL and trying to understand:

Select 
    tblOperation.datOperationDate, 
    tblOperation.txtOperationDescription,
    tblWorker.txtWorkerName, tblWorker.intWorkerId
From 
    tblOperation
Inner join 
    tblWorker on tblOperation.intWorkerId = tblWorker.intWorkerId
Where 
    ((('2000-08-20' < tblOperation.datOperationDate) and 
      ('2012-02-20' < tblOperation.datOperationDate)) or
     (('2010-01-24' < tblOperation.datOperationDate) and  
      ('2018-01-13' < tblOperation.datOperationDate))) and
    (tblOperation.txtOperationDescription = true) and 
    (tblWorker..txtWorkerName = 'ewere') or 
    (tblWorker..txtWorkerName = 'Daria')
Order by 
    tblOperation.datOperationDate

Solution

  • Your date range is overlapping and effectively means (if were written correctly) between '2000-08-21' and '2018-01-12'. For description, probably you meant those that are not null, it is not a boolean field and in SQL server booleans are bit (0 or 1):

    Select tblOperation.datOperationDate, tblOperation.txtOperationDescription,tblWorker.txtWorkerName ,tblWorker.intWorkerId
    From tblOperation
    Inner join tblWorker on tblOperation.intWorkerId = tblWorker.intWorkerId
    Where 
        tblOperation.datOperationDate between '20000821' and '20180112' and
        tblOperation.txtOperationDescription is not null and 
        tblWorker.txtWorkerName in ('ewere','Daria')
    Order by tblOperation.datOperationDate;