Solved

t-sql counting date records

Posted on 2016-08-26
2
28 Views
Last Modified: 2016-08-26
hi experts,

I'm using sql server 2008.

I have a table that looks like this:

my table
Here is script to create this table.

CREATE TABLE [dbo].[TestOrders](
	[CustomerNo] [int] NULL,
	[OrderNo] [int] NULL,
	[OrderDate] [datetime] NULL,
	[ItemOrdered] [nvarchar](255) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1201, 1245, CAST(0x0000A64900000000 AS DateTime), N'envelopes')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1403, 1456, CAST(0x0000A64B00000000 AS DateTime), N'pencils')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1403, 1899, CAST(0x0000A64C00000000 AS DateTime), N'pens')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1403, 1987, CAST(0x0000A64D00000000 AS DateTime), N'erasers')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1101, 2002, CAST(0x0000A64F00000000 AS DateTime), N'ink')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1101, 2102, CAST(0x0000A65000000000 AS DateTime), N'printer paper')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1101, 2102, CAST(0x0000A65100000000 AS DateTime), N'pencils')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (1101, 2234, CAST(0x0000A65200000000 AS DateTime), N'ball point pen')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (578, 2568, CAST(0x0000A65300000000 AS DateTime), N'pencils')
INSERT [dbo].[TestOrders] ([CustomerNo], [OrderNo], [OrderDate], [ItemOrdered]) VALUES (578, 2756, CAST(0x0000A65400000000 AS DateTime), N'pens')

Open in new window


My desired result looks like this:

desired result
Notice the column called OrderDays that is the one i need to to create.
The OrderDays column is the count of OrderDate items for each CustomerNo.

For example, if you look at CustomerNo 1403. That customer placed an order on 7/22/2016, another order on 7/23/16 and another order on 7/24/16.  So on the OrderDays column, on 7/22/2016 that was the first day he placed an order so it gets 1, on 7/23/2016 that was the second day he placed an order so it gets 2,
on 7/24/2016 that was the third day he placed an order so it gets 3,

Any idea of the syntax to create the OrderDays column?
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 41772443
>The OrderDays column is the count of OrderDate items for each CustomerNo.
Using the windowing function ROW_NUMBER will handle this.

SELECT CustomerNo, OrderNo, OrderDate, ItemOrdered, 
   ROW_NUMBER() OVER (PARTITION BY CustomerNo ORDER BY OrderNo) as OrderDays
FROM TestOrders
ORDER BY OrderDate

Open in new window

0
 
LVL 1

Author Closing Comment

by:maqskywalker
ID: 41772478
Thanks.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now