sql查询根据文本和组ID返回项目

时间:2010-05-21 23:22:26

标签: sql select

我不确定如何最好地描述我想要做的事情(或者为此寻找它),但我会尝试。

我有一个预先存在的查询(存储过程),它根据3个特定的ID返回项目。我想要做的是能够根据具有完全相同字符串的结果中的列进一步减少结果。我希望查询返回某个列中具有完全相同值的项目数。

出于某种原因,我对如何做到这一点感到困惑,我对此完全不了解。

这是当前数据的样本:

PROGRAMID   DAY TIMESLOT         SlotInfoDescription    TITLE
2688         4   13:00-16:30      Free Time              NULL
2688         4   16:30-17:10      NULL                 Joint Session with Search Engines: Machine Learning for Social Media Analytics
2689         4   13:10-16:00      Free Time              NULL
2689         4   16:30-17:10      NULL                   Joint Session with Search Engines: Machine Learning for Social Media Analytics
2300         4   12:00-13:30      Free Time
2300         4   16:30-17:10      NULL                   Joint Session with Search Engines: Machine Learning for Social Media Analytics

以下是我想要的结果(基于标题相同):

count = 1

以下是当前查询:

    SELECT
    WS.PROGRAMID, 
    WS.ITEMID, 
    WS.DAY, 
    CASE datepart(weekday, dateadd(day, WS.DAY - 1, WE.EVENTSTARTDATE))
        WHEN 1 THEN 'Sun'
        WHEN 2 THEN 'Mon'
        WHEN 3 THEN 'Tue'
        WHEN 4 THEN 'Wed'
        WHEN 5 THEN 'Thu'
        WHEN 6 THEN 'Fri'
        WHEN 7 THEN 'Sat'
        ELSE 'N/A'
    END
    + ' ' + Convert(char(12), dateadd(day, WS.DAY - 1, WE.EVENTSTARTDATE), 101) as Date, 
    WS.TIMESLOT, 
    WS.SLOTTYPE, 
    WSSTC.DESCRIPTION AS SlotTypeDescription, 
    WS.SLOTINFO, 
    WSSIC.DESCRIPTION AS SlotInfoDescription, 
    ISNULL(WSI.TALKID, 0) AS TalkID, 
    LEFT(WPI.FIRSTNAME, 10) + ' ' + LEFT(WPI.LASTNAME, 10) + ' (' + LEFT(WEA.INSTITUTION, 10) + ')' + ' - ' + LEFT(WSI.TITLE, 10) + '...' + RIGHT(WSI.TITLE, 10) AS Talk, 
    WPI.FIRSTNAME, 
    WPI.LASTNAME, 
    WEA.INSTITUTION, 
    WSI.AUTHORLINE, 
    WSI.TITLE, 
    LEFT(WSI.ABSTRACT, 1) AS Abstract, 
    WS.LOCATION, 
    WS.ADDENDUM
FROM 
    DBO.WEBSPEAKERINFO WSI 
    INNER JOIN
        DBO.WEBPERSONALINFO WPI 
        ON 
            WSI.USERID = WPI.USERID 
    INNER JOIN 
        DBO.WEBEVENTAFFILIATION WEA 
        ON 
            WPI.USERID = WEA.USERID 
            AND 
            WEA.EVENTID IN @EVENTID 
    RIGHT OUTER JOIN
        DBO.WEBSCHEDULESLOTTYPECODES WSSTC 
    RIGHT OUTER JOIN
        DBO.WEBSCHEDULES WS 
        ON 
            WSSTC.SLOTTYPE = WS.SLOTTYPE 
        ON 
            WSI.TALKID = WS.TALKID 
    LEFT OUTER JOIN
        DBO.WEBSCHEDULESLOTINFOCODES WSSIC
        ON 
            WS.SLOTINFO = WSSIC.SLOTINFO
    INNER JOIN
        DBO.WEBEVENTS WE
        ON
            WE.EVENTID IN @EVENTID
WHERE
    WS.PROGRAMID IN @EVENTID
    AND
    WS.DAY = @DAY
ORDER BY
    DAY,
    TIMESLOT

1 个答案:

答案 0 :(得分:1)

假设您正在使用SQL Server。 既然你知道存储过程返回的列是什么,你可以这样做:

create #tempTable ( columnsSprocReturns )
insert into #tempTable
exec yourSproc

select columns 
from   #tempTable
where  someColumn = 'someValue'

drop table #tempTable