Wednesday 15 May 2013

Sql Query Average of a Sum -


I have the following questions which are counting from 2 subquerys and I should give an average of both the count. This is giving an error on C1 in the last query.

  select c0.hour, in the form of average (c0.frequency) in the 'average', average (c1.frequency) as 'avg out' frequency (selection [network ] As 'date', date format (hh, [date time]) 'hour', [scan type], count ([scan type]) as cast (as date [date time]) [ Plex-DW]. [DBO]. [Scan] where network 'network' by Network '3' group, like Detector (HH, [Time of Date]), and 'Scan Type' Cast (date [date time]), [(scan type)) as CC union (select [network], cast (as of date [date time] 'date', date format (hh, [date time] ) As the 'hour', [scan type], count [scan type] frequency [Palex-DW]. [DBO]. [Scan] as C1, where the network 'Fre' and 'scan type ] Like network '11' groups, Datapter (HH, [Datetime]), Dali (as date [date time] ]; [Scan type]] C1    

The problem in your query is actually < Code> c0 Union does not require aliases on the first query:

Try this version:

  select c0.hour, avg (c0 .frequency) as 'AVG In', in the form of 'Date' (Active [Network], cast (date as date), 'AGG' as 'AVG Out' (C1. Frequency) as the 'hour', [scan type] [scale type]] in the form of 'date', deterterport (hh, [date time]] [plex-dw]. [DBO]. [Scan] where the network '3' group like network 'Frey' and [scan type], Datapter (hh, [date time (as date), dali (as date [date time]), [Scan type]) 'scan type' as the union (selection [network], cast (date of [date of time] 'date', date format (hh, [date time]), [[scan type ]) As the frequency [PLEX-DW] [DBO]. [Scan] in the form of C1 where the network 'Fra According to '11' group network like '[scan type],' datepart (hh, [date time]), dali (date of [date of time], [scan type]) as c   

However, I think that you can query by removing union and selecting all the rows in a row:

  c. Hour, avg (eg [scan type] = '3' then 1.0 * c (select [network], cast (as the date time), "avg in" as "avg in", avg (when [Scan type] = '3', then 1.0 * seaf [Plex-DW] [DBA] as frequency in the form of "scan type", count ([scan type]). [DBO]. "Scan" in the group ('3', '11') according to the "hour" date of the date [date] date of [date] date as "date", date format (hh [[date time]), "hour" And by group group such as 'scan type', dumpetter (hh, [time of date]), cast (date of [date of time]), [scan type] by   

You also need a group section for the final result. I multiply the values ​​of 1.0 to be converted into non-integer values. The integer is the whole integer, which can be misleading. I have also removed single quotes around column names; Use double quotes or square bracket (to avoid confusion with actual constants).

No comments:

Post a Comment