Solved

Create a query where the fields are REVERSED based on field criteria

Posted on 2014-04-16
10
246 Views
Last Modified: 2014-04-16
access 2010
I need a query(S) to do 2 things:

01..REVERSE(FLIP) 2 FIELDS
02..DELETE DUPLICATES



3 fields
MATERIAL_NO
INDEX_LEVEL1
INDEX_LEVEL2


MATERIAL_NO      INDEX_LEVEL1                  INDEX_LEVEL2
3EB46            Hand                  Towels
3EB46            Paper                  Towels
3EB46            Towels,                  Hand
3EB46            Towels,                  Paper
3EB46            Towels,                  Roll



IN THE EXAMPLE ABOVE:
ANY COMMA IN INDEX_LEVEL1 FIELD I NEED THE FIELDS TO REVERSE(FLIP)..REMOVE THE COMMA
1st Result
MATERIAL_NO      INDEX_LEVEL1                 INDEX_LEVEL2
3EB46            Hand                                  Towels
3EB46            Paper                                  Towels
3EB46            Hand                                        Towels
3EB46            Paper                                        Towels
3EB46            Roll                                            Towels


2nd Result:  NOW REMOVE DUPLICATES
MATERIAL_NO      INDEX_LEVEL1                     INDEX_LEVEL2
3EB46            Hand                                   Towels
3EB46            Paper                                       Towels
3EB46            Roll                                          Towels
0
Comment
Question by:fordraiders
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 400 total points
ID: 40003397
First create this query, qdyMaterials, to clean up:

SELECT
    tblMaterials.Material_No,
    IIf(Right([Index_Level1],1)=",",[Index_Level2],[Index_Level1]) AS IndexLevel1,
    IIf(Right([Index_Level1],1)=",",Replace([Index_Level1],",",""),[Index_Level2]) AS IndexLevel2
FROM
    tblMaterials;

Then this query to remove duplicates and obtain original field names:

SELECT DISTINCT
    qdyMaterials.Material_No,
    qdyMaterials.IndexLevel1 AS Index_Level1,
    qdyMaterials.IndexLevel2 AS Index_Level2
FROM
    qdyMaterials;

/gustav
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 100 total points
ID: 40003399
You don't indicate whether you actually want to store the results somewhere.  This should work to give you a recordset that meets your requirements as stated above

SELECT DISTINCT
Material_NO
, IIF(Right(yourTable.[Index_Level1], 1) <> ",",
        yourTable.[Index_Level1],
        yourTable.[Index_Level2]) as [Index_Level1]
, IIF(Right(yourTable.[Index_Level1], 1) <> ",",
        yourTable.[Index_Level2],
        LEFT(yourTable.[Index_Level1], LEN(yourTable.[Index_Level1]) -1) as [Index_Level2]
FROM yourTable
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40003400
gustav,

like your use of 'Replace' instead of my use of 'Left'
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40003613
fyed:  getting circuler reference:
 AS IndexLevel1 alias field in   query definition SELECT list
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40003629
gustav,
getting an operator error?

error in missing operator:
I changed the tablename.
operator error
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 6

Expert Comment

by:Dulton
ID: 40003686
circular reference comes from naming the outgoing (result) field the same thing as the original field name. many RDMS support this, but access won't let it fly.

The below will use a nested query in order to keep your result set field names the same as your originals.

SELECT DISTINCT t1.Material_NO
, t1.[IndexLevel1] AS [Index_Level1]
, t1.[IndexLevel2] AS [Index_Level2]
FROM
(SELECT 
Material_NO
, IIF(Right(mt.[Index_Level1], 1) <> ",", 
        mt.[Index_Level1], 
        mt.[Index_Level2]) as [IndexLevel1]
, IIF(Right(mt.[Index_Level1], 1) <> ",", 
        mt.[IndexLevel2], 
        LEFT(mt.[Index_Level1], LEN(mt.[Index_Level1]) -1) as [Index_Level2]
FROM yourTable AS mt) AS t1

Open in new window

0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40003720
You should not get the circular reference error if you preface each of the field names in the IIF( ) clauses with the tablename.  If you try:

iif(Right([Index_Level1], 1) = ",", [Index_Level2], [Index_Level1]) as [Index_Level1]

Then you should get that error, but if you use:

iif(Right(YourTable.[Index_Level1], 1) = ",",
     yourTable.[Index_Level2],
     yourTable.[Index_Level1]) as [Index_Level1]

it should work.
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40003730
> error in missing operator:
> I changed the tablename.

You did more than that.
Try again, this time only changing the table name.

/gustav
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40003772
Yes, Thanks  syntax error.
0
 
LVL 3

Author Comment

by:fordraiders
ID: 40003776
dulton, Sorry...I graded before viewing again.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now