SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help on Group By
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/04/2013 :  22:39:49  Show Profile  Reply with Quote


How can I amend the below query
to get divisor with the same week number (wkno) as dividend based on datetime column of divisor .


SELECT CONVERT(decimal(10, 2), SUM(mAmount) /
(SELECT COUNT(*) AS Expr1
FROM Filterednew_appointment
WHERE (new_appointmentstatus = 'A') )

) AS TotalBillSize, WkNo,'O' AS Type
FROM (SELECT DISTINCT
new_patienthrn AS HRN, new_paidamount AS mAmount, new_name AS BillNo, CASE WHEN (DAY(new_startdate) >= 1 AND DAY(new_startdate) <= 7)
THEN 'Wk1' WHEN (DAY(new_startdate) >= 8 AND DAY(new_startdate) <= 14) THEN 'Wk2' WHEN (DAY(new_startdate) >= 15 AND DAY(new_startdate) <= 21)
THEN 'Wk3' WHEN (DAY(new_startdate) >= 22 AND DAY(new_startdate) <= 28) THEN 'Wk4' WHEN (DAY(new_startdate) > 28)
THEN 'Wk5' END AS WkNo
FROM Filterednew_outpatientbillinginfo
WHERE (new_patienthrn IS NOT NULL) AND (MONTH(new_startdate) = MONTH(@SDate)) AND (YEAR(new_startdate) = YEAR(@SDate))) AS B
GROUP BY WkNo

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/04/2013 :  22:48:24  Show Profile  Reply with Quote
i didnt get your question clearly. I cant see any datetime column in divisor here. Anyways, if your question was to get weekno from datetime field, use DATEPART() function. Also it gives weekno of the year and not weekno of the month.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/04/2013 :  23:06:25  Show Profile  Reply with Quote
Dividend is
CONVERT(decimal(10, 2), SUM(mAmount)
Divisor is
(SELECT COUNT(*) AS Expr1
FROM Filterednew_appointment
WHERE (new_appointmentstatus = 'A') )

Divisor has a date time column "new_appointmentdate" but not in query currently.
Whole query returns sum group by week no and divided by count returned from divisor (suppose divisor returns 30)
Eg.
100/30 wk1
200/30 wk2
But I'd like to change as
if divisor has 10 records for wk1 ,divide 100 by 10
if divisor has 20 records for wk2, divide 200 by 20
Calculation of week number will be the same as main query but take "appointmentdate" from divisor table

Thanks.

Edited by - kwikwisi on 03/04/2013 23:08:25
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/04/2013 :  23:44:38  Show Profile  Reply with Quote
then change divisor to

SELECT COUNT(DISTINCT DATEPART(wk,new_appointmentdate)) AS Expr1
FROM Filterednew_appointment
WHERE (new_appointmentstatus = 'A')

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

280 Posts

Posted - 03/05/2013 :  00:10:59  Show Profile  Reply with Quote
Thank you.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 03/05/2013 :  00:15:13  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000