Solved

How to number records individually when more than one record exists with the same datetime

Posted on 2014-10-28
5
140 Views
Last Modified: 2014-10-28
I have a table that is populated by a different company and can not have them insert the time differently.  The records come looking like image 1.  As you can see the highlighted records show 2 different records but with the same time and date.  
Image1.
I need to number the records for each date 1,2,3,4......  Like image2.  The order of the records for each date are not important, just how many there were and the amount.    
Image2Image3 will give you an idea of what we are trying to accomplish in the end.
Image3Thank you.
0
Comment
Question by:rcowen00
[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
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 500 total points
ID: 40409389
try this:

SELECT 
   Location, depositdate,amount, id    
   ROW_NUMBER() OVER (PARTITION BY Location, depositdate ORDER By Location, depositdate) as depNumber
FROM SOURCE

Open in new window

0
 

Author Comment

by:rcowen00
ID: 40409568
I tried in this stored procedure and am getting "incorrect syntax near )"

SELECT Locations.[Location Name] AS LocationName, Deposits.Date, Deposits.Amount,
      Deposits.Id, StoreType.StoreTypeName, id    
   ROW_NUMBER() OVER (PARTITION BY Location, depositdate ORDER By Location, depositdate) As depNum
FROM         Locations INNER JOIN
                      Deposits ON Locations.LocationNameOrig = Deposits.[Location Name] LEFT OUTER JOIN
                      StoreType ON Locations.StoreTypeID = StoreType.StoreTypeID
GROUP BY Locations.[Location Name], Deposits.Date, Deposits.Amount, Deposits.Id, StoreType.StoreTypeName
HAVING      (Deposits.Date BETWEEN @Begin AND @End) AND (Locations.[Location Name] = N'POS013')
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40409574
Try this:

SELECT Locations.[Location Name] AS LocationName, Deposits.Date, Deposits.Amount, 
      Deposits.Id, StoreType.StoreTypeName, id    
   ROW_NUMBER() OVER (PARTITION BY Location, depositdate ORDER By Location, depositdate) As depNum 
FROM         Locations INNER JOIN
                      Deposits ON Locations.LocationNameOrig = Deposits.[Location Name] LEFT OUTER JOIN
                      StoreType ON Locations.StoreTypeID = StoreType.StoreTypeID
					  where       (Deposits.Date BETWEEN @Begin AND @End) AND (Locations.[Location Name] = N'POS013')
GROUP BY Locations.[Location Name], Deposits.Date, Deposits.Amount, Deposits.Id, StoreType.StoreTypeName

Open in new window

0
 

Author Comment

by:rcowen00
ID: 40409575
I got it, I was missing a comma
0
 

Author Closing Comment

by:rcowen00
ID: 40409612
This is the final

SELECT Locations.[Location Name], Deposits.Date, Deposits.Amount,
      Deposits.Id, StoreType.StoreTypeName,
   ROW_NUMBER() OVER(PARTITION BY Locations.[Location Name], Deposits.Date ORDER By [Locations].[Location Name], Deposits.Date) As depNum
FROM         Locations INNER JOIN
                      Deposits ON Locations.LocationNameOrig = Deposits.[Location Name] LEFT OUTER JOIN
                      StoreType ON Locations.StoreTypeID = StoreType.StoreTypeID
GROUP BY Locations.[Location Name], Deposits.Date, Deposits.Amount, Deposits.Id, StoreType.StoreTypeName
HAVING      (Deposits.Date BETWEEN @Begin AND @End) AND (Locations.[Location Name] = N'POS013')
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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