I am assigned a problem which i am not sure how to write it in SQL SEVER (version 5/8) yet. Here is the simple query and its output:
Select location, date_time, Item_sold
From Product
Location Date_time Item_sold
VA 12/10/2010 1:30:00 PM Candy
VA 12/10/2010 3:30:00 PM Chips
VA 12/13/2010 12:50:00 AM Wine
DC 12/13/2010 8:00:00 AM Gum
DC 12/13/2010 12:30:00 PM Bags
DC 12/13/2010 1:16:00 PM Cheese
DC 12/13/2010 12:00:00 AM Hotdog
NJ 12/14/2010 12:00:00 AM Coffee
NJ 12/14/2010 1:15:00 PM Beers
NJ 12/14/2010 12:00:00 AM Coffee
NJ 12/14/2010 1:45:00 PM Water
Here is my desired output, which I guess a while/for loop or a pivot function could do the job but my experience is not there yet. Basically, I need to count the number of item sold from the Item_sold column incrementally (base line date starts from 12/8 to 12/9, 12/8 to 12/10, 12/8 to 12/11, 12/8 to 12/12...)
Location 12/10 to 12/11 12/10 to 12/12 12/10 to 12/13 12/10 to 12/14
VA 2 2 3 3
DC 0 0 3 3
NJ 0 0 0 4
I was wondering if anyone could edcuate and solve this problem. Many thanks in advance, Joe
Sample table and data
The T-SQL to produce the results required
The above, from cyberkiwi, is too complicated. You need to do something called a 'table rotation'. Assuming you have a product table like this:
The sql you want is the following SELECT statement: