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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Return a PercentCompleted field

Author  Topic 

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-08-18 : 13:11:44
I'm returning a "subscriptions" field and a "subscriptionscompleted" field but I'm not sure how to get a "percentcompleted" field returned in the following query...


SELECT 'View...' AS [view],
studentsid,
lastname,
firstname,
location,
employeenum,
username,
password,
email,
address,
city,
province,
country,
postalcode,
phonenumber,
dateadded,
(SELECT TOP 1 departments.departmentname
FROM departments
WHERE departments.departmentid = students.departmentid) AS department,
(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = students.studentsid) AS subscriptions,

(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = students.studentsid and subscription.completed is not null) AS subscriptionscompleted,

(SELECT Sum(educationalunits)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid
AND subscription.status = 2) AS units,
(SELECT '$' + Convert(VARCHAR(12),Sum(Convert(MONEY,seatprice)),
1)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid) AS seatprice,
(SELECT '$' + Convert(VARCHAR(12),Sum(Convert(MONEY,companycost)),
1)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid) AS companycost,
(SELECT Sum(companytime)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid) AS companytime,
(SELECT Sum(employeetime)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid) AS employeetime,
(SELECT '$' + Convert(VARCHAR(12),Sum(Convert(MONEY,employeecost)),
1)
FROM courses
INNER JOIN subscription
ON courses.courseid = subscription.courseid
WHERE subscription.studentsid = students.studentsid) AS employeecost,
students.extra1,
students.extra2,
students.extra3,
students.extra4,
students.extra5,
students.extra6,
students.extra7,
students.extra8,
students.extra9,
students.extra10,
students.extra11,
students.extra12,
students.extra13,
students.extra14,
students.extra15,
students.extra16,
students.extra17,
students.extra18,
students.extra19,
students.extra20,
students.extra21,
students.extra22,
students.extra23,
students.extra24,
students.extra25,
students.extra26,
students.extra27,
students.extra28,
students.extra29,
students.extra30,
(students.lastloggedin) AS lastloggedin
FROM students
WHERE clientid = 01
AND students.activestatus IN (1)
ORDER BY lastname ASC;

Skorch
Constraint Violating Yak Guru

300 Posts

Posted - 2009-08-18 : 13:32:39
Is percentcompleted a column in the students table? Or is it a calculated field that you need to add?

Some days you're the dog, and some days you're the fire hydrant.
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-08-18 : 14:09:15
I need it to be calculated from the "subscriptions" and "subscriptionscompleted" fields that are returned in the above query.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-18 : 14:33:22
You can do your calculation by repeating the wanted subselects in your formula.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-08-18 : 14:48:46
quote:
Originally posted by webfred

You can do your calculation by repeating the wanted subselects in your formula.


No, you're never too old to Yak'n'Roll if you're too young to die.



That's what I've been trying but I can't seem to get the syntax right.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-18 : 14:51:26
Then please show what you have tried so far and what the error message is.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-08-18 : 17:11:41
I'm trying it independent of the above query (for simplicities sake)...


select test2/test1*100 from (
select
(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = 1172) as test1,
(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = 1172 and subscription.completed is not null) as test2
)


The error I am getting is "Incorrect syntax near ')'"

Also tried this...


select test2/test1*100 from
(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = 1172) as test1,
(SELECT Count(subscription.courseid)
FROM subscription
WHERE subscription.studentsid = 1172 and subscription.completed is not null) as test2


Which gives "No column was specified for column1 of 'test1'"
Go to Top of Page

ferrethouse
Constraint Violating Yak Guru

352 Posts

Posted - 2009-08-18 : 17:23:45
Got it!


select test2.bla2/test1.bla1*100 from
(SELECT Count(subscription.courseid) as bla1
FROM subscription
WHERE subscription.studentsid = 1172) as test1,
(SELECT Count(subscription.courseid) as bla2
FROM subscription
WHERE subscription.studentsid = 1172 and subscription.completed is not null) as test2


Thanks for guiding me to the solution :)
Go to Top of Page
   

- Advertisement -