SQL Server中的子查询和存在

时间:2018-11-16 17:07:14

标签: sql sql-server tsql sql-server-2008-r2

我正在运行以下查询,它从tblFamilytblConstituent返回结果集中的所有列。

SELECT
    *
FROM
    tblFamily AS f
WHERE
    DataProviderID = 23
    AND ISNULL(UsesIndexMarker, 0) = 0
    AND EXISTS (SELECT 1
                FROM tblConstituent
                WHERE FamilyID = f.FamilyID
                  AND StartDate < GETDATE()
                  AND EndDate > GETDATE()
                  AND FilterValue LIKE '%-%-%');

现在,我只希望从tblFamilytblConstituent的结果集中选择/很少列,如下所示。但是当我运行时,我可以从tblFamily获取列,但不能从tblConstituent获取任何列。

SELECT
    td.FamilyID, 
    td.Name,
    td.DataProviderID,
    td.UsesIndexMarker,
    td.OpenDate,
    td.ListingID
FROM
    (SELECT *
     FROM tblFamily AS f
     WHERE DataProviderID = 23
       AND ISNULL(UsesIndexMarker, 0) = 0
       AND EXISTS (SELECT 1
                   FROM tblConstituent AS tc
                   WHERE tc.FamilyID = f.FamilyID
                     AND tc.StartDate < GETDATE()
                     AND tc.EndDate > GETDATE()
                     AND tc.FilterValue LIKE '%-%-%'
                     AND f.DataProviderID = 23
                     AND ISNULL(f.UsesIndexMarker, 0) = 0)) AS td;

有人知道为什么在我运行第一个查询时所有数据都可用时为什么无法从tblConstituent获取数据吗?我收到此错误:

  

第207层,状态1,第79行,第79行
  无效的列名“ ListingID”

希望我的问题很清楚。

谢谢。

PS:下表定义:

tblFamily

FamilyID    int
RefFamilyID int
FamilyTypeID    int
Name    varchar
DataProviderID  int
CalendarID  int
TrustedPrices   tinyint
FXRateSetID int
CurrentDate date
LastRevision    bigint
HasClose    tinyint
HasOpen tinyint
HasTPlus    tinyint
OffsetFromMaxAsAtDate   tinyint
PriceSetID  int
DividendSetID   int
CorporateActionSetID    int
IncludeFilterInNaturalKey   tinyint
MatchAllCodes   tinyint
CodeReferenceFamilyID   int
LatestTaskRunID int
TrustedFXRates  tinyint
HasDividends    tinyint
HasPrices   tinyint
HasRebalance    tinyint
HasReferenceData    tinyint
HasIntradayChanges  tinyint
TplusDataIsDelta    tinyint
ValidateAfterPersisting tinyint
LastUpdate  datetime
FundProviderID  int
TimeDataExpires time
ExcludeZeroNumberOfUnits    bit
PrefilterConstituents   bit
LimitEndDate    bit
IgnoreForPricing    bit
ExcludedAssetSetID  int
NextRebalanceDate   date
TaxRateSetID    int
OpenDataIsDelta bit
RebalanceDataIsDelta    bit
OpenDate    date
AutoAddMissingListing   bit
EnforcedSecurityTypeID  int
TplusDataIsCummulative  bit
CalculationSetID    int
ValidationSetID int
UsesIndexMarker bit
MappingReferenceFamilyID    int
DataExtractionSetID int
MappingReferenceSecurityID  int
AutoAddOddLots  bit
DefaultFilterValue  varchar
SecurityClassID int
CountryClassificationID int
DataStatusMask  int
RebalanceFrequency  varchar
RebalanceNotes  varchar
NextRebalanceDataAvailable  date
CheckFileDates  bit
IsPayDateUseForReinvestment int
ValidationThresholdSetID    int
Created datetime

和表 tblConstituent

ConstituentID   bigint
FamilyID    int
ListingID   int
StartDate   date
EndDate date
FilterValue varchar
ConstituentType tinyint
NumberOfUnits   decimal
PriceAdjustmentFactor   decimal
Factor1 decimal
Factor2 decimal
Factor3 decimal
Factor4 decimal
Factor5 decimal
Factor6 decimal
Factor7 decimal
Factor8 decimal
Factor9 decimal
IsGettingArchived   tinyint

2 个答案:

答案 0 :(得分:3)

您是正确的Zeki Gumus。

但是不需要在下面加入等号事件

    AND f.DataProviderID = 23
    AND ISNULL(f.UsesIndexMarker, 0) = 0

“哪里”存在它们

答案 1 :(得分:1)

您不能将ListingId列出,因为您将tblConstituent与EXISTS一起使用,而不是JOIN。请检查以下查询,我相信这是您想要的:

SELECT  F.FamilyID, 
        F.Name,
        F.DataProviderID,
        F.UsesIndexMarker,
        F.OpenDate,
        TC.ListingID
FROM    tblFamily AS f
INNER JOIN tblConstituent AS tc
                   ON tc.FamilyID = f.FamilyID
                     AND tc.StartDate < GETDATE()
                     AND tc.EndDate > GETDATE()
                     AND tc.FilterValue LIKE '%-%-%'
                     AND f.DataProviderID = 23
                     AND ISNULL(f.UsesIndexMarker, 0) = 0

我只是注意到我们不需要这一部分。谢谢Cagdas。

--WHERE DataProviderID = 23
--AND       ISNULL(F.UsesIndexMarker, 0) = 0