?
Solved

Sql how to exclude from a table

Posted on 2016-08-24
3
Medium Priority
?
134 Views
Last Modified: 2016-08-25
Need some help here. Trying to write a select statement that pulls store/agreement/order information. I want all store and agreement information from AGR_DAILY_BuyerStep table except for those in the manually created exclusion table.


declare @OrgUnits table (OrgunitID typ__ID) --this table holds store info
declare @StoreAgmnt table (OrgunitId typ__id, AgreementId typ__id) --agreements by store to exclude
declare @OrgUnitIds Varchar(max) --store ids
      set @OrgUnitIds = ('10000032-10002125-10002124-10002123-10002120-10002119') -- stores we are using

--exclusion table
insert into @StoreAgmnt
            (OrgunitId, AgreementId)
      VALUES
            (10000032,10000001),
            (10002125,10000004),
            (10002124,10000005),
            (10002123,10000001),
            (10002120,10000004),
            (10002119,10000001)

--accepts store params
insert into @OrgUnits (OrgunitID)
 select item from fn__String2TableEx(@OrgUnitIds,'-')

--select all POIDS except for those in  exclusion table
SELECT distinct(POID)
  FROM AGR_DAILY_BuyerStep a
  --left outer join @StoreAgmnt b on a.BuyerID=b.OrgunitId
  where
      buyerid in (select OrgunitID from @OrgUnits)  
      and NOT EXISTS(  -----this is the part i am not sure of how to exclude?
0
Comment
Question by:jacobJL
[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
  • 2
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 total points
ID: 41769215
<knee jerk reaction>
>I want all store and agreement information from AGR_DAILY_BuyerStep table except for those in the manually created exclusion table.
SELECT distinct(POID)
FROM AGR_DAILY_BuyerStep a
   --- The LEFT means all rows in AGR_Daily_BuyerStep are included.. 
   LEFT JOIN @StoreAgmnt b on a.BuyerID=b.OrgunitId
-- The WHERE means now filter out all rows where there is a matching @StoreAgmnt row
WHERE b.OrgunitId IS NULL

Open in new window

1
 

Author Closing Comment

by:jacobJL
ID: 41770532
Thank you, this was close enough. got me on the right path. I had this at one point. the problem was I needed to add a.AgreementId = b.AgreementId  to the  join clause
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 41770537
Thanks for the accept.  btw if you do an images.google.com search for 'SQL JOIN' you'll see lots of pretty pictures with T-SQL on how that works.
0

Featured Post

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

764 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