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


Trying to understand this SQL Query


Trying to understand this SQL Query

Author
Message
Oracle765
Oracle765
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 214
Hi.

I am fairly new to SQL Server and have come across this SQL Server Query at work and I am trying to understand their logic here.

Is there a more tidy way to re-write this query, I am not even sure if it is return the desired unique results or if it is return a cartisian product.

anyway professionals here is the code I am trying to work with


SELECT distinct a1.Netbios_Name0,
c1.SerialNumber0,
a1.Operating_System_Name_and0,
b1.Publisher0,
b1.DisplayName0,
b1.Version0,
b1.InstallDate0,
c1.TimeStamp
FROM v_R_System a1

inner join v_add_remove_programs b1
on a1.ResourceID = b1.ResourceID

inner join v_GS_PC_BIOS c1
on a1.ResourceID = c1.ResourceID

left outer join v_GS_CCM_RECENTLY_USED_APPS d1
on b1.ResourceID = d1.ResourceID
and b1.GroupID = d1.GroupID

GROUP BY a1.Netbios_Name0,
c1.SerialNumber0,
a1.Operating_System_Name_and0,
b1.Publisher0,
b1.DisplayName0,
b1.Version0,
b1.InstallDate0,
c1.TimeStamp

ORDER BY 1;


Andy Hyslop
Andy Hyslop
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2165 Visits: 3045
Hi

What don't you understand about it?

Looks like the GROUP BY is not needed (Unless its in an effort to eliminate duplicates)

Andy

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
Arthur Kirchner
Arthur Kirchner
SSC Veteran
SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)SSC Veteran (251 reputation)

Group: General Forum Members
Points: 251 Visits: 398
.... and the ORDER BY 1 means ORDER BY the first column expression the SELECT Statement.

Just adding this because I didn't know that not a long while ago...
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62879 Visits: 17959
Arthur Kirchner (6/18/2013)
.... and the ORDER BY 1 means ORDER BY the first column expression the SELECT Statement.

Just adding this because I didn't know that not a long while ago...



To add to this, it is also a bad practice. You should not order by the ordinal position, instead you should order by the name of the column. It eliminates confusion and possible issues down the road. If the column order changes, the results will be ordered incorrectly unless you also remember to change the order by. w00t

_______________________________________________________________

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)
mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2850 Visits: 3318
Hi

I may be wrong, but I can't see a reason for this join
left outer join v_GS_CCM_RECENTLY_USED_APPS d1
on b1.ResourceID = d1.ResourceID
and b1.GroupID = d1.GroupID


Oracle765
Oracle765
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 214
Hi all

yes i agree, i am not sure about the left out join thats what has confused me a bit.


would this query provide the same results?

SELECT distinct a1.Netbios_Name0,
c1.SerialNumber0,
a1.Operating_System_Name_and0,
b1.Publisher0,
b1.DisplayName0,
b1.Version0,
b1.InstallDate0,
c1.TimeStamp
FROM v_R_System a1,
v_add_remove_programs b1,
v_gs_PC_bios c1,
v_gs_ccm_recently_used_apps d1
where a1.resourceid = b1.resourceid
and a1.resourceid = c1.resourceid
and b1.resourceid = d1.resourceid
and b1.groupid = d1.groupid

ORDER BY a1.netbios_name0;


mickyT
mickyT
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2850 Visits: 3318
Oracle765 (6/18/2013)
Hi all

yes i agree, i am not sure about the left out join thats what has confused me a bit.


would this query provide the same results?

SELECT distinct a1.Netbios_Name0,
c1.SerialNumber0,
a1.Operating_System_Name_and0,
b1.Publisher0,
b1.DisplayName0,
b1.Version0,
b1.InstallDate0,
c1.TimeStamp
FROM v_R_System a1,
v_add_remove_programs b1,
v_gs_PC_bios c1,
v_gs_ccm_recently_used_apps d1
where a1.resourceid = b1.resourceid
and a1.resourceid = c1.resourceid
and b1.resourceid = d1.resourceid
and b1.groupid = d1.groupid

ORDER BY a1.netbios_name0;


Given that it appears that the v_gs_ccm_recently_used_apps table isn't used to filter or return any results in the original, I would say that it could be removed from the query altogether.
In your query above you are changing the outer join to this table an inner join. This may affect (reduce) the rows returned.
Oracle765
Oracle765
SSC-Addicted
SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)SSC-Addicted (450 reputation)

Group: General Forum Members
Points: 450 Visits: 214
Hi micky t.


I have done that and all seems fine now


thanks everyone.


not sure how to close this as resolved
Andy Hyslop
Andy Hyslop
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2165 Visits: 3045
mickyT (6/18/2013)
Oracle765 (6/18/2013)
Hi all

yes i agree, i am not sure about the left out join thats what has confused me a bit.


would this query provide the same results?

SELECT distinct a1.Netbios_Name0,
c1.SerialNumber0,
a1.Operating_System_Name_and0,
b1.Publisher0,
b1.DisplayName0,
b1.Version0,
b1.InstallDate0,
c1.TimeStamp
FROM v_R_System a1,
v_add_remove_programs b1,
v_gs_PC_bios c1,
v_gs_ccm_recently_used_apps d1
where a1.resourceid = b1.resourceid
and a1.resourceid = c1.resourceid
and b1.resourceid = d1.resourceid
and b1.groupid = d1.groupid

ORDER BY a1.netbios_name0;


Given that it appears that the v_gs_ccm_recently_used_apps table isn't used to filter or return any results in the original, I would say that it could be removed from the query altogether.
In your query above you are changing the outer join to this table an inner join. This may affect (reduce) the rows returned.


Be careful those are old ANSI-89 Joins, its much better to use the ANSI-92 Joins

==========================================================================================================================
A computer lets you make more mistakes faster than any invention in human history - with the possible exceptions of handguns and tequila. Mitch Ratcliffe
aaron.reese
aaron.reese
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2431 Visits: 907
What any means is that the FROM clause is just listing the tables and not the relationships between them and relies on the WHERE clause for its joining criteria.

Old Style

Select
A.somecol,
B.othercol
FROM
A,
B
WHERE
A.foreignkey = B.primaryKey
AND
A.somecol = 'somevalue'


New Style

Select
A.somecol,
B.othercol
FROM
A
INNER JOIN
B on B.primaryKey = A.foreignKey

WHERE
A.somecol = 'somevalue'


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