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»»

Trying to understand this SQL Query Expand / Collapse
Author
Message
Posted Monday, June 17, 2013 4:46 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 3, 2014 4:58 PM
Points: 130, 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;

Post #1464425
Posted Tuesday, June 18, 2013 2:26 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 4:21 AM
Points: 692, Visits: 2,804
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
Post #1464501
Posted Tuesday, June 18, 2013 2:48 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, October 18, 2014 7:23 AM
Points: 152, Visits: 389
.... 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...

Post #1464507
Posted Tuesday, June 18, 2013 8:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:44 AM
Points: 13,093, Visits: 12,574
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.


_______________________________________________________________

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 #1464697
Posted Tuesday, June 18, 2013 2:00 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:04 PM
Points: 1,066, Visits: 3,138
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

Post #1464864
Posted Tuesday, June 18, 2013 4:48 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 3, 2014 4:58 PM
Points: 130, 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;

Post #1464908
Posted Tuesday, June 18, 2013 6:37 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 2:04 PM
Points: 1,066, Visits: 3,138
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.
Post #1464925
Posted Tuesday, June 18, 2013 7:27 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 3, 2014 4:58 PM
Points: 130, Visits: 214
Hi micky t.


I have done that and all seems fine now


thanks everyone.


not sure how to close this as resolved
Post #1464931
Posted Wednesday, June 19, 2013 1:52 AM


Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 4:21 AM
Points: 692, Visits: 2,804
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
Post #1464979
Posted Wednesday, June 19, 2013 6:11 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 10:34 AM
Points: 386, Visits: 624
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'

Post #1465117
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse