平面文件的多对一加入帮助

发布于 2024-08-03 11:35:47 字数 698 浏览 6 评论 0原文

我不确定是否已经有人问过这个问题——我环顾四周但什么也没找到。

我的最终结果是将多个 SQL 数据库表中的数据获取到逗号分隔的平面文件中。通常这不会是一个问题,但由于它们的多对一关系以及我被迫将其放入事实证明的格式。

第一,我有一张表,即报告表,其中包含报告的所有基本信息。例如

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

,第二个我有另一个表,其中包含每个报告的案例。

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

第三,我有一个平面文件定义,其中包含以下内容:

ReportID, 报告月, 报告详情, 举报案例1, 举报案例2, ReportCase3

我需要做的是添加从 tblReportCasesExample 中获取前三个案例作为报告,以某种方式将其与 tblReportExample 中的数据连接起来,并将其作为 ReportCase1、2 和 3 添加到平面文件中。

我一直在研究这个东西一整天都搞不明白。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怕倦 2024-08-10 11:35:47

建议更新您的问题:不仅仅是“平面文件”,而是“CSV 文件”。据推测,CSV 文件是导出到另一个系统,或者只是导出到喜欢在 Excel 中查看内容的人:-)

来自老前辈的另一条评论(意思是,做过大量文件传输系统集成工作的人):并非所有world是一个SQL数据库。特别是如果您不需要“ACID”来执行任务(除了围绕初始提取的事务)。

将两个表(的相关部分)转储到一对 CSV 文件中。然后“按程序”组装最终的 CSV 文件。实用提取和报告语言(又名“perl”)是处理此类事情的好工具,但还有其他工具。

将“tblReportCasesExample”数据读入某种索引数据结构中。

迭代“tblReportExample”数据:

  • 选择您“最喜欢的”tblReportCasesExample 条目

  • 转储 tblReportExample 字段,然后从相关 tblReportCasesExample 行中选择(键)值/lines。

如果数据中有任何特殊字符,请查找 CSV 处理库。否则,只需使用“split”(perl 和 java 都有类似的东西,我怀疑 .NET 库也有)。

需要更多细节/护理吗?

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?

只是偏爱你 2024-08-10 11:35:47

请确保您在此处使用正确的 ID。我对你所追求的感到困惑。

据我了解,您的问题是以下两个之一:

1。
一份报告指向多个案件
案例可以分配给

需要多对多连接的多个报告。

2.
一份报告指向多个案件
单个案例可以分配给

一对多的

单个报告在第二点中,我看不出是什么导致了您的麻烦,请详细说明。

Please make sure you're using the correct ids here. I'm confused with what you're after.

As I could understand your problem is either one of these two:

1.
single report points to multiple cases
cases can be assigned to multiple reports

that'd require a many-to-many connection.

2.
single report points to multiple cases
single case can ba assigned to a single report

that's one-to-many

In the 2nd point, I can't see what causes you the trouble, please elaborate.

茶色山野 2024-08-10 11:35:47

案例表中的 ReportID 与报告表中的 ReportID 相同。场景2是我的问题。我可以得到一个带有左外连接的案例,但找不到一种创造性的方法来获得其他两个:

SELECT re.ReportID, re.ReportMonth, re.ReportDeails, rce.ReportCase 
FROM tblReportExample re 
LEFT OUTTER JOIN tblReportCasesExample rce 
ON(re.ReportID = rce.ReportID)

The ReportID in the case table is the same ReportID in the reports table. Scenario 2 is my problem. I can get one case with a left outter join but can't find a creative way to get to the other two:

SELECT re.ReportID, re.ReportMonth, re.ReportDeails, rce.ReportCase 
FROM tblReportExample re 
LEFT OUTTER JOIN tblReportCasesExample rce 
ON(re.ReportID = rce.ReportID)
玩套路吗 2024-08-10 11:35:47

您只需要将更多案例添加到案例表中,并添加一个 id 来区分它们:

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

导致我困惑的是为什么您需要文本文件?

You just need to add more cases to the cases table, and add an id to distinguish them:

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

What causes my confusion is why you need the text file?

极度宠爱 2024-08-10 11:35:47

我还没有在 MS SQL Server 上对此进行测试(这是我从您使用 smalldatetime 数据类型推断出来的),但我看过一些关于可以使用 FOR XML PATH 实现的技巧的文章

SELECT r.*,
  (SELECT TOP 3 c.ReportCase + ',' AS [text()]
   FROM tblReportCasesExample c
   WHERE c.ReportId = r.ReportId
   FOR XML PATH('')
  ) AS ReportCaseList
FROM tblReportExample r;

结果应包含 tblReportExample 列,以及前三个报告案例的逗号分隔字符串。然后,也许在 CSV 文件中,您不会知道某些逗号是该字符串的一部分,而不是分隔列。 :-)

I haven't tested this on MS SQL Server (which I infer from your use of the smalldatetime data type), but I've seen some articles about tricks you can do with FOR XML PATH.

SELECT r.*,
  (SELECT TOP 3 c.ReportCase + ',' AS [text()]
   FROM tblReportCasesExample c
   WHERE c.ReportId = r.ReportId
   FOR XML PATH('')
  ) AS ReportCaseList
FROM tblReportExample r;

The result should have the columns of tblReportExample, plus a comma-separated string of the top three report cases. Then perhaps in the CSV file, you won't know that some of the commas were part of that string instead of separating columns. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文