Solved

t-sql counting date records

Posted on 2016-08-26
2
32 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

20 Experts available now in Live!

Get 1:1 Help Now