📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

Anyone good with SQL?

Options
13

Comments

  • JohnB47
    JohnB47 Posts: 2,668 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    edited 6 January at 4:51PM
    Old programmer in me is shouting "get the input data validation tightened up".

    Makes this type of problem unnecessarily hard if the conditional tests can have any old rubbish thrown at them. 
    Not sure what you mean by validation but in my case, the input is derived from a CSV file supplied by my credit card company. I and maybe they, have no control over how the various companies supply their names. Sometimes the same company name is uppercase, sometimes it's lower, sometimes mixed, sometimes with additional characters. No idea why it changes.

    For info, I take the CSV, convert it to xlxs format and run a macro that converts the format of the fields to suit the target database table, then I append the data to that table. Then I run the SQL in the database that allocates a 'Type' number to the various spend categories. Then the database forms are use to select/display the spend/totals by type.

    I suppose I could run a macro on the CSV or XLXS (or add to my existing macro) that would change all company names to upper or lower case and that would avoid me having to change my rather large SQL by adding 'upper' in lots of places. (Edit: I'd still have to make lots of changes actually.) Perhaps that's a question for another thread. Still wouldn't get rid of the extra characters like Fred's.

    Anyway It's been good to see the solutions quoted here.
  • victor2
    victor2 Posts: 8,138 Ambassador
    Part of the Furniture 1,000 Posts Name Dropper
    Probably a whole new project you may not even wish to tackle, but you could bypass using Excel altogether and just develop a routine in Libre office to import the data into the database.
    I have no idea what capabilities Libre office has, but it could be worth a new thread on here if you wished to explore it...

    I’m a Forum Ambassador and I support the Forum Team on the In My Home MoneySaving, Energy and Techie Stuff boards. If you need any help on these boards, do let me know. Please note that Ambassadors are not moderators. Any posts you spot in breach of the Forum Rules should be reported via the report button, or by emailing forumteam@moneysavingexpert.com. 

    All views are my own and not the official line of MoneySavingExpert.

  • Ergates
    Ergates Posts: 3,045 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Excel can open .csv files without converting.  and a macro can be written to just ingest the data straight from an external file (if you know the file format, which presumably you do).

    From a data perspective, it would make more sense to standardise the data on load rather than at point of use - 
  • flaneurs_lobster
    flaneurs_lobster Posts: 6,570 Forumite
    Sixth Anniversary 1,000 Posts Photogenic Name Dropper
    JohnB47 said:
    Old programmer in me is shouting "get the input data validation tightened up".

    Makes this type of problem unnecessarily hard if the conditional tests can have any old rubbish thrown at them. 
    Not sure what you mean by validation but in my case, the input is derived from a CSV file supplied by my credit card company. I and maybe they, have no control over how the various companies supply their names. Sometimes the same company name is uppercase, sometimes it's lower, sometimes mixed, sometimes with additional characters. No idea why it changes.
    No chance they also supply a company identifier code as well as the name? CoRegNo would be good. 
  • JohnB47
    JohnB47 Posts: 2,668 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Thanks for the ideas. I'll look into those.
  • Vitor
    Vitor Posts: 648 Forumite
    500 Posts First Anniversary Photogenic Name Dropper
    edited 6 January at 5:38PM
    Depending on size/scope of system, you'd generally not put the literal text of the supplier name in the Spend table, but instead had a supplier table with unique numeric code for each supplier, along with the standard name text. Possibly also a 'parent' code field so you can build a hierarchy of suppliers. The supplier's code number would go into the Spend table.

    Of course your input data processing will need to a process to add new suppliers and correct variations on the name of existing suppliers to link to their code number.
  • flaneurs_lobster
    flaneurs_lobster Posts: 6,570 Forumite
    Sixth Anniversary 1,000 Posts Photogenic Name Dropper
    Vitor said:
    Depending on size/scope of system, you'd generally not put the literal text of the supplier name in the Spend table, but instead had a supplier table with unique numeric code for each supplier, along with the standard name text. Possibly also a 'parent' code field so you can build a hierarchy of suppliers. The supplier's code number would go into the Spend table.

    Of course your input data processing will need to a process to add new suppliers and correct variations on the name of existing suppliers to link to their code number.
    Back in the day that chunk of analysis would have cost one of my clients a day's billing.....
  • Shimrod
    Shimrod Posts: 1,163 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    JohnB47 said:
    Thanks for the quick replies folks. Obviously I'll play around with those suggestions but I need to check my understanding.

    A couple of you have referred to 'changing' the case and "Another vote to change case using upper or lower." I don't actually want to change the entries in the "Description" field. I want the SQL to inspect and find those records where that field has, say Fred, or FRED or fred in them and then carry out a procedure accordingly.

    Anyway, are you saying that this code would work (given my simple example):

    UPDATE "tblSpend"

    SET "Type" = '4'

    WHERE (upper("Description") Like upper('%Freds Groceries%'))

    I can't quite see why that would do what I want. But then what do I know! Thanks again.



    What flavour of database are you using? Postgresql and Snowflake both have an ilike operator which performs a case insensitive compare. So your query becomes: 

    UPDATE "tblSpend"

    SET "Type" = '4'

    WHERE ("Description" iLike '%Freds Groceries%')



  • JohnB47
    JohnB47 Posts: 2,668 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    edited 6 January at 7:38PM
    Shimrod said:
    JohnB47 said:
    Thanks for the quick replies folks. Obviously I'll play around with those suggestions but I need to check my understanding.

    A couple of you have referred to 'changing' the case and "Another vote to change case using upper or lower." I don't actually want to change the entries in the "Description" field. I want the SQL to inspect and find those records where that field has, say Fred, or FRED or fred in them and then carry out a procedure accordingly.

    Anyway, are you saying that this code would work (given my simple example):

    UPDATE "tblSpend"

    SET "Type" = '4'

    WHERE (upper("Description") Like upper('%Freds Groceries%'))

    I can't quite see why that would do what I want. But then what do I know! Thanks again.



    What flavour of database are you using? Postgresql and Snowflake both have an ilike operator which performs a case insensitive compare. So your query becomes: 

    UPDATE "tblSpend"

    SET "Type" = '4'

    WHERE ("Description" iLike '%Freds Groceries%')



    Interesting. It's  LibreOffice Base database. I'm very much an amateur with it. I'll check if it has that operator
  • onomatopoeia99
    onomatopoeia99 Posts: 7,159 Forumite
    Part of the Furniture 1,000 Posts Name Dropper
    Vitor said:
    Depending on size/scope of system, you'd generally not put the literal text of the supplier name in the Spend table, but instead had a supplier table with unique numeric code for each supplier, along with the standard name text. Possibly also a 'parent' code field so you can build a hierarchy of suppliers. The supplier's code number would go into the Spend table.

    Of course your input data processing will need to a process to add new suppliers and correct variations on the name of existing suppliers to link to their code number.

    Or "use third normal form in your database design".
    Proud member of the wokerati, though I don't eat tofu.Home is where my books are.Solar PV 5.2kWp system, SE facing, >1% shading, installed March 2019.Mortgage free July 2023
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 351K Banking & Borrowing
  • 253.1K Reduce Debt & Boost Income
  • 453.6K Spending & Discounts
  • 244.1K Work, Benefits & Business
  • 599K Mortgages, Homes & Bills
  • 177K Life & Family
  • 257.4K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.1K Discuss & Feedback
  • 37.6K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.