Solved

Sql Qry problem

Posted on 2008-06-18
2
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

622 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