Files
linqpad/JAZZ-14426 Create SQL.linq
T

79 lines
3.1 KiB
C#

<Query Kind="Program">
<Connection>
<ID>bd4e97c5-84e7-4c61-80ca-8d53d7876af2</ID>
<Persist>true</Persist>
<Server>D0001-SQL-11.sdt.local</Server>
<Database>jazz dev master Prod</Database>
<ShowServer>true</ShowServer>
</Connection>
<Namespace>System.Globalization</Namespace>
</Query>
void Main()
{
var ti = CultureInfo.CurrentCulture.TextInfo;
var _2020MSDRGs = (from x in DimMSDRGs where x.DRGSchema == "2020MSDRG" select x.MSDRGCode).Distinct();
var _caseTypeFamilyMappingICD10 = (from x in CaseTypeFamilyMappingICD10s select x.MSDRGCode).Distinct();
var _ctfGlobalID = (from x in CaseTypeFamilies where x.CaseTypeFamilyGlobalID.StartsWith("M-") select x.CaseTypeFamilyGlobalID)
.ToList()
.Where(x => Regex.IsMatch(x, @"M-\d+"))
.Select(x => int.Parse(Regex.Replace(x, @"M-(\d+)", "$1")))
.OrderByDescending(x => x)
//.Select(x => $"M-{x}")
.First();
//_ctfGlobalID.Dump();
var part1 = (from msdrg in DimMSDRGs
where msdrg.DRGSchema == "2021MSDRG"
&& !_2020MSDRGs.Contains(msdrg.MSDRGCode)
select new
{
msdrg.MSDRGCode,
CaseTypeFamilyName = ti.ToTitleCase(msdrg.Description.ToLower())
})
.ToList();
var part2 = (from msdrg in DimMSDRGs
where msdrg.DRGSchema == "2021MSDRG"
&& !_caseTypeFamilyMappingICD10.Contains(msdrg.MSDRGCode)
select new
{
msdrg.MSDRGCode,
CaseTypeFamilyName = ti.ToTitleCase(msdrg.Description.ToLower())
})
.ToList();
var results = part1.Union(part2).ToList()
.ConvertAll(x =>
{
_ctfGlobalID++;
return new
{
CaseTypeFamilyGlobalID = $"M-{_ctfGlobalID}",
x.MSDRGCode,
x.CaseTypeFamilyName
};
});
//results.Dump("results");
var deleteCaseTypeFamily = results
.Aggregate("", (x, y) => $"{x}\r\nDELETE FROM dss.CaseTypeFamily WHERE Name = '{y.CaseTypeFamilyName}' AND CaseTypeFamilyGlobalID = '{y.CaseTypeFamilyGlobalID}'");
var insertCaseTypeFamily = results
.Aggregate($"INSERT INTO dss.CaseTypeFamily ( CaseTypeFamilyGUID, Name, CaseTypeFamilyGlobalID )\r\nVALUES",
(x, y) => $"{x}\r\n('{Guid.NewGuid()}', '{y.CaseTypeFamilyName}', '{y.CaseTypeFamilyGlobalID}'),").TrimEnd(",".ToCharArray());
var deleteICD10 = results
.Aggregate("", (x, y) => $@"{x}
DELETE FROM dss.CaseTypeFamilyMappingICD10
WHERE EXISTS(SELECT 1 FROM dss.CaseTypeFamily ctf
WHERE ctf.CaseTypeFamilyGlobalID = '{y.CaseTypeFamilyGlobalID}' AND SkipICD = 1 AND ctf.CaseTypeFamilyID = CaseTypeFamilyMappingICD10.CaseTypeFamilyID)
AND CaseTypeFamilyMappingICD10.MSDRGCode = '{y.MSDRGCode}' AND CaseTypeFamilyVersionID = 2");
var insertICD10 = results
.Aggregate($"INSERT INTO dss.CaseTypeFamilyMappingICD10 ( CaseTypeFamilyID, SkipICD, ICD10Code, MSDRGCode, CaseTypeFamilyVersionID )\r\nVALUES",
(x, y) => $"{x}\r\n((SELECT ctf.CaseTypeFamilyID from dss.CaseTypeFamily ctf where ctf.CaseTypeFamilyGlobalID = '{y.CaseTypeFamilyGlobalID}'), 1, '0', '{y.MSDRGCode}', 2),").TrimEnd(new[] { ',' });
var migration =
deleteCaseTypeFamily + "\r\nGO\r\n" +
insertCaseTypeFamily + "\r\nGO\r\n" +
deleteICD10 + "\r\nGO\r\n" +
insertICD10 + "\r\nGO\r\n";
migration.Dump();
}
// Define other methods and classes here