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 |
|
kumar.raja
Starting Member
2 Posts |
Posted - 2010-10-23 : 23:12:46
|
| The issue i am facing is that i have this table with two columns, date and another column with certain codes as shown below.date Code25\06\2009 A23\07\2010 B20\11\2009 D18\08\2008 C14\10\2010 A14\09\2010 E14\03\2010 D14\03\2009 D14\01\2009 D13\08\2010 E13\01\2009 E12\08\2010 F10\06\2009 A09\09\2009 B07\12\2009 C06\10\2009 A05\10\2009 F05\07\2008 D02\02\2009 E01\08\2009 F01\07\2009 G01\01\2008 DNow what i actually want from this table is that for each date in column 1, i would want to know the code from column b that has appeared most within one year of the date that i have considered.For example, if i take the first date 25\06\2009 the codes that have appeared within a year of that would be D (thrice), E(twice) and F (once). So D wins as the code that appears most. I would like to know the code that would give the winnig code for each date in column 1.for some reason, my query seems to be returning nothing.select t1.date,t1.code,count (1) as count_code from table t1inner join table t2 (nolock) on t1.date=t2.date where t2.date between dateadd (year,-1,t1.date) and t1.dategroup by t1.datei see the error in my code as i am writing this, but i couldn't find out a solution either. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
kumar.raja
Starting Member
2 Posts |
Posted - 2010-10-24 : 02:03:43
|
| the datetime data is in the datetime format.no there are no backslashes. i wrote down the data. |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2010-10-24 : 13:48:11
|
there is an error in your code:try like this:select t1.date,t1.code,count (1) as count_code from table t1inner join table t2 with (nolock) on t1.date=t2.datewhere t2.date between dateadd (year,-1,t1.date) and t1.dategroup by t1.date,t1.code |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-10-25 : 17:16:37
|
| Please don't use reserved words for data element names. You did not say how to handle ties, so let's get all the counts by codes: SELECT E1.event_date, SUM(CASE WHEN E1.foobar_code = 'A' THEN 1 ELSE 0 END)AS a_code_cnt, SUM(CASE WHEN E1.foobar_code = 'B' THEN 1 ELSE 0 END)AS b_code_cnt, SUM(CASE WHEN E1.foobar_code = 'C' THEN 1 ELSE 0 END)AS c_code_cnt, SUM(CASE WHEN E1.foobar_code = 'D' THEN 1 ELSE 0 END)AS d_code_cnt, SUM(CASE WHEN E1.foobar_code = 'E' THEN 1 ELSE 0 END)AS e_code_cnt, SUM(CASE WHEN E1.foobar_code = 'F' THEN 1 ELSE 0 END)AS f_code_cnt FROM Events AS E1, Events AS E2WHERE E2.event_date BETWEEN DATEADD (YEAR, -1, E1.event_date) AND E1.event_dateGROUP BY E1.event_date;Also, Standard SQL uses only the "YYYY-MM-DD HH:MM:SS.sss.." format. --CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
rahuldhoble
Starting Member
3 Posts |
Posted - 2010-10-26 : 01:26:14
|
| In the year of 25\06\2009 the codes D appeared twice only,if this is right then query suggested by jcelko is right .Regards,Rahul DhobleSr.Software EngineerMumbai |
 |
|
|
|
|
|
|
|