Many to one join help for flat file

flatflat-filejoin;one-to-many

I’m not sure if this has already been asked – I looked around for quite a bit but couldn’t find anything.

My end result is to get data from several of my SQL database tables into a comma delimited flat file. Normally this would not be a problem but due to their many to one relationship and the format I’m forced to put it in it’s proving to be.

1st I’ve got one table, the Report table which has all basic info for a report. For example

CREATE TABLE tblReportExample
(
    ReportID int,
    ReportMonth smalldatetime,
    ReportDetails varchar(500)
)

2nd I’ve got another table with cases for each report.

CREATE TABLE tblReportCasesExample
(
    ReportID int,
    ReportCase varchar(50)
)

3rd I have a flat file definition with the following:

ReportID,
ReportMonth,
ReportDetails,
ReportCase1,
ReportCase2,
ReportCase3

What I need to do is add take the top three cases from the tblReportCasesExample for a report, join it somehow with the data in tblReportExample and add it to the flat file as ReportCase1, 2 and 3.

I’ve been looking at this thing all day and just can’t figure it out.

Any ideas?

Best Answer

Suggested update to your question: Not just "flat file", but "CSV file". Presumably, the CSV file is an export to another system, or just to somebody who likes to view things in Excel :-)

Another comment from an old timer (meaning, sombody who has done MUCH file transfer system integration work): Not all the world is an SQL database. Especially if you don't need "ACID" for a task (other than a transaction around the initial extract(s)).

Dump (the relevant portions of) the two tables into a pair of CSV files. Then assemble the final CSV file "procedurally". The Practical Extraction and Reporting Language (aka "perl") is a good tool for this sort of thing, but there are others.

Read the "tblReportCasesExample" data into an indexed data structure of some kind.

Iterate through the "tblReportExample" data:

  • select your "favorite" tblReportCasesExample entries

  • dump the tblReportExample fields, followed by the select (key) values from the relevant tblReportCasesExample rows / lines.

Find a CSV handling library if you have any special chars in the data. Otherwise, just use "split" (perl and java both have something like this, I suspect the .NET library does as well).

Need more details / care at all?

Related Topic