Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

SQL Logical errors Expand / Collapse
Author
Message
Posted Wednesday, March 27, 2013 11:57 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 9, 2013 11:04 AM
Points: 7, Visits: 16
Hi All,

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

use MSIU;

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



Post #1436267
Posted Thursday, March 28, 2013 12:56 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, April 4, 2014 12:38 AM
Points: 90, Visits: 99
Hi,

Can you describe error details???
Post #1436283
Posted Thursday, March 28, 2013 7:51 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 2:35 PM
Points: 975, Visits: 3,350
I have to say this, and I mean it is a nice way.

That is just horrible naming convention for your database objects.

Please change all that. truly the worst ever.

Andrew SQLDBA
Post #1436469
Posted Thursday, March 28, 2013 8:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 13,282, Visits: 12,116
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436497
Posted Thursday, March 28, 2013 8:45 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 2:35 PM
Points: 975, Visits: 3,350
Hey Sean
You are exactly correct. I should have offered some explanation like you did. I like the way you explained why and gave a nice example.

Andrew SQLDBA
Post #1436514
Posted Thursday, March 28, 2013 8:48 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:19 PM
Points: 23,243, Visits: 31,938
Just another reason to use table aliases in your code, Microsoft has deprecrated 3- and 4-part naming conventions in the select list. These will not be supported in a future version (who knows what version is not known by us) of SQL Server.




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1436517
Posted Thursday, March 28, 2013 8:54 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 13,282, Visits: 12,116
Lynn Pettis (3/28/2013)
Just another reason to use table aliases in your code, Microsoft has deprecrated 3- and 4-part naming conventions in the select list. These will not be supported in a future version (who knows what version is not known by us) of SQL Server.



I had no idea it was actually deprecated. It is certainly painful to have to list it over and over so I haven't done it in years. Good to know.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436527
Posted Thursday, March 28, 2013 9:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 9, 2013 11:04 AM
Points: 7, Visits: 16
Hi All,

Thank you both for your responses. My query has a lot of wording because I was dragging the table names from the database tables in SQL Management Studio.
However, the problem is that when I execute that query, it runs very well but the output has more records than expected.

The whole idea is described below;

I have 6 Tables (VI,VID,SE,ST,VCD and DT) which I need to query as described below with their keys;

VI VID ST SE VCD DT
id1 id1 id2 ad2 id1 id3
id2 id2
id3

I need output from all the tables left joined to VI since it's my basis of comparison.

Thank you.

Post #1436554
Posted Thursday, March 28, 2013 9:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 9, 2013 11:04 AM
Points: 7, Visits: 16
Hi,

I actually get duplicates when I run my query.
Post #1436556
Posted Thursday, March 28, 2013 9:40 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, August 9, 2013 11:04 AM
Points: 7, Visits: 16
I think what I need is a tutorial which will teach me on how to join multiple table.

Most of the tutorials I am getting on the internet do not show how to deal with multiple tables like 5 or more thanks.
Post #1436563
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse