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

Microsoft SQL Server 2008Microsoft AccessMicrosoft SQL Server

Avatar of undefined
Last Comment
Marisa Stevenson
SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland 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.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Bitsqueezer
Bitsqueezer
Flag of Germany 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 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
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
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