Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
As we know that the record will stored to the database if there is a transaction on that date. But if there is certain date that we had no transactions, No date of transactions will be keep on record to the database. Now, I will make a report, I need to display the dates including no transactions. Including Saturday and Sunday. I used pivot method to my sql query.

What I have tried:

SELECT *
FROM (
    SELECT 
       A.employee_id,B.firstname, B.branch_code , A.trans_date as [Date],A.bioinoutmode as [Type], 
        A.trans_time as Time 
    FROM biometricdata A
LEFT JOIN Employees B ON A.employee_id = B.employee_id
WHERE A.employee_id = B.employee_id
) as s
PIVOT
(
    MAX(Time)
    FOR [Type] IN ([0],[1],[2],[3],[4],[5])
)AS pvt Order by DATE
Posted
Updated 30-Nov-17 21:37pm
v2

1 solution

I usually do this by generating a list of the dates that I need (see Generating a Sequence in SQL[^] ) and then using that table in a LEFT JOIN to my actual data table.
E.g.
SQL
SELECT ....-- column list
FROM MyListOfDates D
LEFT OUTER JOIN biometricdata A ON D.DesiredDate = A.[DATE] -- or whatever your column is
LEFT OUTER JOIN Employees B ON A.employee_id = B.employee_id

Just remember to handle the resulting NULLs returned from A & B for the empty dates
 
Share this answer
 
Comments
Member 13264296 3-Dec-17 20:38pm    
I had another code here. But the missin dates are not display..

DECLARE @startdate DATETIME
,@enddate DATETIME;

SET @startdate = '11/18/2017';
SET @enddate = '11/25/2017';
WITH calendardates
AS ( SELECT date = @startdate
UNION ALL
SELECT DATEADD(DAY, 1, date)
FROM calendardates
WHERE DATEADD(DAY, 1, date) <= @enddate
)
SELECT CONVERT(VARCHAR(15), c.date, 101) AS logdate
,t.employee_id
,CONVERT(VARCHAR(10), t.trans_time, 108) AS timein
FROM dbo.biometricdata t
RIGHT JOIN calendardates c
ON t.trans_date = c.date WHERE t.employee_id = '7263'
CHill60 4-Dec-17 8:28am    
You should use
FROM calendardates c
LEFT OUTER JOIN dbo.biometricdata t ON t.trans_date = c.date
.
Use LEFT OUTER when you don't want data on the left "LEFT OUT"!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900