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 |
PGG_CA
Starting Member
24 Posts |
Posted - 2013-10-11 : 18:28:49
|
table1student course credit enddate1 1258 1 2012-10-191 1211 1 2011-01-251 2512 5 2013-05-231 1212 5 2012-10-091 2312 1 2013-05-072 1223 1 2011-10-012 2061 5 2013-04-262 2062 5 2013-05-292 1906 5 2013-03-052 1905 1 2013-01-24table2student enrollment_date withdrawl_date unit 1 2011-05-24 2012-08-30 YAC1 2012-08-31 2012-09-12 CST1 2012-09-13 2012-10-01 CST1 2012-10-02 2013-06-06 CST1 2013-06-07 2013-06-11 CST1 2013-06-12 2013-06-12 YAC1 2013-06-13 2013-06-30 CST1 2013-07-01 2013-10-10 YAC2 2011-05-29 2012-05-29 RDL2 2012-05-30 2013-10-01 RDL2 2013-10-02 2013-10-10 CSTThese 2 table scontain my sample data. What I want to do is show the unit and the total credits per unit between a specific period, say Sep 1, 2012 and Aug 27, 2013. I need to check the withdrawl_date from table2 to determine which unit (YAC, CST or RDL) would be credited with the course credits. If the course's enddate occurred when the student is in a particular unit (within an enrollment and a withdawl date) , then that unit gets the credit.The output should look like this:YAC 0CST 12 RDL 16Thanks for any help. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-12 : 04:59:06
|
[code]SELECT m.unit,COALESCE(n.totCredit,0) AS CreditsFROM (SELECT DISTINCT unit FROM table2) mLEFT JOIN (SELECT t2.unit,SUM(t1.credit) AS TotCreditFROM table1 t1INNER JOIN table2 t2ON t1.enddate > t2.enrollment_date AND t1.enddate < t2.withdrawl_dateAND t1.student = t2.studentGROUP BY t2.unit)nON n.unit = m.unit[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
PGG_CA
Starting Member
24 Posts |
Posted - 2013-10-15 : 11:46:47
|
Thanks visakh16.How do I filter the data so that only data between Sep 1, 2012 and Aug 27, 2013 are included |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-16 : 02:17:51
|
[code]SELECT m.unit,COALESCE(n.totCredit,0) AS CreditsFROM (SELECT DISTINCT unit FROM table2) mLEFT JOIN (SELECT t2.unit,SUM(t1.credit) AS TotCreditFROM table1 t1INNER JOIN table2 t2ON t1.enddate > t2.enrollment_date AND t1.enddate < t2.withdrawl_dateAND t1.student = t2.student WHERE t1.enddate > = '20120901' AND t1.enddate < '20130828'GROUP BY t2.unit)nON n.unit = m.unit[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|
|
|
|