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 |
|
sqlbug
Posting Yak Master
201 Posts |
Posted - 2011-02-24 : 15:45:38
|
I need to get average value for each day for each station/parameter/method/units.I have these tables:SAMPLE_DATA: CREATE TABLE [dbo].[SAMPLE_DATA]([SAMPLE_NUM] [numeric](12, 0) IDENTITY(1,1) NOT NULL,(Primary Key)[SAMPLE_TYPE] [char](1) NOT NULL,[SAMPLE_STATION] [int] NOT NULL,[SAMPLE_INTERVAL] [char](1) NOT NULL, [SAMPLE_START_DATE] [datetime] NOT NULL,[SAMPLE_START_TIME] [int] NOT NULL,[SAMPLE_END_DATE] [datetime] NOT NULL,[SAMPLE_END_TIME] [int] NOT NULL, CONSTRAINT [PK_SAMPLE_DATA] PRIMARY KEY CLUSTERED ([SAMPLE_NUM] ASC))AndCREATE TABLE [dbo].[STATION_DATA]([DATA_ID] [int] IDENTITY(1,1) NOT NULL, (Primary Key)[DATA_SAMPLE_NUM] [numeric](12, 0) NOT NULL, (Foreign Key)[DATA_PARAMETER] [char](8) NOT NULL,[DATA_METHOD] [char](3) NOT NULL,[DATA_UNIT] [char](2) NOT NULL,[DATA_DECIMAL] [numeric](1, 0) NULL,[DATA_VALUE] [numeric](8, 4) NULL,[DATA_FLAG] [char](1) NULL,[DATA_AUDIT_CODE] [char](1) NULL,)In the Sample Data table - I have record for each hour of the day for station/type/intervals.In the Station Data table - I have the parameter/method/unit and data values for each. these tables are linked by the Sample Number, so basically - in the 2nd table - I have different parameter/method/unit/values for each station/type/interval in the first table.How can I get the average for each day (Sample Start and Sample End dates would be the same)?For example - I tried the following:SELECT SAMPLE_STATION,SAMPLE_START_DATE,DATA_PARAMETER,AVG(DATA_VALUE),DATA_FLAG,GUIDE_LEVELFROM SAMPLE_DATA INNER JOIN STATION_DATA ON SAMPLE_NUM = DATA_SAMPLE_NUMINNER JOIN GUIDELINE_INFO ON GUIDE_PARAM = DATA_PARAMETERWHERE SAMPLE_STATION = 1168 AND SAMPLE_START_DATE >= '1-Jan-2010' AND SAMPLE_END_DATE <= '31-Dec-2010' AND DATA_PARAMETER = '81104'AND GUIDE_TIME = '7' AND SAMPLE_START_DATE = SAMPLE_END_DATEGROUP BY SAMPLE_STATION,SAMPLE_START_DATE,DATA_PARAMETER,DATA_VALUE,DATA_FLAG,GUIDE_LEVEL,GUIDE_PRECISIONHAVING ROUND(AVG(DATA_VALUE),GUIDE_PRECISION) > GUIDE_LEVEL And didn't work. Definitely need some help.THanks. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-02-24 : 18:08:04
|
| Well, for starters, do you want the average value for each group ofSAMPLE_STATION,SAMPLE_START_DATE,DATA_PARAMETER,DATA_FLAG,GUIDE_LEVELsince that's what you're going to get with your query as it is. But only for recordswhere the start day and end day are the same. You may just need to add the sample_start_date to your SELECT and GROUP BY clauses.JimP.S. What do you mean by "didn't work"? Some sample data for each table and what you'd like the output to be would be very helpful.Everyday I learn something that somebody else already knew |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-25 : 00:25:30
|
| remove this check from the where clause of your query and check if you get any dataAND SAMPLE_START_DATE = SAMPLE_END_DATECheersMIK |
 |
|
|
sqlbug
Posting Yak Master
201 Posts |
Posted - 2011-02-25 : 12:39:17
|
| Hi Jim and Mik, the reason for the SAMPLE_START_DATE = SAMPLE_END_DATE is - I want average for each day. Because there are 24 hours of data for each day and SAMPLE_START_DATE / SAMPLE_END_DATE are same for all of them, I used that. So - what I need to get is - Average for each START_DATE/STATION/PARAMETER/METHOD/UNIT.Sample:SAMPLE_STATION SAMPLE_START_DATE DATA_PARAMETER (No column name) DATA_FLAG GUIDE_LEVEL1168 2010-01-20 00:00:00.000 81104 33.800000 NULL 30.00001168 2010-02-05 00:00:00.000 81104 30.100000 V 30.0000Thanks. |
 |
|
|
|
|
|
|
|