| Author |
Topic |
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-11 : 20:54:47
|
| create view times asselect substring(Convert(char(19),a.transdatetime,120),12,2) asT1,avg(ivalue*quantity) as averagefrom transactions a,transactionItems bwhere a.transid=b.transidgroup by substring(Convert(char(19),a.transdatetime,120),12,2)select avg(average) morning from Timeswhere T1>=8 and t1<11select avg(average) midday from Timeswhere T1>=11 and t1<17select avg(average) afternoon from Timeswhere T1>=17 and t1<20this give me the average of every time_group (8-11,11-17,17-20) in three different tables with one value each. i want to find a way to make them all in one table if it is possible... the two select statements following find the average per store and the average per date. i want modify the above and find the average per day (per monday, per tuesday......per sunday) and include all the three statements in oneselect transStore,avg(ivalue*quantity) as avgPerStorefrom transactions as A,transactionItems as Bwhere A.transID=B.transIDgroup by transStoreselect distinct convert(varchar,transdatetime,101) as thedate,avg(ivalue*quantity) as avgPerdatefrom transactions a,transactionItems bwhere a.transID=b.transIDgroup by convert(varchar,transdatetime,101)order by avgPerDate desc |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 22:57:24
|
| [code]select avg(case when T1>=8 and t1<11 then average else null end) morning ,avg(case when T1>=11 and t1<27 then average else null end) midday ,avg(case when T1>=17 and t1<20 then average else null end) afternoon from Times[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 23:09:49
|
| [code]select dateadd(dd,datediff(dd,0,transdatetime),0) as thedate,avg(ivalue*quantity) as avgPerdate,avg(case when datepart(hh,transdatetime) >=8 and datepart(hh,transdatetime)<11 then ivalue*quantity else null) as avgmorning,avg(case when datepart(hh,transdatetime) >=11 and datepart(hh,transdatetime)<17 then ivalue*quantity else null) as avgmidday,avg(case when datepart(hh,transdatetime) >=17 and datepart(hh,transdatetime)<20 then ivalue*quantity else null) as avgafternoonfrom transactions ainner join transactionItems bon a.transID=b.transIDgroup by dateadd(dd,datediff(dd,0,transdatetime),0)order by avgPerDate desc[/code] |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-12 : 11:09:34
|
| Msg 102, Level 15, State 1, Line 3Incorrect syntax near ')'.i received this syntax error but i think that the () are all correct. is there anything wrong?? |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-12 : 23:04:24
|
| After case u have use end keyword select dateadd(dd,datediff(dd,0,transdatetime),0) as thedate,avg(ivalue*quantity) as avgPerdate,avg(case when datepart(hh,transdatetime) >=8 and datepart(hh,transdatetime)<11 then ivalue*quantity else null END) as avgmorning,avg(case when datepart(hh,transdatetime) >=11 and datepart(hh,transdatetime)<17 then ivalue*quantity else null END ) as avgmidday,avg(case when datepart(hh,transdatetime) >=17 and datepart(hh,transdatetime)<20 then ivalue*quantity else null END ) as avgafternoonfrom transactions ainner join transactionItems bon a.transID=b.transIDgroup by dateadd(dd,datediff(dd,0,transdatetime),0)order by avgPerDate desc |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-12 : 23:25:27
|
quote: Originally posted by constantinos1987 Msg 102, Level 15, State 1, Line 3Incorrect syntax near ')'.i received this syntax error but i think that the () are all correct. is there anything wrong??
i missed end after case. so use the query bklr provided. |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-13 : 12:43:41
|
| is it possible to have the day instead of the date??i need to find the average per monday, per tuesday...the result i need has to be like this....store 100, monday, 8-11, 24 eurostore 100, monday, 11-17, 100 eurostore 100, monday, 17-20, 150 eurostore 100, tuesday, 8-11, 24 eurostore 100, tuesday, 11-17, 100 eurostore 100, tuesday, 17-20, 150 euro.....store 100, friday, 8-11, 24 eurostore 100, friday, 11-17, 100 eurostore 100, friday, 17-20, 150 euro ........store 200, monday, 8-11, 244 eurostore 200, monday, 11-17, 160 eurostore 200, monday, 17-20, 180 euro........... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-13 : 13:34:38
|
| [code]select transtore,DATENAME(dw,dateadd(dd,datediff(dd,0,transdatetime),0)) as thedate,avg(ivalue*quantity) as avgPerdate,avg(case when datepart(hh,transdatetime) >=8 and datepart(hh,transdatetime)<11 then ivalue*quantity else null end) as avgmorning,avg(case when datepart(hh,transdatetime) >=11 and datepart(hh,transdatetime)<17 then ivalue*quantity else null end) as avgmidday,avg(case when datepart(hh,transdatetime) >=17 and datepart(hh,transdatetime)<20 then ivalue*quantity else null end) as avgafternoonfrom transactions ainner join transactionItems bon a.transID=b.transIDgroup by transtore,dateadd(dd,datediff(dd,0,transdatetime),0)order by avgPerDate desc[/code] |
 |
|
|
constantinos1987
Starting Member
25 Posts |
Posted - 2008-12-13 : 21:30:51
|
| this is the solution i think.... thank you all guys!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-13 : 22:11:41
|
welcome |
 |
|
|
|
|
|