• The formatting of this makes my eyes bleed. Let's start by cleaning this up a little bit so it is easier to read.

    SELECT DISTINCT [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE AS IssuanceDate

    ,[dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE

    ,[dbo].[tbl_SALES_TEAM_MASTER].[str_SALES_TEAM_NAME]

    ,[dbo].[tbl_VOUCHER_CAPTURE_DETAIL].str_BARCODE AS VouchersSold

    ,[dbo].[tbl_VOUCHER_CAPTURE].dt_VOUCHER_ISSUE_DATE AS SalesDate

    ,[dbo].[tbl_SALES_EXECUTIVE_MASTER].str_SALES_EXECUTIVE_NAME AS BCCName

    ,[dbo].[tbl_DISTRIBUTOR_MASTER].[str_DISTRIBUTOR_NAME] AS CBDName

    ,[dbo].[tbl_DISTRICT_MASTER].str_DISTRICT_NAME CBD_District

    FROM [dbo].[tbl_VOUCHER_ISSUE]

    LEFT JOIN [dbo].[tbl_VOUCHER_ISSUE_DETAIL] ON [dbo].[tbl_VOUCHER_ISSUE].int_VOUCHER_ISSUE_ID = [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_ID

    LEFT JOIN [dbo].[tbl_SALES_TEAM_MASTER] ON [dbo].[tbl_VOUCHER_ISSUE].int_SALES_TEAM_ID = [dbo].[tbl_SALES_TEAM_MASTER].int_SALES_TEAM_ID

    LEFT JOIN [dbo].[tbl_VOUCHER_CAPTURE_DETAIL] ON [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID = [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_ISSUE_DETAIL_ID

    LEFT JOIN [dbo].[tbl_VOUCHER_CAPTURE] ON [dbo].[tbl_VOUCHER_CAPTURE_DETAIL].int_VOUCHER_CAPTURE_ID = [dbo].[tbl_VOUCHER_CAPTURE].int_VOUCHER_CAPTURE_ID

    LEFT JOIN [dbo].[tbl_SALES_EXECUTIVE_MASTER] ON [dbo].[tbl_VOUCHER_CAPTURE].int_SALES_EXECUTIVE_ID = [dbo].[tbl_SALES_EXECUTIVE_MASTER].int_SALES_EXECUTIVE_ID

    LEFT JOIN [dbo].[tbl_DISTRIBUTOR_MASTER] ON [dbo].[tbl_VOUCHER_CAPTURE].[int_DISTRIBUTOR_ID] = [dbo].[tbl_DISTRIBUTOR_MASTER].[int_DISTRIBUTOR_ID]

    LEFT JOIN [dbo].[tbl_DISTRICT_MASTER] ON [dbo].[tbl_DISTRIBUTOR_MASTER].int_DISTRICT_ID = [dbo].[tbl_DISTRICT_MASTER].int_DISTRICT_ID

    WHERE [dbo].[tbl_VOUCHER_ISSUE_DETAIL].str_BARCODE LIKE 'FPUG%'

    AND [dbo].[tbl_VOUCHER_ISSUE].dt_VOUCHER_ISSUE_DATE BETWEEN '2012-06-01'

    AND '2012-06-30'

    AND [dbo].[tbl_VOUCHER_ISSUE_DETAIL].int_STATUS != 5

    So now to address the issue.

    I need some one help me identify why am getting logical errors in the output of my query below.

    What does that mean? We don't know your system and we can't see your screen nor do we know the business requirements so we can't begin to answer that based on a query.

    I have to agree with Andrew SQLDBA about the naming conventions. However instead of just telling you how bad they are I think it would help to explain what is bad about them. It doesn't help to just hear "that is awful". Constructive criticism goes long way.

    The only real issue with your naming convention is the prefixes. These do not provide any actual benefit and will actually start to drive you nuts. Why tbl_? Of course it is a table. That adds 4 extra keystrokes and makes your code more difficult to read because it is cluttered with noise. Even worse though is the datatype prefixes on your column names. This is an absolutely horrible idea. What happens when you realize at some point that you need to change datatypes for a column? Suddenly either your prefix is misleading or you have to change all the code that touches that column.

    Then if you use table aliases your query will be a lot easier to read and TONS less typing. I removed all the prefixes and used aliases. Look how much cleaner this looks now.

    SELECT DISTINCT vi.VOUCHER_ISSUE_DATE AS IssuanceDate

    ,vid.BARCODE

    ,stm.[SALES_TEAM_NAME]

    ,vcd.BARCODE AS VouchersSold

    ,vc.VOUCHER_ISSUE_DATE AS SalesDate

    ,sem.SALES_EXECUTIVE_NAME AS BCCName

    ,dm.[DISTRIBUTOR_NAME] AS CBDName

    ,dim.DISTRICT_NAME CBD_District

    FROM [dbo].[VOUCHER_ISSUE] vi

    LEFT JOIN [dbo].[VOUCHER_ISSUE_DETAIL] vid ON vi.VOUCHER_ISSUE_ID = vid.VOUCHER_ISSUE_ID

    LEFT JOIN [dbo].[SALES_TEAM_MASTER] stm ON vi.SALES_TEAM_ID = stm.SALES_TEAM_ID

    LEFT JOIN [dbo].[VOUCHER_CAPTURE_DETAIL] vcd ON vid.VOUCHER_ISSUE_DETAIL_ID = vcd.VOUCHER_ISSUE_DETAIL_ID

    LEFT JOIN [dbo].[VOUCHER_CAPTURE] vc ON vcd.VOUCHER_CAPTURE_ID = vc.VOUCHER_CAPTURE_ID

    LEFT JOIN [dbo].[SALES_EXECUTIVE_MASTER] sem ON vc.SALES_EXECUTIVE_ID = sem.SALES_EXECUTIVE_ID

    LEFT JOIN [dbo].[DISTRIBUTOR_MASTER] dm ON vc.[DISTRIBUTOR_ID] = dm.[DISTRIBUTOR_ID]

    LEFT JOIN [dbo].[DISTRICT_MASTER] dim ON dm.DISTRICT_ID = dim.DISTRICT_ID

    WHERE vid.BARCODE LIKE 'FPUG%'

    AND vi.VOUCHER_ISSUE_DATE BETWEEN '2012-06-01' AND '2012-06-30'

    AND vid.STATUS != 5

    Hard to believe that is the same query huh?

    OK so if you can explain what the issue is we can probably help.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/