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 |
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2010-09-28 : 10:03:33
|
Hi, I have the following select statement which works fine, however, know I want it to match to columns and display the amoount on a final table, e.g., ProjectID Vision_PatId Paystatus FullFee PartFee1 7633 Full Pay 100.00 50.002 9273 Part Pay 100.00 50.00 3 8887 Full Pay 100.00 50.00The above example is provided by the select statement below, what I want to have also is another column called Gross Amount asProjectID Vision_PatId Paystatus FullFee PartFee G.Amount1 7633 Full Pay 100.00 50.00 100.002 9273 Part Pay 100.00 50.00 50.003 7633 Full Pay 120.00 50.00 120.00Notice it checks if its Full Pay then it inserts 100.00 and 120.00 but if it is Part Fee it inserts 50.00. Finally, if the Vision_PatId is the same it sums them, and for the above example the answer will be 220.00 for vision_patid = 7633. Any help please............ Many thanks SELECT dbo.qrypreviewpayment.ProjectID, dbo.qrypreviewpayment.Vision_PatID, dbo.qrypreviewpayment.PayStatus, dbo.tblProjects.FullFee, dbo.tblProjects.PartFeeFROM dbo.tblProjects INNER JOIN dbo.qrypreviewpayment ON dbo.tblProjects.ProjectID = dbo.qrypreviewpayment.ProjectID |
|
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-09-28 : 10:47:20
|
| Try this:SELECT dbo.qrypreviewpayment.ProjectID, dbo.qrypreviewpayment.Vision_PatID, dbo.qrypreviewpayment.PayStatus, dbo.tblProjects.FullFee, dbo.tblProjects.PartFee,Case when dbo.qrypreviewpayment.PayStatus = 'Full Pay' then dbo.tblProjects.FullFeeelsedbo.tblProjects.PartFeeEnd as GrossAmount FROM dbo.tblProjects INNER JOIN dbo.qrypreviewpayment ON dbo.tblProjects.ProjectID = dbo.qrypreviewpayment.ProjectID |
 |
|
|
dr223
Constraint Violating Yak Guru
444 Posts |
Posted - 2010-09-28 : 11:32:50
|
Thanks, re-generated my code to SELECT TOP (100) PERCENT dbo.tblProjects.ProjectID, dbo.tblProjectPatients.Prac_no, dbo.tblProjectPatients.PayStatus, CASE WHEN dbo.tblProjectPatients.PayStatus = 'Full Pay' THEN dbo.tblProjects.FullFee WHEN dbo.tblProjectPatients.PayStatus = 'Part Pay' THEN dbo.tblProjects.PartFee ELSE 0.00 END AS AmountPerPatientFROM dbo.tblProjects INNER JOIN dbo.tblProjectPatients ON dbo.tblProjects.ProjectID = dbo.tblProjectPatients.ProjectIDGROUP BY dbo.tblProjects.ProjectID, dbo.tblProjectPatients.Prac_no, dbo.tblProjectPatients.PayStatus .. This works fine. One last thing, I may have a result like; ProjectID Prac_no PayStatus Amountoperpatient1 100 Full Pay 50.001 100 Part Pay 25.001 102 Full Pay 70.00I would like to have a result as shown below; ProjectID Prac_no GrossAmount1 100 75.001 102 70.00Note for prac_no = 100 it adds 50.00 + 25.00 = 75.00many thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-09-30 : 12:25:13
|
| [code]SELECT TOP (100) PERCENT dbo.tblProjects.ProjectID, dbo.tblProjectPatients.Prac_no, SUM(CASE WHEN dbo.tblProjectPatients.PayStatus = 'Full Pay' THEN dbo.tblProjects.FullFee WHEN dbo.tblProjectPatients.PayStatus = 'Part Pay' THEN dbo.tblProjects.PartFee ELSE 0.00 END) AS AmountPerPatientFROM dbo.tblProjects INNER JOIN dbo.tblProjectPatients ON dbo.tblProjects.ProjectID = dbo.tblProjectPatients.ProjectIDGROUP BY dbo.tblProjects.ProjectID, dbo.tblProjectPatients.Prac_no[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|