Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Show 0 if no results

I have a table with result set below:

C_Date    C_Time     Day        L_TID     Terminal_Name      Branch   Branch_Name           L_UID     C_Name            C_Unique    L_Result    C_Card
20140327  905        Thursday   4640601   046406 Virdi 4000  46406    Sr Blue Downs 046406  1220578   Tsutsu;Simamkele  1220578     0

What is happening is that if a branch has no information, it does not come up at all. What I would like is for it to show 0s if possible. I've tried doing a CASE on the C_Date column however everything is just coming up as Unknown and still not bringing my branch up!

Below is my script:

    SELECT 

 tEnter.C_Date, tEnter.C_Time, 
 datename (dw,tEnter.C_Date) AS [Day], 
CONVERT(CHAR(8), GETDATE () -7, 112) as [startdate],
CONVERT(CHAR(8), GETDATE () -1, 112) as [enddate], 
            tEnter.L_TID, 
            tTerminal.C_Name AS Terminal_Name, 
            tTerminal.C_Office AS Branch, 
            tTerminal.C_Place AS Branch_Name, 
            tEnter.L_UID, 
        tEnter.C_Name, 
        tEnter.C_Unique,
        tEnter.L_Result, 
        tEnter.C_Card,
   CASE tEnter.C_Date
        WHEN 1 THEN '1' WHEN 0 THEN 'Nothing' ELSE 'Unknown' END AS 'Test'

INTO #TEMPTABLE

FROM         tEnter INNER JOIN
                      tTerminal ON tEnter.L_TID = tTerminal.L_ID

WHERE 

         tEnter.C_Date between CONVERT(CHAR(8), GETDATE () -7, 112) and 
         CONVERT(CHAR(8), GETDATE () -1, 112)

select * from #TEMPTABLE
like image 338
Lisa Avatar asked Feb 20 '26 17:02

Lisa


1 Answers

After a lot of playing around, this did the trick.

        SELECT DISTINCT C_Office
        INTO #Branch
        FROM tTerminal

        SELECT 

        ISNULL(tEnter.C_Date,0) AS C_Date,
        ISNULL( tEnter.C_Time,0) AS C_Time, 
        datename (dw,tEnter.C_Date) AS [Day], 
        CONVERT(CHAR(8), GETDATE () -7, 112) as [startdate],
        CONVERT(CHAR(8), GETDATE () -1, 112) as [enddate], 
                    tEnter.L_TID, 
                    tTerminal.C_Name AS Terminal_Name, 
                    BRN.C_Office AS Branch, 
                    tTerminal.C_Place AS Branch_Name, 
                    tEnter.L_UID, 
                tEnter.C_Name, 
                tEnter.C_Unique,
                tEnter.L_Result, 
                tEnter.C_Card

        INTO #TEMPTABLE2 
        FROM #Branch as BRN

                LEFT OUTER JOIN tEnter 
                on tEnter.C_Office = BRN.C_Office
                AND (tEnter.C_Date between CONVERT(CHAR(8), GETDATE () -7, 112) and CONVERT(CHAR(8), GETDATE () -1, 112))

                LEFT OUTER JOIN tTerminal 
                ON tEnter.L_TID = tTerminal.L_ID
like image 196
Lisa Avatar answered Feb 23 '26 07:02

Lisa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!