Trying to understand this SQL Query

  • 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;

  • 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

  • .... 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...

  • 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 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/

  • 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

  • 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

    FROMv_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;

  • 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

    FROMv_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.

  • Hi micky t.

    I have done that and all seems fine now

    thanks everyone.

    not sure how to close this as resolved

  • 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

    FROMv_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

  • 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'

  • 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.

    Read this http://msdn.microsoft.com/en-us/library/ms191472(v=sql.105).aspx


    Alex Suprun

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply