-2

i would to add this calculation:

select datepart(week, Order_date) as Weeks ,sum(Quantity) as Jumlah_Order 
from sales group by datepart(week, order_date);

to this table:

create table sales(Order_date date,Quantity int);

enter image description here

but i still not get it, because i query

ALTER TABLE sales
ADD datepart(week, Order_date) as Weeks ,sum(Quantity) as Jumlah_Order int
from sales group by datepart(week, order_date);

there's error message

Pranav Singh
  • 17,079
  • 30
  • 77
  • 104

1 Answers1

-1

You cannot have calculated columns on a table. If you want to have a queryable dataset with calculated columns, you could consider looking into creating a 'view'

To add 'weeks' as a column, you could first add the column to your table, and then populate it with the datepart;

ALTER TABLE sales ADD Weeks int;
UPDATE sales SET Weeks = datepart(week, Order_date);

For the 2nd column, 'Quantity', it seems you want to populate an aggregated value (a SUM). As you store data on row-level, but you want a grouped column (=SUM), that logically does not work.