Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-18 : 05:04:24
|
Hi Guys, I have found this a couple of times throughout the forum but cant figure out how to amend it to my query! In a nutshell there are 3 departments that have a per piece cost (this is stored in ic.[Cost_Per_Unit]) which can be joined on ic.[Department] and dc.[Department]. I would like a single column per [Patient_ID] for these Indirect Costs based on a count of how many there are by [Patient_ID]. The code I have so far is:Select dc.Patient_ID,dc.Department,count(dc.Amount) as "count", sum(case when dc.department in ('IMG','PAT','PHY') then (count(dc.amount) * ic.Cost_Per_Unit) else '0' end) as "Indirect Costs"from DirectCosts dcleft join IndirectCosts ic on dc.Department = ic.departmentgroup by dc.Patient_ID,dc.Department,ic.Cost_Per_Unit,ic.Departmentorder by Patient_IDThis is failing as there is a sum in the case statement but I cant figure out how to change this to further joins etc.If anyone can suggest a better way to get this result I would be eternally grateful! Many thanksDanIf you cant sleep at night, its not the coffee its the bunk! |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-18 : 07:19:08
|
It seems to me like the count inside the case is obsolete:Select dc.Patient_ID,dc.Department,count(dc.Amount) as "count", sum(case when dc.department in ('IMG','PAT','PHY') then (dc.amount * ic.Cost_Per_Unit) else 0 end) as "Indirect Costs"from DirectCosts dc left join IndirectCosts ic on dc.Department = ic.departmentgroup by dc.Patient_ID,dc.Departmentorder by Patient_ID- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-18 : 07:34:09
|
quote: Originally posted by Lumbago It seems to me like the count inside the case is obsolete:Select dc.Patient_ID,dc.Department,count(dc.Amount) as "count", sum(case when dc.department in ('IMG','PAT','PHY') then (dc.amount * ic.Cost_Per_Unit) else 0 end) as "Indirect Costs"from DirectCosts dc left join IndirectCosts ic on dc.Department = ic.departmentgroup by dc.Patient_ID,dc.Departmentorder by Patient_ID- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein
The count is needed to tell the calculation how many exist for the particular patient to multiply the cost_per_unit.If the count is not there then it will multiply by the count of the items in the table.ThanksIf you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-18 : 08:11:17
|
| Hm...maybe my head is not keeping up today but are you sure? The SUM around the case-statement should handle that...can you try it?- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-18 : 09:10:35
|
quote: Originally posted by Lumbago The SUM around the case-statement should handle that...can you try it?
Tried it again and it kind of does but the result is not what im after.Here is a snippet from the resultsPatient_ID | Department | Count | Indirect Cost0001883 IMG 4 30036.130001883 PHY 3 1335.840001920 PAT 7 39343.6252It is counting correctly from the source so the calculation in the script should be:4 x IMG cost_per_unit3 x PHY cost_per_unit7 x PAT cost_per_unitHowever the results are quite a way out as the cost_per_unit is a very small number (between 10 / 20) so the highest figure I would expect is around 105.50.It is instead just multiply the figure in dc.[amount] by the cost_per_unit and not the number of occurrence of each department. The field dc.amount is irrelevant for the calculation just a field I picked to count the number of for the query.--- On a little aside the next step I am looking at is combining all of the patient_id's to get a single figure for Indirect Costs which after a little playing with distinct is not really working!!I hope this help as I am stumped! If you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-18 : 10:15:35
|
| I'm feeling awfully slow here but it would be a lot better if you could post some sample data and your expected output. Please create the sample data with full create table/insert statements and your expected output in the exact format your'e looking for...- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-18 : 11:37:32
|
quote: Originally posted by Lumbago I'm feeling awfully slow here but it would be a lot better if you could post some sample data and your expected output. Please create the sample data with full create table/insert statements and your expected output in the exact format your'e looking for...- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein
USE [DBName]GO/****** Object: Table [dbo].[IndirectCosts] Script Date: 03/18/2010 15:02:18 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[IndirectCosts]( [Indirect_Item] [char](20) NOT NULL, [Department] [char](3) NULL, [Apportionment] [char](10) NOT NULL, [Cost_Per_Unit] [float] NOT NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOUSE HMT_TheatreINSERT INTO DBName.dbo.IndirectCostsSELECT 'Imaging' as "Indirect_Itemt",'IMG' as "Department",'Test' as "Apportionment",'13.45' as "Cost_Per_Unit"INSERT INTO DBName.dbo.IndirectCostsSELECT 'Pathology' as "Indirect_Itemt",'PAT' as "Department",'Test' as "Apportionment",'5.06' as "Cost_Per_Unit"INSERT INTO DBName.dbo.IndirectCostsSELECT 'Physio' as "Indirect_Itemt",'PHY' as "Department",'Test' as "Apportionment",'7.26' as "Cost_Per_Unit"GOUSE [DBName]GOUSE [DBName]GO/****** Object: Table [dbo].[DirectCosts] Script Date: 03/15/2010 15:06:05 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[DirectCosts]( [Patient_ID] [char](7) NOT NULL, [Episode_ID] [char](7) NOT NULL, [Invoice] [char](8) NOT NULL, [Department] [char](3) NOT NULL, [Amount] [numeric](13, 2) NOT NULL, [Direct_Item] [char](10) NOT NULL, [Description] [varchar](110) NOT NULL, [Markup] [char](3) NOT NULL, [Site_Name] [char](25) NOT NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOUSE HMT_TheatreINSERT INTO DBName.dbo.DirectCostsSELECT '9083229' as "Patient_ID",'0286719' as "Episode_ID",'I0093517' as "Invoice",'PHA' as "Department",'0.00' as "Amount", 'PY999' as "Direct_Item", 'Dressings' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '7001619' as "Patient_ID",'0284360' as "Episode_ID",'I0093584' as "Invoice",'PHY' as "Department",'19.00' as "Amount", 'PH999' as "Direct_Item", 'Scan' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9079542' as "Patient_ID",'0279904' as "Episode_ID",'I0093544' as "Invoice",'IMG' as "Department",'15.00' as "Amount", 'IMG999' as "Direct_Item", 'Treatment' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9079542' as "Patient_ID",'0279904' as "Episode_ID",'I0093501' as "Invoice",'PHY' as "Department",'15.00' as "Amount", 'PH999' as "Direct_Item", 'Treatment' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9079542' as "Patient_ID",'0279904' as "Episode_ID",'I0093536' as "Invoice",'PHY' as "Department",'15.00' as "Amount", 'PH999' as "Direct_Item", 'Treatment' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9079542' as "Patient_ID",'0279904' as "Episode_ID",'I0093547' as "Invoice",'PHY' as "Department",'15.00' as "Amount", 'PH999' as "Direct_Item", 'Treatment' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9079542' as "Patient_ID",'0283397' as "Episode_ID",'I0093578' as "Invoice",'PHY' as "Department",'15.00' as "Amount", 'PH999' as "Direct_Item", 'Treatment' as "Description", '0' as "Markup", 'CCC' as "Site_Name"INSERT INTO DBName.dbo.DirectCostsSELECT '9009769' as "Patient_ID",'0281166' as "Episode_ID",'I0093536' as "Invoice",'PAT' as "Department",'26.00' as "Amount", 'TS999' as "Direct_Item", 'Other' as "Description", '0' as "Markup", 'CCC' as "Site_Name"GOThese are the two data source tables and I would like to populate this table with the results of the query I am struggling with:USE [DBName]GO/****** Object: Table [dbo].[Central] Script Date: 03/18/2010 15:01:49 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Central]( [Patient_ID] [char](7) NOT NULL, [Episode_ID] [char](7) NOT NULL, [Site_Name] [char](25) NOT NULL, [Consultant] [varchar](110) NULL, [Anaethetist] [varchar](110) NULL, [Procedure_Code] [char](10) NOT NULL, [Direct_Costs] [float] NOT NULL, [Indirect_Costs] [float] NOT NULL, [Total_Cost] [float] NOT NULL) ON [PRIMARY]GOSET ANSI_PADDING OFFGOThe results I want in the central table are 5 lines based on the Episode_ID:1. 9083229 0286719 CCC AB01 AC01 W12345 0.00 7.21 7.212. 7001619 0284360 CCC AB01 AC01 W12345 19.00 7.21 26.213. 9079542 0279904 CCC AB01 AC01 W12345 60.00 53.80 113.804. 9079542 0283397 CCC AB01 AC01 W12345 15.00 5.06 20.065. 9009769 0281166 CCC AB01 AC01 W12345 26.00 0.00 26.00This is based on the 'Amount' being the 'Direct Cost' and the sum of the count * cost per item being the 'Indirect Cost'The Total is Direct + Indirect.It should group by Episode_ID and not Patient_ID.I hope this help clarify my issue!Thanks for helping!DanIf you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-19 : 04:06:37
|
| I'm sorry but I can't get the expected output you posted to make any sense at all. How do you compute the last two columns? 7.21 is not anywhere in the sample data you posted...- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-19 : 06:16:09
|
quote: Originally posted by Lumbago I'm sorry but I can't get the expected output you posted to make any sense at all. How do you compute the last two columns? 7.21 is not anywhere in the sample data you posted...
Morning,The columns are calculated (hopefully) as follows:[Patient_ID] - Pulled from DirectCosts[Episode_ID] - pulled from DirectCosts[Site_Name] - Pulled from DirectCosts[Consultant] - Pulled from another table[Anaethetist] - Pulled from another table[Procedure_Code] - Pulled from another table[Direct_Costs] - Sum of 'Amount' in DirectCosts for the same Epiosde_ID[Indirect_Costs] - Based on Department multiply the number of instances by the IndirectCosts.dbo.Cost_Per_Unit field. This is only for PHY,IMG and PHA all other departments = 0[Total_Cost] - Direct_Costs + Indirect_CostsThe confusing part is the Indirect_Costs. In the examples posted we have 5 individual cases through 8 lines in Direct_Costs based on the Episode_ID.For Episode_ID 0279044 we have 4 lines. 3 of them refer to the PHY department and 1 is the IMG department. Therefore the indirect_costs for this episode are (3 x 7.26) + (1 x 13.45) = 35.23The direct costs for these are all 15 so 15 x 4 = 60 + 35.23 = 95.23HUGE APOLOGY - As I was just working this out I realised that my expected results were wrong. They should be as follows:1. 9083229 0286719 CCC AB01 AC01 W12345 00.00 00.00 00.002. 7001619 0284360 CCC AB01 AC01 W12345 19.00 07.26 26.263. 9079542 0279904 CCC AB01 AC01 W12345 60.00 35.23 95.234. 9079542 0283397 CCC AB01 AC01 W12345 15.00 07.26 22.265. 9009769 0281166 CCC AB01 AC01 W12345 26.00 05.06 31.06I hope this makes a bit more sense. Its slowly driving me insane!!!Thank you so much for your patience and help!DanIf you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-19 : 07:44:51
|
Ok, I think I got it...took me a good while to understand your logic though:SELECT DISTINCT dc.Patient_ID, dc.Episode_ID, Direct_Costs, Indirect_Costs, TotalCost = Direct_Costs + Indirect_CostsFROM directCosts dc INNER JOIN ( SELECT Episode_ID, Direct_Costs = SUM(Amount), Indirect_Costs = COALESCE(SUM(CASE WHEN b.Department IN ('IMG','PAT','PHY') THEN Cost_Per_unit ELSE 0 END), 0) FROM directCosts a LEFT OUTER JOIN IndirectCosts b ON a.Department = b.Department GROUP BY Episode_ID ) dt ON dt.Episode_ID = dc.Episode_IDORDER BY dc.Patient_ID- LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-19 : 09:28:19
|
quote: Originally posted by Lumbago Ok, I think I got it...took me a good while to understand your logic though:SELECT DISTINCT dc.Patient_ID, dc.Episode_ID, Direct_Costs, Indirect_Costs, TotalCost = Direct_Costs + Indirect_CostsFROM directCosts dc INNER JOIN ( SELECT Episode_ID, Direct_Costs = SUM(Amount), Indirect_Costs = COALESCE(SUM(CASE WHEN b.Department IN ('IMG','PAT','PHY') THEN Cost_Per_unit ELSE 0 END), 0) FROM directCosts a LEFT OUTER JOIN IndirectCosts b ON a.Department = b.Department GROUP BY Episode_ID ) dt ON dt.Episode_ID = dc.Episode_IDORDER BY dc.Patient_ID
Words have completely escaped me at the genius you have just shown!I am eternally grateful. Thank you thank you thank you for you endless patience.This is exactly what I was after!If you were local I would buy you a pint!Have a wonderful weekend!Thanks againDanIf you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-19 : 09:35:12
|
Great thing that it worked out! Notice that there is no COUNT anywhere though..I hope you take the time to understand what is really going on. Have a good weekend and a pint for me - LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
sql_dan
Starting Member
43 Posts |
Posted - 2010-03-19 : 10:18:17
|
quote: Have a good weekend and a pint for me 
I certainly will. I do understand the majority of it I just didn't know you could do subqueries within Joins!This will open up a few more things I am working on!!!!I need to get myself on a T-SQL course!! cheers! If you cant sleep at night, its not the coffee its the bunk! |
 |
|
|
|
|
|
|
|