+ Reply to Thread
Results 1 to 9 of 9

Flag unique values per field in SQL, 0 or 1

  1. #1
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Flag unique values per field in SQL, 0 or 1

    I'm trying to create a field in an SQL query which will flag whether another field contains a unique value

    Example, a query returns 1000 invoices. Some are duplicates, some are unique. I am trying to place a 1 on all uniques, and a 1 on only the first of the duplicates.

    So
    Please Login or Register  to view this content.

    I'm at a loss!

    Thank you
    Last edited by Speshul; 08-14-2015 at 02:36 PM.
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Flag unique values per field in SQL, 0 or 1

    Hi Speshul,

    If I understand your question correctly, you have an Excel column of Invoice Numbers, and you want to identify the first occurrence of an Invoice Number and you want to identify duplicates.

    This problem can be solved using an Excel 'Scripting Dictionary', which is very good at finding unique items in a list. For more information about the Excel 'Scripting Dictionary' see: http://www.snb-vba.eu/VBA_Dictionary_en.html

    Try the attached file which contains the following code:
    Please Login or Register  to view this content.
    Lewis
    Last edited by LJMetzger; 08-15-2015 at 03:45 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Flag unique values per field in SQL, 0 or 1

    This is an interesting solution, but unfortunately it still crashes excel.

    To clarify exactly what I am trying to do to hopefully narrow down possible solutions;

    I have a data set which is pulled from an SQL query to be analyzed and pivoted within Excel. I have a column to determine the first unique occurrence of the invoice number which is a simple =IF(COUNTIF($B$2:B2,B2)=1,1,0)

    Unfortunately, 90k records seems to be enough to completely lock up Excel every time the formula calculates or the sheet is re-sorted making it unusable. Even copy/paste special/values appears to lock up excel.


    I am looking for a pre-excel solution, to do the equivalent calculation within the query itself as I have a feeling raw SQL has an easier time handling such calculation than excel.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Flag unique values per field in SQL, 0 or 1

    This isn't possible ( I don't think) in vanilla SQL, most databases offer a superset of SQL though with additional functionality - so it depends on which database you are using as to how hard this is.

    PS the reason that it's is that in simple terms, databases work within rows, not columns

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Flag unique values per field in SQL, 0 or 1

    In the past, my technique has caused excel to crash at approximately 20,000 records due to Excel memory limitations for a module. If you want an Excel solution, it is possible that you may be successful with 64 bit Excel. Before buying, I would try 64 bit Excel out first. 64 bit Excel comes with compatibility issues (including code rewrite required) if API (Application Program Interface) functions are used in VBA.

    Sorry I have no SQL experience. Even though your question was in the 'Access Tables and DataBases' forum, I thought you might be looking for an Excel Solution.

  6. #6
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Flag unique values per field in SQL, 0 or 1

    I am working from Microsoft SQL Server Management Studio, database is Microsoft SQL Server Enterprise x64 (11.0)

    Edit: LJMetzger, I am running 64bit Excel 2010 under Windows 7 Enterprise x64. I have tried =IF(ISNUMBER(MATCH(B2,$B$1:B1,0)),0,1) which is faster but still quite slow for what I need


    Flagging only the first occurrence seems like something that should exist, can't find it anywhere though :\
    Last edited by Speshul; 08-17-2015 at 10:47 AM.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Flag unique values per field in SQL, 0 or 1

    In TSQL you'd do it like this:

    I've had to attach the file since the forum won't let me post sql
    Attached Files Attached Files

  8. #8
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    276

    Re: Flag unique values per field in SQL, 0 or 1

    One of the problems is that data reccords are typically not stored sequentally within a database and tend to move arround as they get edited; so one that's first today may be last tomorrow. We'll need to know the sorting fields to help you build a query that looks for the "first" reccord...

    MS-Access Example:

    Field1
    DateField
    A 9/17/2015
    A 9/18/2015
    B 9/18/2015
    C 9/18/2015
    D 9/16/2015
    D 9/17/2015
    D 9/18/2015
    E 9/18/2015
    F 9/17/2015
    F 9/18/2015

    Note: Swap out e for é, in the following code
    Please Login or Register  to view this content.
    Gives:
    Field1
    DateField
    CT
    MinD
    TST
    A 9/17/2015 2 9/17/2015 1
    A 9/18/2015 2 9/17/2015 0
    B 9/18/2015 1 9/18/2015 1
    C 9/18/2015 1 9/18/2015 1
    D 9/16/2015 3 9/16/2015 1
    D 9/17/2015 3 9/16/2015 0
    D 9/18/2015 3 9/16/2015 0
    E 9/18/2015 1 9/18/2015 1
    F 9/17/2015 2 9/17/2015 1
    F 9/18/2015 2 9/17/2015 0
    Last edited by Gregor y; 09-18-2015 at 01:19 PM. Reason: Add Example

  9. #9
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    276

    Re: Flag unique values per field in SQL, 0 or 1

    oh shoot, I guess a couple more notes:

    CT really wasn't needed in this case, as Field1+MinD forms a unique value on the table; however it does show how you can form a more complicated query criteria.

    This query is going to run really CPU heavy on the server, and will get exponentially worse the more sub (sélect * from ...) statements you add per reccord count.

    For your simple case you're much better off with Kyle123's solution as it uses built-in code to get better performance.

    You can switch out IIF() from the MS-Acces code to the CASE statment when converting to a more standard SQL Code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Flag values in a list of pairs?
    By unipsychologist in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-31-2015, 12:46 AM
  2. Flag for Multiple Values within 3 mins of each other
    By MagiCarty in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-01-2014, 04:54 PM
  3. Replies: 0
    Last Post: 07-21-2010, 01:43 PM
  4. Macro to set Flag on dup values
    By carlysyme in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2009, 09:16 AM
  5. Subtotal unique values in autofiltered field
    By 1eyedjack in forum Excel General
    Replies: 1
    Last Post: 06-28-2008, 12:39 AM
  6. Flag row if cell values = something specific
    By Some Dude in forum Excel General
    Replies: 5
    Last Post: 05-24-2006, 08:45 AM
  7. [SOLVED] Can Excel automatically flag up cells within given values?
    By Wilson in forum Excel General
    Replies: 4
    Last Post: 03-01-2006, 09:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1