Search code examples
sql.nett-sqlmicrosoft-chart-controls

Tsql Sorting an ORDER BY with WHERE


I am working on a .net chart control project and am new to sql. I want to be able to sort the date which is from a textbox input.

SELECT Employee.[First Name] + ' ' + Employee.[Last Name] AS 'Name',
sum(Time.[Total Hours]) AS 'Total Hours'
, FORMAT([Time].[Time in], 'd', 'en-gb') AS 'Worked On'
FROM Employee
inner join [Time] on
Employee.ID =  Time.EmployeeIdFK 
GROUP BY FORMAT([Time].[Time in], 'd', 'en-gb')
,Employee.[First Name] + ' ' + Employee.[Last Name]

--WHERE Time.[Time in] between '6/23/2016' and '7/26/2016'

So far after running the query I get. This is just a dummy.mdf i am testing out.

Name                                 Total Hours            Worked On
------------------------------------ ---------------------- -----------
Cola Coco                            11.3332033110278       23/07/2016
Cola Coco                            0.00417034752421024    26/07/2016
Lmimited Pepsi                       14.2175327702778       23/07/2016
Lmimited Pepsi                       2.9601598583878E-07    24/07/2016
Lmimited Pepsi                       1.05082415577342E-06   26/07/2016

I want to add

WHERE Time.[Time in] between '6/23/2016' and '7/26/2016'

or something similar so that I can just input the date from the text box.


Solution

  • --UPDATE--

    Thank you for the comments, I have been placing the WHERE after GROUP BY which has been giving me an error.

     SELECT Employee.[First Name] + ' ' + Employee.[Last Name] AS 'Name',
     sum(Time.[Total Hours]) AS 'Total Hours'
     , FORMAT(Time.[Time in], 'd', 'en-gb') AS 'Worked On'
     FROM Employee
     inner join Time on
     Employee.ID = Time.EmployeeIdFK 
     WHERE Time.[Time in] between '6/23/2016' and '7/26/2016'
     GROUP BY FORMAT(Time.[Time in], 'd', 'en-gb')
     ,Employee.[First Name] + ' ' + Employee.[Last Name]
    
    Name                                 Total Hours            Worked On
    ------------------------------------ ---------------------- -----------
    Cola Coco                            11.3332033110278       23/07/2016
    Lmimited Pepsi                       14.2175327702778       23/07/2016
    Lmimited Pepsi                       2.9601598583878E-07    24/07/2016
    

    This works fine now.