Avatar of Grayson Hampton
Grayson Hampton

asked on 

Create new field based on results of existing field

I need to create a query that assigns a 1 to the first occurrence of a value and a 0 to any occurrence after for the BatchID field.'
The result would look like "NewField"

CompanyID       JobType            TempID                    Customer ID        OfficeID              SBUID      JobNumber      ConsultantID              BatchID              NewField
ADSE             Temp Job      2449129            113517               DENVER               ADMN       131705            2375138            TSB1.6.18A       1      
ADSE             Temp Job      2449129            113517               DENVER               ADMN       131705            2375138            TSB1.6.18A       0    
ADSE             Temp Job      2642569            113517               DENVER               ADMN       132512            2375138            TSB1.6.18B       1
* T-SQLSQL

Avatar of undefined
Last Comment
Grayson Hampton
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

>>try customize the part that highlighted above when it's necessary.

You may need to tweak the "order by" as well.  IT all depends on the requirements.  In this case, for duplicates across ALL columns, you need ALL columns in the partition by.  May not in the order by but for grins, I added them.
Avatar of Grayson Hampton

ASKER

Thank you slightwv and Ryan Chong.

Results:
CompanyID      JobType      TempID      CustomerNumber      OfficeID      SBUID      JobNumber      ConsultantID      BatchID      NewField
ADSE       Temp Job      2449129              113517               DENVER               ADMN       131705                 2375138R             TSB1.6.18A           1
ADSE       Temp Job      2449129              113517               DENVER               ADMN       131705                 2375138S             TSB1.6.18A           1
ADSE       Temp Job      2642569              113517               DENVER               ADMN       132512                 2375138S             TSB1.6.18B           1


Expected results(the second BatchID "TSB1.6.18A" should be 0):
CompanyID      JobType      TempID      CustomerNumber      OfficeID      SBUID      JobNumber      ConsultantID      BatchID      NewField
ADSE       Temp Job      2449129              113517               DENVER               ADMN       131705                 2375138R             TSB1.6.18A           1
ADSE       Temp Job      2449129              113517               DENVER               ADMN       131705                 2375138S             TSB1.6.18A           0
ADSE       Temp Job      2642569              113517               DENVER               ADMN       132512                 2375138S             TSB1.6.18B           1


Query:
with cte as (
      select pr.CompanyID, pr.JobType, pr.TempID, pr.CustomerNumber, pr.OfficeID, pr.SBUID, pr.JobNumber, pr.ConsultantID, pr.BatchID,
            row_number() over(partition by pr.CompanyID, pr.JobType, pr.TempID, pr.CustomerNumber, pr.OfficeID, pr.SBUID, pr.JobNumber, pr.ConsultantID, pr.BatchID
                                    order by pr.CompanyID, pr.JobType, pr.TempID, pr.CustomerNumber, pr.OfficeID, pr.SBUID, pr.JobNumber, pr.ConsultantID, pr.BatchID) rn
      from Rept] pr
      where [TransactionDate] = '01/06/2018'
      and pr.ConsultantID like '2375138%'
      and pr.[CustomerNumber] = 113517
      and OfficeId = 'DENVER'
)
select CompanyID, JobType, TempID, CustomerNumber, OfficeID, SBUID, JobNumber, ConsultantID, BatchID,
      case when rn=1 then 1 else 0 end NewField
from cte;
The consultantID is different which is why you get a 1.  2375138R and 2375138S for the same job number.  So, they aren't "duplicates".



Figure out the fields that determine "distinct" rows and make sure those are the ones in the "partition by" clause.

Depending on which row you want to be 1 and which ones are 0, those are the columns you need to care about in the "order by".
Avatar of Grayson Hampton

ASKER

Thanks Ryan Chong and slightwv. I have a part 2 to this question... I will open a new question to ask it.
Avatar of Grayson Hampton

ASKER

Thank you both for being very helpful.
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo