distinct union query need a numeric field added for sorting purposes

currently i have this disitnct union query:

Is there anyway to add a sort column called "Sort"

Usually i just Select 0 As Sort then build the sql...But I habve this distinct query now...the sort column can be at the end of the table also.

sQuery = "SELECT DISTINCT Iif(wwgsku = '" & rng & "', 'WSKU'," & vbCrLf
sQuery = sQuery & "Iif(WWGMfrNumCondensed = '" & rng & "', 'WMFR'," & vbCrLf
sQuery = sQuery & "Iif([CompSKUCondensed] = '" & rng & "', 'CSKU'," & vbCrLf
sQuery = sQuery & "Iif([CompMfrNumCondensed] = '" & rng & "', 'CMFR', 'NA')))) AS Match_Type, a.WWGSKU, a.WWGShortDesc, a.WWGLongDesc, a.WWGMfrNum, a.WWGMfrNumCondensed, a.WWGVdrBrandName, a.WWGUOM, a.WWGUOMQty, a.WWGPrice, a.WWGSlsStatus, a.WWGCatalogPgNo, a.CompMfrNum, a.CompMfrNumCondensed, a.CompMfrName, a.CompSKU, a.CompSKUCondensed, a.CompName, a.CompSKUDesc, a.CompUOM, a.CompUOMQty, a.CompPrice, a.XRefType, a.XRef_Comment, a.WWGCAOrange, a.WWGCAWhite, a.WWGCountry, a.WWGGreen, a.WWGExclBrand, a.WWGGSAComplaint, a.WWGParentSKU, a.WWGDivision, a.WWGSlsStatus" & vbCrLf
sQuery = sQuery & "FROM main_xref_t1_1 AS a" & vbCrLf
sQuery = sQuery & "WHERE [CompMfrNumCondensed] = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or [CompSKUCondensed] = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or wwgsku = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or WWGMfrNumCondensed = '" & rng & "'" & vbCrLf
sQuery = sQuery & "union " & vbCrLf
sQuery = sQuery & "SELECT DISTINCT Iif(wwgsku = '" & rng & "','WSKU', " & vbCrLf
      sQuery = sQuery & "Iif(WWGMfrNumCondensed = '" & rng & "', 'WMFR'," & vbCrLf
      sQuery = sQuery & "Iif([CompSKUCondensed] = '" & rng & "', 'CSKU'," & vbCrLf
      sQuery = sQuery & "Iif([CompMfrNumCondensed] = '" & rng & "', 'CMFR', 'NA')))) AS Match_Type, a.WWGSKU, a.WWGShortDesc, a.WWGLongDesc, a.WWGMfrNum, a.WWGMfrNumCondensed, a.WWGVdrBrandName, a.WWGUOM, a.WWGUOMQty, a.WWGPrice, a.WWGSlsStatus, a.WWGCatalogPgNo, a.CompMfrNum, a.CompMfrNumCondensed, a.CompMfrName, a.CompSKU, a.CompSKUCondensed, a.CompName, a.CompSKUDesc, a.CompUOM, a.CompUOMQty, a.CompPrice, a.XRefType, a.XRef_Comment, a.WWGCAOrange, a.WWGCAWhite, a.WWGCountry, a.WWGGreen, a.WWGExclBrand, a.WWGGSAComplaint, a.WWGParentSKU, a.WWGDivision, a.WWGSlsStatus" & vbCrLf
sQuery = sQuery & "FROM main_xref_t1_2 AS a" & vbCrLf
sQuery = sQuery & "WHERE [CompMfrNumCondensed] = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or [CompSKUCondensed] = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or wwgsku = '" & rng & "'" & vbCrLf
  sQuery = sQuery & "or WWGMfrNumCondensed = '" & rng & "'" 

Open in new window



Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
Can you add another column = Yes
It can be called "Sort"

you could add this to lines 4 & 14
or, insert new lines under each of those.
e.g.

[new line] sQuery = sQuery & ", [something] as [sort]" & vbCrLf

but somehow I feel I'm not getting the point of the question
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PortletPaulfreelancerCommented:
Just thought I'd mention something, you don't need "DISTINCT" in that query; UNION will do this for you anyway.

Look at the data, queries and results below
    
    CREATE TABLE Table1
    	([ID] int, [A_FK] int, [SomeText] varchar(21))
    ;
    	
    INSERT INTO Table1
    	([ID], [A_FK], [SomeText])
    VALUES
    	(1, 2, 'Repetitive originally'),
    	(2, 3, 'now I'),
    	(3, 1, 'I was'),
    	(4, 2, 'Repetitive originally'),
    	(5, 3, 'now I'),
    	(6, 4, 'am Unique'),
    	(7, 3, 'now I'),
    	(8, 2, 'Repetitive originally'),
    	(9, 4, 'am Unique'),
    	(10, 1, 'I was'),
    	(11, 1, 'I was'),
    	(12, 4, 'am Unique'),
    	(13, 2, 'Repetitive originally'),
    	(14, 3, 'now I'),
    	(15, 4, 'am Unique')
    ;
    
    CREATE TABLE Table2
    	([ID] int, [A_FK] int, [SomeText] varchar(21))
    ;
    	
    INSERT INTO Table2
    	([ID], [A_FK], [SomeText])
    VALUES
    	(1, 2, 'Repetitive originally'),
    	(2, 3, 'now I'),
    	(3, 1, 'I was'),
    	(4, 2, 'Repetitive originally'),
    	(5, 3, 'now I'),
    	(6, 4, 'am Unique'),
    	(7, 3, 'now I'),
    	(8, 2, 'Repetitive originally'),
    	(9, 4, 'am Unique'),
    	(10, 1, 'I was'),
    	(11, 1, 'I was'),
    	(12, 4, 'am Unique'),
    	(13, 2, 'Repetitive originally'),
    	(14, 3, 'now I'),
    	(15, 4, 'am Unique')
    ;

Open in new window

**Query 1 just UNION **:

    SELECT a_fk, SomeText FROM table1
    UNION
    SELECT a_fk, SomeText FROM table2
    ORDER BY a_fk
    
    | A_FK |              SOMETEXT |
    |------|-----------------------|
    |    1 |                 I was |
    |    2 | Repetitive originally |
    |    3 |                 now I |
    |    4 |             am Unique |


**Query 2 plus DISTINCT **:

    SELECT DISTINCT a_fk, SomeText FROM table1
    UNION
    SELECT DISTINCT a_fk, SomeText FROM table2
    ORDER BY a_fk
    
    | A_FK |              SOMETEXT |
    |------|-----------------------|
    |    1 |                 I was |
    |    2 | Repetitive originally |
    |    3 |                 now I |
    |    4 |             am Unique |



  [1]: http://sqlfiddle.com/#!3/df8bc/5

Open in new window

0
FordraidersAuthor Commented:
Thanks
0
PortletPaulfreelancerCommented:
No problem, thank you. Cheers, Paul

ps: I changed 'numric' to 'numeric' in title in case you are wondering who did it
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.