Solved

t-sql counting date records

Posted on 2016-08-26
2
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 66

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

636 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