Question

I have a CSV to convert. Transforming the data into my schema is a bit problematic. I have a locationID in my end table, and in the CSV, there are 1's or a NULL in about 10 columns(each one is a different location).

My logic is: If column("locationThree") == 1, then set locationID to 5 (for example)

I can't figure out how to do this in SQL, is there a tool for this in Pentaho, or how could I do this in SQL?

E.G

INSERT INTO endTable(locationID)
SELECT * FROM grubbyCSVData
if(locationOne == "1")
 locationID = 5;
if(locationTwo == "1")
 locationID = 6;

Is something like that possible?

Was it helpful?

Solution

You might like to try combining the COALESCE and CASE expressions in SQL. Assuming you are using SQL Server something like the following might work. I have not verified that this executes correctly as I've just written it in the browser.

INSERT INTO endTable(locationID)
SELECT COALESCE( CASE locationOne
                 WHEN 1 THEN 5
                 ELSE NULL
                 END,
                 CASE locationTwo
                 WHEN 1 THEN 6
                 ELSE NULL
                 END
                );

Alternatively you might want to look into the PIVOT and UNPIVOT statements which are available in SQL Server 2005 onwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top