当ID相同时,如何将多行合并为单行,每行数据的列不同

时间:2019-04-08 15:44:59

标签: c# sql sql-server

我有多个内部联接的结果。我想将此行合并为1行,并将行中的每个数据附加列。

Select fp.idfp, fp.idperson, p.fname+' '+p.mname+' '+p.lname as Patient,
fp.date, m.idmed, n.medname+' '+n.meddosage+' '+n.medtype as MedicineName, m.quantity, m.date as [Date Dispense]
from FPTRANSAC fp
Inner Join PRECORD p
On fp.idperson = p.idperson

inner join MEDOUT m
ON fp.idfp = m.idtransac

inner join NEWMED n
on m.idmed = n.idmed

union all

Select i.idimmu, i.idperson, p.fname+' '+p.mname+' '+p.lname as Patient,
i.date, m.idmed, n.medname+' '+n.meddosage+' '+n.medtype as MedicineName, m.quantity, m.date as [Date Dispense]
from IMMUTRANSACTION i
Inner Join PRECORD p
On p.idperson = i.idperson

inner join MEDOUT m
ON i.idimmu = m.idtransac

inner join NEWMED n
on m.idmed = n.idmed

这是结果:

idfptran | idperson | Patient     | date     | idmed | MedicineName  | quantity | Date Dispense
F-1      | 00001    | Jenny Jones | datehere | 1     | Cetirizine    | 5        | datehere
F-1      | 00001    | Jenny Jones | datehere | 3     | Tylenol       | 8        | datehere
I-1      | 00015    | Mark Sawyer | datehere | 2     | Salbutamol    | 2        | datehere  
I-1      | 00015    | Mark Sawyer | datehere | 4     | Amoxicillin   | 3        | datehere
I-1      | 00015    | Mark Sawyer | datehere | 7     | Carbocisteine | 3        | datehere  

我想要这样的表,但我不知道是否可能。你能帮忙吗?我已经尝试了好几天了:(请帮忙。我正在使用mssql。对于Date Dipense,它可以获取具有相同ID或最后一个ID的最后一行的值。

idfptran | idperson | Patient     | date     | idmed | MedicineName  | quantity | idmed | MedicineName | quantity | idmed | MedicineName | quantity | Date Dispense
F-1      | 00001    | Jenny Jones | datehere | 1     | Cetirizine    | 5        | 3     | Tylenol      | 8        |       |              |          | datehere
I-1      | 00015    | Mark Sawyer | datehere | 2     | Salbutamol    | 2        | 4     | Amoxicillin  | 3        | 7     | Carbocisteine| 3        | datehere

2 个答案:

答案 0 :(得分:1)

我建议先将所有与人相关的数据收集到一个表或查询中,然后再使用下一层中其他源表中的其他数据(如idmed,医学名称,...)来收集,而不是建立一个联合。每个源的外部联接。

答案 1 :(得分:0)

这是示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication108
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("idfptran", typeof(string));
            dt.Columns.Add("idperson", typeof(string));
            dt.Columns.Add("Patient", typeof(string));
            dt.Columns.Add("date", typeof(DateTime));
            dt.Columns.Add("idmed", typeof(int));
            dt.Columns.Add("MedicineName", typeof(string));
            dt.Columns.Add("quantity", typeof(int));
            dt.Columns.Add("Date Dispense", typeof(DateTime));
            dt.Rows.Add(new object[] {"F-1", "00001", "Jenny Jones", DateTime.Parse("4/8/19"), 1, "Cetirizine", 5, DateTime.Parse("4/8/19")});
            dt.Rows.Add(new object[] {"F-1", "00001", "Jenny Jones", DateTime.Parse("4/8/19"), 3, "Tylenol", 8, DateTime.Parse("4/8/19")});
            dt.Rows.Add(new object[] {"I-1", "00015", " Mark Sawyer", DateTime.Parse("4/8/19"), 2, "Salbutamol", 2, DateTime.Parse("4/8/19")});
            dt.Rows.Add(new object[] {"I-1", "00015", " Mark Sawyer", DateTime.Parse("4/8/19"), 4, "Amoxicillin", 3, DateTime.Parse("4/8/19")});
            dt.Rows.Add(new object[] {"I-1", "00015", " Mark Sawyer", DateTime.Parse("4/8/19"), 7, "Carbocisteine", 3, DateTime.Parse("4/8/19")});

            var groups = dt.AsEnumerable().GroupBy(x => new { idperson = x.Field<string>("idperson"), dispense = x.Field<DateTime>("Date Dispense") }).ToList();

            int maxMedicenes = groups.Select(x => x.Count()).Max(x => x);

            DataTable pivot = new DataTable();
            pivot.Columns.Add("idfptran", typeof(string));
            pivot.Columns.Add("idperson", typeof(string));
            pivot.Columns.Add("Patient", typeof(string));
            pivot.Columns.Add("date", typeof(DateTime));

            for (int i = 1; i <= maxMedicenes; i++)
            {
                pivot.Columns.Add("idmed_" + i.ToString(), typeof(int));
                pivot.Columns.Add("MedicineName_" + i.ToString(), typeof(string));
                pivot.Columns.Add("quantity_" + i.ToString(), typeof(int));
            }
            pivot.Columns.Add("Date Dispense", typeof(DateTime));

            foreach (var group in groups)
            {
                DataRow newRow = pivot.Rows.Add();
                newRow["idfptran"] = group.First().Field<string>("idfptran");
                newRow["idperson"] = group.Key.idperson;
                newRow["Patient"] = group.First().Field<string>("Patient");
                newRow["date"] = group.First().Field<DateTime>("date");

                int i = 1;
                foreach (DataRow row in group)
                {
                    newRow["idmed_" + i.ToString()] = row.Field<int>("idmed");
                    newRow["MedicineName_" + i.ToString()] = row.Field<string>("MedicineName");
                    newRow["quantity_" + i.ToString()] = row.Field<int>("quantity");
                    i++;
                }
                newRow["Date Dispense"] = group.Key.dispense;
            }

        }
    }
}