Solved

Sql Qry problem

Posted on 2008-06-18
2
253 Views
Last Modified: 2010-03-19
I have  taken a specific example so that i can clearly explain the problem
In the qry mentioned below , it returns 3 rows.

Qry
Select hbdf.caseid,hbdf.DoctorCode,sum(hBdf.Charges)as visitcharges
   From HISBillDoctorFees hBdf
   inner join hisbill hb on hb.caseid = hbdf.caseid
   where billdate between '20080501' and '20080531'
   and billcancelled = 0  and billno <> 0 and branch = 2
   and hbdf.caseid = 67161
   Group by hbdf.caseid,doctorcode
O/p
caseid      DoctorCode      visitcharges
67161      1      0
67161      70      500
67161      164      0

For this caseid i have given a discount . to retrive the discount for this caseid
i have written the following qry.
Select A.caseid,A.doctorcode,A.Visitcharges,
  dbo.fnBillDiscount(A.caseid,2)
 From
  (Select hbdf.caseid,hbdf.DoctorCode,sum(hBdf.Charges)as visitcharges
   From HISBillDoctorFees hBdf
   inner join hisbill hb on hb.caseid = hbdf.caseid
   where billdate between '20080501' and '20080531'
   and billcancelled = 0  and billno <> 0 and branch = 2
   and hbdf.caseid = 67161
   Group by hbdf.caseid,doctorcode
  )A
  order by A.caseid
where  dbo.fnBillDiscount(A.caseid,2) is function retruns a discount for a particualr caseid and branch.
when i run this qry, it returns the foll output

caseid      doctorcode      Visitcharges      discount
67161      1      0      50
67161      70      500      50
67161      164      0      50

Here though the actual disocunt is give for a caseid is rs 50 , it is repeating 3 times because
caseid is repeating 3 times.
My Qry is  in the below mentioned qry
Select A.caseid,A.doctorcode,A.Visitcharges,
  dbo.fnBillDiscount(A.caseid,2)
 From
  (Select hbdf.caseid,hbdf.DoctorCode,sum(hBdf.Charges)as visitcharges
   From HISBillDoctorFees hBdf
   inner join hisbill hb on hb.caseid = hbdf.caseid
   where billdate between '20080501' and '20080531'
   and billcancelled = 0  and billno <> 0 and branch = 2
   and hbdf.caseid = 67161
   Group by hbdf.caseid,doctorcode
  )A
  order by A.caseid

is there any way i can retrieve the discount only once and not  3 times

Thanks in advance and i very much appreciate experts advice at the earliest
regards
Venkat



0
Comment
Question by:venkataramanaiahsr
  • 2
2 Comments
 
LVL 2

Expert Comment

by:AntonyDN
ID: 21814191
With the query as it is, I guess, "NO!"

It would help to see the SQL for the function, but my guess is that that is applied to the CaseID, but you are returning rows at the DoctorCode level - a different granularity.

If you removed the DoctorCode, you'd get one row, and - I guess - only one "50" .

This code should (I hope - I didn't set anything up to test it) show you what I mean.


Select A.caseid
--,A.doctorcode
,A.Visitcharges
  dbo.fnBillDiscount(A.caseid,2)
 From
  (Select hbdf.caseid
--,hbdf.DoctorCode
,sum(hBdf.Charges)as visitcharges 
   From HISBillDoctorFees hBdf 
   inner join hisbill hb on hb.caseid = hbdf.caseid 
 
   where billdate between '20080501' and '20080531'
   and billcancelled = 0  and billno <> 0 and branch = 2
   and hbdf.caseid = 67161
   Group by hbdf.caseid --,doctorcode
  )A
  order by A.caseid 

Open in new window

0
 
LVL 2

Accepted Solution

by:
AntonyDN earned 500 total points
ID: 21814434
This code will actually work! Apologies for the previous version ...
   Select hbdf.caseid
--,hbdf.DoctorCode
,  sum(hBdf.Charges)as visitcharges 
,  dbo.fnBillDiscount(hbdf.caseid,2)
   From HISBillDoctorFees hBdf 
   inner join hisbill hb on hb.caseid = hbdf.caseid 
   where billdate between '20080501' and '20080531'
   and billcancelled = 0  and billno <> 0 and branch = 2
   and hbdf.caseid = 67161
   Group by hbdf.caseid --,doctorcode

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now