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 |
|
julien_roy50
Starting Member
4 Posts |
Posted - 2012-02-22 : 09:47:59
|
Hello guys. I have a table containing many departments with a start date and an end date. Here is my queryDeclare @nomTech as nvarchar(50)Declare @anneeDeConsultation as nvarchar(50)Set @nomTech = 'Moriphil'Set @anneeDeConsultation = '2011'SELECT top 25 nom, datediff(dd,date_creat,date_real) as differenceFROM RDS_REQ_VUE join ach_code_util on ach_code_util.CODE_UTIL=RDS_REQ_VUE.code_util_reQwhere code_util_resp = @nomTechAND ETAT != '99'AND ETAT != '02'and (left(CONVERT(VARCHAR(10),DATE_CREAT, 102),4) <= @anneeDeConsultationand left(CONVERT(VARCHAR(10),DATE_REAL, 102),4) > @anneeDeConsultation OR (DATE_REAL IS NULL AND left(CONVERT(VARCHAR(10),DATE_CREAT, 102),4) <= @anneeDeConsultation)AND ETAT != '50') and there is what it returnsnom----------------------------differenceGRDS - Ens-MEC- École 070------21GRDS - Ens-SANTÉ- École 070----152GRDS - Ens-ACI- École 070------49GRDS - Ens-COIF- École 070-----NULLGRDS - Ens-ESA- École 070------219GRDS - Ens-Usinage- École 070--NULLGRDS - Ens-Usinage- École 070--236GRDS - Ens-ACI- École 070------27GRDS - Ens-ACI- École 070------139GRDS - Ens-FG- École 070-------140GRDS - Ens-ESA- École 070------NULLGRDS - Direction école 070-----126GRDS - Ens-ESA- École 070------39GRDS - Ens-ACI- École 070------69GRDS - Ens-ACI- École 070------85GRDS - Ens-ESA- École 070------NULLGRDS - Ens-ESA- École 070------272GRDS - Ens-MEC- École 070------52GRDS - Ens-ESA- École 070------142GRDS - Ens-FG- École 070-------18GRDS - Ens-ESA- École 070------219GRDS - Direction école 070-----64GRDS - Ens-FG- École 070-------103GRDS - Ens-ACI- École 070------84GRDS - Ens-ESA- École 070------219What I need to get as a result table is the mode of the colum difference for each department.(The mode is the value that is the most repeated in the series). I have tried many things and look over the net without sucess. Thanks in advance. Hope every thing is clear. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-22 : 09:53:45
|
this?SELECT nom,differenceFROM(SELECT nom,difference,COUNT(*) AS cnt,ROW_NUMBER() OVER (PARTITION BY nom,difference ORDER BY COUNT(*) DESC) AS RnFROM tableGROUP nom)tWHERE rn=1 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
julien_roy50
Starting Member
4 Posts |
Posted - 2012-02-22 : 10:16:53
|
Thanks for the reply.First I want to excuse myself since diffenrence is a reserve name. So I'll rather use diffenrences as colum nameBase on your code I have tested the following.1.SELECT nom,differences2.FROM3.(4.SELECT nom,datediff(dd,date_creat,date_real) as differences,COUNT(*) AS cnt,ROW_NUMBER() OVER (PARTITION BY nom,diffenrences order by count(*) DESC) AS Rn5.FROM RDS_REQ_VUE 6.join ach_code_util on ach_code_util.CODE_UTIL=RDS_REQ_VUE.code_util_reQ7.GROUP nom8.)t9.WHERE rn=1 I got the following messages error quote: Msg 102, Niveau 15, État 1, Ligne 7Incorrect syntax near 'nom'.
Maybe your idea is right but i has no luck testing it and i must admit i am not use to this code ROW_NUMBER() OVER (PARTITION BY nom,diffenrences order by count(*) DESC) AS Rn |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-02-22 : 10:29:42
|
[code]GROUP BY nom[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
julien_roy50
Starting Member
4 Posts |
Posted - 2012-02-22 : 10:41:21
|
Yes thanks that one was an obvious mistake.there is the code now1.SELECT nom,differences2.FROM3.(4.SELECT nom,datediff(dd,date_creat,date_real) as differences,COUNT(*) AS cnt,ROW_NUMBER() OVER (PARTITION BY nom,diffenrences order by count(*) DESC) AS Rn5.FROM RDS_REQ_VUE 6.join ach_code_util on ach_code_util.CODE_UTIL=RDS_REQ_VUE.code_util_reQ7.GROUP by nom8.)t9.WHERE rn=1 and there is the error i get quote: Msg 8120, Niveau 16, État 1, Ligne 1Column 'RDS_REQ_VUE.DATE_CREAT' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 8120, Niveau 16, État 1, Ligne 1Column 'RDS_REQ_VUE.DATE_REAL' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Msg 207, Niveau 16, État 1, Ligne 4Invalid column name 'diffenrences'.
|
 |
|
|
julien_roy50
Starting Member
4 Posts |
Posted - 2012-02-22 : 11:43:29
|
| Problem is line 4 column datediff(dd,date_creat,date_real) as differences is not an aggregate function, so any other idea? |
 |
|
|
|
|
|
|
|