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 |
|
EarlEBurba
Starting Member
3 Posts |
Posted - 2005-07-14 : 15:57:08
|
| I'm trying to generate reports to display the latest dates for id's. The data comes from two different tables. Given the following data id year month Count Volume----- ----- ----- ------ ----------5559 2005 3 60110 85413035095559 2005 2 58367 81968391655559 2005 1 57333 80096254065559 2004 12 56376 78253358315558 2005 3 5756 10327216075558 2005 2 5405 9616659225558 2005 1 5186 9210963015558 2004 12 4956 8752961375554 2005 3 48 51148615554 2005 2 45 48688615554 2005 1 43 47538615554 2004 12 45 48668615554 2004 11 43 46168615553 2005 3 20 8547575553 2005 2 18 779980Does anyone know how to create a report of id's having the latest month/year combination.The resultant report would look like: id year month Count Volume----- ----- ----- ------ ----------5559 2005 3 60110 85413035095559 2004 12 56376 78253358315558 2005 3 5756 10327216075558 2004 12 4956 8752961375554 2005 3 48 51148615554 2004 12 45 48668615553 2005 3 20 854757The closest I've come isSELECT distinct TreatyID, Year, Month, Count, VolumeFROM dbo.tbl1 INNER JOIN dbo.tbl2 ON dbo.tbl_db.1.ID = dbo.AccountSummary.TreatyID AND dbo.tbl1.Year = dbo.tbl2.Year AND dbo.tbl1.Month = dbo.tbl2.Monthwhere dbo.tbl1.ID = dbo.tbl2.IDGROUP BY dbo.tbl1.ID, dbo.tbl1.Year, dbo.tbl1.Month, dbo.tbl1.Count, dbo.tbl1.VolumeORDER BY dbo.tbl1.ID DESC, dbo.tbl1.Year DESC, dbo.tbl1.Month DESC;Thanks for any help, I've been working on this problem for the last 3 days.Earl |
|
|
jhermiz
3564 Posts |
Posted - 2005-07-14 : 16:06:39
|
Why are you using distinct?Do you understand the differences between distinct and group by?You will need to use aggregate functions such as MAX and then use a GROUP BYclause to get what you want.A lot of people mix distinct with group by hoping to get the right results, only tofind that their results work sometimes (it's very unexpected results).For your query you will need toGroup By ID, Year and Month. You will not need to group by count. Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2005-07-14 : 16:07:26
|
| [code]select d.id,d.year,d.month,t.count,t.volumefrom( select id,year,max(month) month from t group by id,year) djoin t t on t.id = d.id and t.year = d.year and t.month = d.month[/code] |
 |
|
|
EarlEBurba
Starting Member
3 Posts |
Posted - 2005-07-15 : 08:27:41
|
| Thanks I'll try your suggestions. I appreciate your help since I'm trying to learn as I go. |
 |
|
|
EarlEBurba
Starting Member
3 Posts |
Posted - 2005-07-17 : 13:17:56
|
| I'm still struggling, and tried your suggestions, but still can't quite get it.--- This works for getting the correct TreatyID and max_month for each period yearSELECT TOP 100 PERCENT dbo.AccountSummary.TreatyID, dbo.AccountSummary.PeriodYear, MAX(dbo.AccountSummary.PeriodMonth) AS Max_MonthFROM dbo.AccountSummary INNER JOIN dbo.tbl_vwAMRPolicyExhibit ON dbo.AccountSummary.PeriodMonth = dbo.tbl_vwAMRPolicyExhibit.PeriodMonth AND dbo.AccountSummary.PeriodYear = dbo.tbl_vwAMRPolicyExhibit.PeriodYear AND dbo.AccountSummary.TreatyID = dbo.tbl_vwAMRPolicyExhibit.TreatyIDGROUP BY dbo.AccountSummary.TreatyID, dbo.AccountSummary.PeriodYearHAVING dbo.AccountSummary.PeriodYear=2005ORDER BY dbo.AccountSummary.TreatyID DESC, dbo.AccountSummary.PeriodYear DESC, Max_Month DESC-- Actual ResultsTreatyID PeriodYear PeriodMonth-------- ---------- -----------5559 2005 35558 2005 35554 2005 35553 2005 35552 2005 35551 2005 25550 2005 25548 2005 35547 2005 25542 2005 35541 2005 25538 2005 25537 2005 35532 2005 25531 2005 15522 2005 35521 2005 35518 2005 15517 2005 35516 2005 25514 2005 25513 2005 3-- Actual results after adding in additional table entriesSELECT TOP 100 PERCENT dbo.AccountSummary.TreatyID, dbo.AccountSummary.PeriodYear, MAX(dbo.AccountSummary.PeriodMonth) AS Max_Month, dbo.tbl_vwAMRPolicyExhibit.EndingCount, dbo.tbl_vwAMRPolicyExhibit.EndingVolumeFROM dbo.AccountSummary INNER JOIN dbo.tbl_vwAMRPolicyExhibit ON dbo.AccountSummary.PeriodMonth = dbo.tbl_vwAMRPolicyExhibit.PeriodMonth AND dbo.AccountSummary.PeriodYear = dbo.tbl_vwAMRPolicyExhibit.PeriodYear AND dbo.AccountSummary.TreatyID = dbo.tbl_vwAMRPolicyExhibit.TreatyIDGROUP BY dbo.AccountSummary.TreatyID, dbo.AccountSummary.PeriodYear, dbo.tbl_vwAMRPolicyExhibit.EndingCount, dbo.tbl_vwAMRPolicyExhibit.EndingVolumeHAVING (dbo.AccountSummary.PeriodYear = 2005)ORDER BY dbo.AccountSummary.TreatyID DESC, dbo.AccountSummary.PeriodYear DESC, Max_Month DESCTreatyID PeriodYear PeriodMonth EndintCount EndingVolume-------- ---------- ----------- ----------- ------------5559 2005 3 60110 85413035095559 2005 2 58367 81968391655559 2005 1 57333 80096254065558 2005 3 5756 10327216075558 2005 2 5405 9616659225558 2005 1 5186 9210963015554 2005 3 48 51148615554 2005 2 45 48688615554 2005 1 43 47538615553 2005 3 20 8547575553 2005 2 18 7799805553 2005 1 10 5581495552 2005 3 35 33258135552 2005 2 28 27322445552 2005 1 23 14185675551 2005 2 367 414154175551 2005 1 352 406118765550 2005 2 2646 3676761695550 2005 1 2400 3348037055548 2005 3 7 5953935547 2005 2 2 2028005542 2005 3 63 64912505542 2005 2 8 4837505542 2005 1 1 225005541 2005 2 93 32218805538 2005 2 39 6768205538 2005 1 29 5415725537 2005 3 2076 1823940665537 2005 2 306 255399755537 2005 1 7 3606755532 2005 2 26721 4096473055532 2005 1 21995 3346585155531 2005 1 4185 3366869305522 2005 3 83 49348755522 2005 2 74 45297485522 2005 1 59 32678835521 2005 3 130 152166135521 2005 2 119 126630035521 2005 1 115 122813885518 2005 1 0 05517 2005 3 75 79239425517 2005 2 64 71224735517 2005 1 34 34417235516 2005 2 1352 498928655516 2005 1 78 50696705514 2005 2 104 124333315514 2005 1 89 106257255513 2005 3 294 314872255513 2005 2 266 278894755513 2005 1 233 24897600-- Desired/Expected ResultsTreatyID PeriodYear PeriodMonth EndintCount EndingVolume-------- ---------- ----------- ----------- ------------5559 2005 3 60110 85413035095558 2005 3 5756 10327216075554 2005 3 48 51148615553 2005 3 20 8547575552 2005 3 35 33258135551 2005 2 367 414154175550 2005 2 2646 3676761695548 2005 3 7 5953935547 2005 2 2 2028005542 2005 3 63 64912505541 2005 2 93 32218805538 2005 2 39 6768205537 2005 3 2076 1823940665532 2005 2 26721 4096473055531 2005 1 4185 3366869305522 2005 3 83 49348755521 2005 3 130 152166135518 2005 1 0 05517 2005 3 75 79239425516 2005 2 1352 498928655514 2005 2 104 124333315513 2005 3 294 31487225Any help is greatly appreciated |
 |
|
|
|
|
|
|
|