Link to home
Create AccountLog in
Avatar of Marisa Stevenson
Marisa StevensonFlag for United States of America

asked on

SQL Script - Querying a result set

How would I modify the script below to select  rows from its result set?  I want to take the results of this query, and from those results/subset of data, select rows with a Persons.LastUpdated > a specific date?  Do I have to run it first to generate an output file, then create a temp table from that output file?  In the end, I want to take this subset of data and create a pipe-delimited text file.

For example, if the output from the script below contains records for BMSId 9 with a Persons.LastUpdated date of 3/10/2016, I want to be able to search the result set (only - not the entire db) for BMSId 9 with a Persons.LastUpdated > 3/10/2016.

The script below is translated from MS Access.  We have an Access Macro that directs the output to a pipe delimited file.  

SELECT dbo_BadgeHistory.serialno, dbo_BadgeHistory.proxnumber AS CardNo, dbo_BadgeHistory.Status AS BadgeStatus, Right([ssno],4) AS Expr1, Replace([LastName],Chr(13) & Chr(10),"") AS Expr2, Replace([FirstName],Chr(13) & Chr(10),"") AS Expr3, IIf(Not Null,Replace([Middle],Chr(13) & Chr(10),""),[Middle]) AS Expr4, dbo_Persons.IDNumber AS ImageID, Replace([LastUpdated],Chr(13) & Chr(10),"") AS Expr5, dbo_Persons.CompanyIndex AS VendorIndex, dbo_Persons.DivisionIndex AS SponsorIndex, dbo_Persons.PersonStatus, dbo_Persons.FullOrganizationName, dbo_Persons.FullDivisionName, IIf(Len([district])=7,Right([district],2) & Left([district],2),IIf(Left([district],3)="A/L",Right([district],2) & "00",IIf(Len([district])=6,Right([district],2) & "0" & Left([district],1)))) AS Expr6, dbo_Persons.organizationtype, dbo_Persons.Congress, dbo_Persons.PersonExpireDate, dbo_Persons.LASTUPDATED
FROM dbo_Persons INNER JOIN dbo_BadgeHistory ON dbo_Persons.BMSID = dbo_BadgeHistory.Bmsid
WHERE (((dbo_BadgeHistory.proxnumber) Is Not Null) AND ((dbo_BadgeHistory.Status)="ACTIVE" Or (dbo_BadgeHistory.Status)="INACTIVE" Or (dbo_BadgeHistory.Status)="DESTROYED" Or (dbo_BadgeHistory.Status)="NON-RETURNED" Or (dbo_BadgeHistory.Status)="NON-RETURNED-B" Or (dbo_BadgeHistory.Status)="lost" Or (dbo_BadgeHistory.Status)="stolen") AND ((dbo_Persons.IDNumber)=[dbo_badgehistory].[idnumber]) AND ((dbo_Persons.LASTUPDATED)=Date() Or (dbo_Persons.LASTUPDATED)=Date()-1) AND ((dbo_Persons.LastName) Not Like "1*" And (dbo_Persons.LastName) Not Like "2*" And (dbo_Persons.LastName) Not Like "testing*"))
ORDER BY dbo_Persons.IDNumber, dbo_BadgeHistory.serialno DESC , dbo_Persons.LastName;

Open in new window

SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Marisa Stevenson

ASKER

Vitor,

It's a "duh!" moment for me.  Another case of making things more complicated than they need to be - I had actually starting adding column definitions for a temp table.  

Thanks so much!  Appreciate the fast response!

m