Unusual SQL Dataset I Can't Figure out How to Use

Hi guys,

I have been practicing data manipulation in SQL, and I have been provided with a data source I can't seem to make sense of. I was hoping there might be someone more familiar here that might be able to point me in the right direction. I need to work on some SQL queries on the dataset, but I haven't the faintest idea on where to start.

The data looks like this for instance:

Company Code - Field - Value (3 fields)

1001 - Vendor Name - 7 Eleven
1001 - Vendor Name - Bob Jane
1001 - Vendor Name - Krispy Kreme
1001 - Vendor Address - 102 Reservoir Street
1001 - Vendor Address - 110 Pitt Road
1001 - Vendor Address - 23 Foxy Place

Usually, I would expect to see it in a somewhat relational type of table like

Company Code Vendor Name Vendor Address
1001 7 Eleven 102 Reservoir Street.
1001 Bob Jane 110 Pitt Road.

Comments

  • -3

    Yes, ask about SQL on a website made for finding bargains…

    Because you know… Google is too hard, so can't find a more relevant website… Like… Oooohhh I dunno…. https://stackoverflow.com

  • +1

    Are you sure all company code's are 1001? The data would be usable with a unique identifier of the vendor, but without one it's difficult.

    • Yes, I was expecting a primary key of some sort - but none exist.

      • +1

        You can translate the rows into "Vendor Name" and "Vendor Address" columns with CASE statements e.g. something like:

        SELECT "Company Code",
        max(CASE WHEN Field = 'Vendor Name' THEN Value END) AS Vendor_Name,
        max(CASE WHEN Field = 'Vendor Address' THEN Value END) as Vendor_Address
        from table
        GROUP BY "Company Code"

        but that would only work if company code was unique per vendor. Without a unique key I can't see what you can do with the data.

  • +1

    Is there something else? How is one to know which address applies to which company?

  • +1

    Looks like this data came from SAP and I am guessing from the SAP vendor change report. Agreed with Xyzzy - it needs a vendor number. Whoever gave you this data neglected to provide that.

    I think once you have that and assuming this is MS SQL you could use the SQL pivot command:

    https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

    Just use google as Drew22 suggested.

    Let me guess - looking for a job working in audit analytics?

    • Thanks for the suggestions guys, I have concluded it is a broken table. You are right though - assuming it was supposed to be that way - it could look like CDHDR or CDPOS.

      Assuming each field had a primary key, I would have been able to recompose the table. It was actually an Employee Master file, but I simplified it for the explanation.

      Already in Audit Analytics, but I don't personally use SQL for it.

      • Do you use something like Idea or ACL?

  • +1

    That's EAV (Entity, Attribute, Value) format; as mentioned you could use PIVOT to get to what you'd prefer but someone has cocked up the 'Entity' column.

    Send it back and ask for a refund.

    Complain to the ACCC as well, which seems to be a popular theme here at the moment for minor grievances.

    • -1

      You forgot about the bikies.

Login or Join to leave a comment