3520: A constant value in a query is not valid for column sale_month.
3535: A character string failed conversion to a numeric value.
2665: Invalid date.
All of these errors are due to the date value that you are inserting. If you encounter any of these errors, try permutation and combination of the date value and see if you can get rid of the error. If you still can not then find out from your sys admin what date format in allowed while you are inserting dates. Here in this example the format is ‘yyyy-mm-dd’. All of the below statements are tested in SQL assistant. Create a Volatile Table “Sales” as follows:
CREATE SET VOLATILE TABLE <username>.sales,
NO FALLBACK,
CHECKSUM = DEFAULT,
LOG
(
“store_no” INTEGER,
“sale_month” DATE,
“prod_code” CHAR (20) CHARACTER SET LATIN NOT CASESPECIFIC,
“projected_sale” INTEGER,
“actual_sale” INTEGER
)
PRIMARY INDEX ( “store_no” )
ON COMMIT PRESERVE ROWS;
Insert data as follows:
INSERT INTO sales VALUES(10,’2007-01-01′,’Apple’,15000,13500);
INSERT INTO sales VALUES(20,’2007-02-01′,’Banana’,10000,11000);
INSERT INTO sales VALUES(30,’2007-03-01′,’Orange’,11000,10500);
INSERT INTO sales VALUES(40,’2007-04-01′,’Apricot’,12500,12500);
INSERT INTO sales VALUES(50,’2007-05-01′,’Grapes’,15000,12500);
INSERT INTO sales VALUES(60,’2007-06-01′,’Apple’,12000,11500);
INSERT INTO sales VALUES(70,’2007-07-01′,’Banana’,15500,11500);
INSERT INTO sales VALUES(80,’2007-07-01′,’Grapes’,15000,15500);
INSERT INTO sales VALUES(90,’2007-06-01′,’Orange’,16000,15500);
INSERT INTO sales VALUES(10,’2007-05-01′,’Apricot’,14500,13500);
INSERT INTO sales VALUES(20,’2007-04-01′,’Mango’,15500,12500);
INSERT INTO sales VALUES(30,’2007-03-01′,’Mango’,15000,12500);
INSERT INTO sales VALUES(40,’2007-02-01′,’Guava’,14000,11500);
INSERT INTO sales VALUES(50,’2007-01-01′,’Guava’,12000,10500);
INSERT INTO sales VALUES(30,’2007-02-01′,’Strawberry’,12000,10000);
Now take the example of AVG () function. As you know AVG () computes the cumulative or moving average of a column, we will calculate something similar in the following scenario.
Scenario is to find out the actual sales in each store, averaged over the current month and the preceding month. Here preceding month would be understood as the previous month to the current month, now what is current month, it is the first row returned after the result set is ordered after partitioning. Following is the query.
SELECT store_no,
sale_month,
actual_sale,
projected_sale,
AVG(actual_sale) OVER (PARTITION BY store_no
ORDER BY projected_sale ROWS 1 PRECEDING)
FROM sales;
And it would return the following result set.
store_no sale_month actual_sale projected_sale Moving Avg(actual_sale)
10 5/1/2007 13,500 14,500 13,500.00
10 1/1/2007 13,500 15,000 13,500.00
20 2/1/2007 11,000 10,000 11,000.00
20 4/1/2007 12,500 15,500 11,750.00
30 3/1/2007 10,500 11,000 10,500.00
30 2/1/2007 10,000 12,000 10,250.00
30 3/1/2007 12,500 15,000 11,250.00
40 4/1/2007 12,500 12,500 12,500.00
40 2/1/2007 11,500 14,000 12,000.00
50 1/1/2007 10,500 12,000 10,500.00
50 5/1/2007 12,500 15,000 11,500.00
60 6/1/2007 11,500 12,000 11,500.00
70 7/1/2007 11,500 15,500 11,500.00
80 7/1/2007 15,500 15,000 15,500.00
90 6/1/2007 15,500 16,000 15,500.00
Note that the moving average number is changing every 2nd row in case there is a difference in between the 1st and 2nd row figure within the same store number. Particularly for the store number 30, you can see that there is a slight increase in the moving average in spite of good increase in actual sales. Projected Sales is also added just to have an idea how the actual sale is performing over the project sale.
Our Random Articles
- Somdev Devvarman ousts Xavier Malisse 6-1, 3-6,7-6(5)
- How To Choose A Good Online Traffic School
- New Year Wishes 2010
- New Year Wishes
- How to use EXTRACT function with date and time columns
More Links




4 Comments
Material that you are providing is very easy to learn. But it is much better than this , if you ll give the full material of this at ONE LINK.
Thank you
Suresh.
That is right Suresh. This is the reason I have given all the links in one page, please visit this
http://readvitamin.com/teradata/
Hope this helps.
Hi. Can anyone tell me how to extract time from timestamp?
For Example
timestamp=’2008-03-28 14:11:04′
i want time ’14:11:04′ from timestamp through query.
Thanks in advance.
Hi Vaideesh,
If you need only time, you can probably try the following;
SELECT time;
To answer your question you should do something like this.
SELECT CAST(VaideeshTime as time(0)) from timestamp;
VaideeshTime
——–
14:11:04
Hope that helps.