SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Logical errors


SQL Logical errors

Author
Message
sundayose
sundayose
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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
vnbwork
vnbwork
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 Visits: 163
Hi,

Can you describe error details???
AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4312 Visits: 3427
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
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60763 Visits: 17954
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.

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)
AndrewSQLDBA
AndrewSQLDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4312 Visits: 3427
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
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92241 Visits: 38954
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.

Cool
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)
Sean Lange
Sean Lange
SSC Guru
SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)SSC Guru (60K reputation)

Group: General Forum Members
Points: 60763 Visits: 17954
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 Modens 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)
sundayose
sundayose
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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.
sundayose
sundayose
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 16
Hi,

I actually get duplicates when I run my query.
sundayose
sundayose
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search