将两张表和一张链接表变成一张大表

发布于 2024-11-30 21:40:55 字数 1045 浏览 4 评论 0原文

我尝试寻找这个问题,但没有发现任何相关内容。所以这里...

我有三个表,其极其简化的版本是:

Practitioners:

  • practitioner_id :: int
  • name :: nvarchar

保险:

  • insurance_id :: int
  • name :: nvarchar

InsuranceLink:

  • practitioner_id :: int >
  • insurance_id :: int

因此,从业者表包含从业者列表,保险表包含保险列表,链接表代表哪个从业者支持哪种保险。

现在,我需要创建一个可以显示如下信息的视图:

ViewTable:

  • practitioner_id :: int
  • practitioner_name :: nvarchar
  • Insurance_1 :: 位
  • insurance_2 :: 位 .....
  • insurance_100 :: bit

换句话说,视图中的列是从业者的 ID 和姓名,以及 Insurances 中存在的每项保险(带有保险名称作为列名称(强制条件是保险名称是唯一的))。保险栏中的单元格将指示该从业者是否支持该保险。

有什么办法可以做到这一点吗?

或者更好的是,是否可以在 VB.NET 形式的数据集上使用 Excel 样式的数据透视表?这也能解决我的很多问题。

I tried looking for this question, but found nothing relevant. So here goes...

I have three tables, the extremely simplified versions of which are:

Practitioners:

  • practitioner_id :: int
  • name :: nvarchar

Insurances:

  • insurance_id :: int
  • name :: nvarchar

InsuranceLink:

  • practitioner_id :: int
  • insurance_id :: int

So, the practitioner table contains a list of practitioners, the insurance table contains a list of insurances, and the link table represents which practitioner supports which insurance.

Now, I need to create a view which can display the information like this:

ViewTable:

  • practitioner_id :: int
  • practitioner_name :: nvarchar
  • insurance_1 :: bit
  • insurance_2 :: bit
    .....
  • insurance_100 :: bit

In other words, the columns in the view are the ID and name of the practitioner, and every insurance that exists in Insurances (with the insurance name as the column name (there is an enforced condition that insurance names are unique)). The cells in the insurance columns will indicate if that practitioner supports that insurance.

Is there any way of doing this?

Or better yet, is it possible to use an excel-style pivot table on a DataSet in a VB.NET form? That would also solve many of my problems.

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

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

发布评论

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

评论(1

埋情葬爱 2024-12-07 21:40:56

PIVOT 是您正在寻找的关键字。

select * from 
   (select 
        practitioner_id, 
        practitioner_name, 
        insurance_name
    from practitioner p
    join insurancelink il on p.practitioner_id = il.practitioner_id
    join insurance i on il.insurance_id = i.insurance_id
   )
pivot (count(*) for insurance_name in ([insurancename1],[insurancename2], ..., [insurancename100]))

我知道现在您想知道是否有任何方法可以避免列出所有保险名称/使其不是一个固定列表。答案是否定的,几乎没有。您可以以编程方式创建视图,但添加保险时仍需要更新视图。

PIVOT is the keyword you're looking for.

select * from 
   (select 
        practitioner_id, 
        practitioner_name, 
        insurance_name
    from practitioner p
    join insurancelink il on p.practitioner_id = il.practitioner_id
    join insurance i on il.insurance_id = i.insurance_id
   )
pivot (count(*) for insurance_name in ([insurancename1],[insurancename2], ..., [insurancename100]))

I know that now you're wondering if there's any way to avoid listing out all the insurance names/having it not be a constant list. The answer is no, pretty much. You can create the view programatically but the view is still going to need to be updated when an insurance is added.

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