从column = 1和第2列MsSql 2012中选择Id

时间:2014-04-21 18:04:57

标签: sql-server-2012

SELECT Distinct ListingId,FieldId,FieldValue
  FROM [Chant-GreyPar].[dbo].[gp_listing_field]
  where (FieldId = 54) and (FieldId = 69)
      order by ListingId asc

你好,我有桌子

ListingId   FieldId FieldValue
238878  54  Paupackan Lake
238878  69  N
238879  54  None
238879  69  N
238880  54  Westcolang Lake
238881  54  None
238882  54  None

我需要选择获得FieldId 54和69的ID ...需要你的帮助。

更新:

select distinct l.Id,[SquareFeet],[HouseNumber],[StreetAddress],[PropertyTypeId],[Bedrooms],[Bathrooms],[ListingPrice],
(select top 1 PhotoUrl from [Chant-GreyPar].dbo.gp_listing_photo where gp_listing_photo.ListingId = f.ListingId ) AS PhotoUrl,  
(Select AreaName1 from [Chant-GreyPar].dbo.gp_location where gp_location.Id = l.LocationId) AS AreaName1,
(Select AreaStateCode from [Chant-GreyPar].dbo.gp_location where gp_location.Id = l.LocationId) AS AreaStateCode 
from [Chant-GreyPar].dbo.gp_listing l inner join [Chant-GreyPar].[dbo].[gp_listing_field] f 
on f.ListingId = l.Id left join [Chant-GreyPar].dbo.gp_vw_DecimalListingField s on s.ListingId = l.Id 
where  (l.DisplayListing='1' and  f.FieldId='69' and f.FieldValue='Y') and (l.DisplayListing='1' and f.FieldId='15' and f.FieldValue='Window Unit AC' or f.FieldValue='Wall Unit AC' or f.FieldValue='Window Unit AC' or f.FieldValue='Central AC')
 and ListingPrice >= 0 and ListingPrice <= 99999999999 and Bedrooms >= 0 and Bathrooms >= 0 
 and SquareFeet >= 0  and (FieldValueDecimal >= 0 or FieldValueDecimal is null ) 
 order by ListingPrice desc 

如何在这里集成它。 Thnks。

1 个答案:

答案 0 :(得分:1)

好的,如果您正在尝试做我认为您尝试做的事情,请使用此功能:

SELECT DISTINCT ListingId, FieldId, FieldValue
FROM [Chant-GreyPar].[dbo].[gp_listing_field]
WHERE ListingId IN (
    SELECT ListingId
    FROM [Chant-GreyPar].[dbo].[gp_listing_field]
    WHERE FieldId IN ( 54, 69 )
    GROUP BY ListingId
    HAVING Count(ListingId) = 2
)
ORDER BY ListingId ASC

这使用子查询(WHERE子句中的SELECT语句)来获取具有2条记录的所有ListingId的列表。子查询中的WHERE子句按您要查找的2个值进行筛选 - 54和69 - 因此它只会为您提供同时包含54和69的记录。然后主查询使用该列表获取只是那些行的值。

如果没有我前面的数据库,您的更新会有点复杂。您所要做的就是使用我的子查询代替WHERE子句的第一部分,您将在其中搜索FieldId值。不能保证我的语法在这里是正确的:

USE [Chant-GreyPar]  -- This eliminates the need to keep repeating it in the query
GO
SELECT DISTINCT l.Id, SquareFeet, HouseNumber, StreetAddress, PropertyTypeId, Bedrooms, Bathrooms, ListingPrice,
    (SELECT TOP 1 PhotoUrl FROM dbo.gp_listing_photo WHERE gp_listing_photo.ListingId = f.ListingId ) AS PhotoUrl,  
    (SELECT AreaName1 FROM dbo.gp_location WHERE gp_location.Id = l.LocationId) AS AreaName1,
    (SELECT AreaStateCode FROM dbo.gp_location WHERE gp_location.Id = l.LocationId) AS AreaStateCode 
FROM dbo.gp_listing l
INNER JOIN dbo.gp_listing_field f ON f.ListingId = l.Id
LEFT JOIN dbo.gp_vw_DecimalListingField s ON s.ListingId = l.Id 
WHERE ListingId IN (
    SELECT ListingId
    FROM dbo.gp_listing_field
    WHERE (l.DisplayListing='1' AND f.FieldId='69' AND f.FieldValue='Y')
    OR (l.DisplayListing='1' AND f.FieldId='15' AND f.FieldValue IN ('Window Unit AC', 'Wall Unit AC', 'Window Unit AC', 'Central AC' ))
    GROUP BY ListingId
    HAVING Count(ListingId) = 2
)
AND ListingPrice BETWEEN 0 AND 99999999999
AND Bedrooms >= 0
AND Bathrooms >= 0
AND SquareFeet >= 0
AND (FieldValueDecimal >= 0 OR FieldValueDecimal IS NULL)
ORDER BY ListingPrice DESC

顺便说一下,您将第二个FieldId值从54更改为15.不确定这是否是故意的。