选择区别于不同的结果

时间:2013-03-05 10:03:58

标签: sql

以下是查询

select DISTINCT movie_master.MovieMasterID, MOVIE_VERSION.ReleaseDate, 
movie_master.displayorder,movie_classification.mc_code,
movie_master.title As Title,
movie_master.BioCast,movie_master.BioDirector,movie_master.LinkWord,
CASE WHEN ((DATEDIFF(day,MOVIE_VERSION.ReleaseDate,getdate())  <=7) and (DATEDIFF(day,MOVIE_VERSION.ReleaseDate,getdate()) >=0)) THEN 'JUST RELEASED' ELSE '' END AS RELEASETAG,
movie_master.Synopsis,movie_language.NAME as Language,
CASE WHEN movie_master.DistributorID = 8 THEN 0 ELSE 9999 END AS distributororder,
movie_distributor.name as distributor
from movie_master 
left join MOVIE_LANGUAGE on MOVIE_MASTER.Language = movie_language.languageid
left join movie_distributor on MOVIE_MASTER.distributorid = movie_distributor.distributorid
left join movie_version on MOVIE_MASTER.MovieMasterID = MOVIE_VERSION.MovieMasterID
left join movie_classification on MOVIE_MASTER.classification = movie_classification.classificationid
left join movie_itemdetails on MOVIE_MASTER.MovieMasterID = movie_itemdetails.MovieMasterID 
where MOVIE_VERSION.ReleaseDate <= convert(varchar, GETDATE(), 101) 
and MOVIE_VERSION.MovieMasterID = movie_master.MovieMasterID
and movie_master.classification = movie_classification.classificationid
and movie_master.MovieMasterID = movie_itemdetails.MovieMasterID and movie_itemdetails.fieldid = 1 and movie_itemdetails.Type ='TRAILER'
and ((MOVIE_VERSION.FilmCode in (SELECT DISTINCT FILMID FROM MOVIE_TEMP)) OR (MOVIE_VERSION.Title in (SELECT DISTINCT TITLE FROM MOVIE_TEMP)))  
AND LEFT(MOVIE_VERSION.FILMCODE,1) <> 6
and MOVIE_VERSION.Enabled = 1
AND MOVIE_MASTER.Enabled = 1
AND (MOVIE_MASTER.MovieMasterID in (SELECT DISTINCT MOVIE_MASTER.MovieMasterID FROM MOVIE_MASTER))
order by MOVIE_MASTER.displayorder, MOVIE_VERSION.ReleaseDate desc, distributororder, MOVIE_MASTER.TITLE

和结果集

310 2013-03-01 00:00:00.000 9999    NA  test456             JUST RELEASED       NA  9999
310 2013-02-28 00:00:00.000 9999    NA  test456             JUST RELEASED       NA  9999
244 2013-02-10 00:00:00.000 9999    U   I Love Hong Kong 2013   Alan Tam, Veronica Yip, Natalis Chan, Eric Tsang, Stanley Fung, Bosco Wong, Michael Tse, Kate Tsui, Joyce Cheng Chung Shu-Kai           The story takes place in the 1970s until modern times, in which all families are happily prepare and welcome the Chinese New Year in Hong Kong. However, a traditional restaurant in Yau Ma Tei faces terrible problem. At last, everything solved with the neighbours’ kind support and assistance as happy ending.    CANTONESE   0

我的问题是,如何进一步增强查询以区分相同的记录?

3 个答案:

答案 0 :(得分:0)

您可以选择上次日期,并按选择列表中的其他项目添加分组:

select DISTINCT movie_master.MovieMasterID, max(MOVIE_VERSION.ReleaseDate),
movie_master.displayorder, movie_classification.mc_code,
movie_master.title As Title,
movie_master.BioCast, movie_master.BioDirector, movie_master.LinkWord,
CASE WHEN ((DATEDIFF(day,max(MOVIE_VERSION.ReleaseDate),getdate())  <=7)
and (DATEDIFF(day,max(MOVIE_VERSION.ReleaseDate),getdate()) >=0))
THEN 'JUST RELEASED' ELSE '' END AS RELEASETAG,
movie_master.Synopsis,movie_language.NAME as Language,
CASE WHEN movie_master.DistributorID = 8 THEN 0 ELSE 9999 END AS distributororder,
movie_distributor.name as distributor
...
group by movie_master.MovieMasterID, movie_master.displayorder, movie_classification.mc_code,
movie_master.title, movie_master.BioCast, movie_master.BioDirector,
movie_master.LinkWord, movie_master.Synopsis,
movie_language.NAME, movie_master.DistributorID, movie_distributor.name
order by MOVIE_MASTER.displayorder, max(MOVIE_VERSION.ReleaseDate) desc, distributororder, MOVIE_MASTER.TITLE

答案 1 :(得分:0)

我想到的第一件事是:


    SELECT 
    MovieMasterID, 
    MAX(ReleaseDate) as ReleaseDate, 
    displayorder,
    mc_code,
    Title,
    BioCast,
    BioDirector,
    LinkWord,
    RELEASETAG,
    Synopsis,
    Language,
    distributororder,
    distributor
    FROM(
    select  DISTINCT
        movie_master.MovieMasterID, 
        MOVIE_VERSION.ReleaseDate, 
        movie_master.displayorder,
        movie_classification.mc_code,
        movie_master.title As Title,
        movie_master.BioCast,
        movie_master.BioDirector,
        movie_master.LinkWord,
        CASE 
        WHEN ((DATEDIFF(day,MOVIE_VERSION.ReleaseDate,getdate())  =0)) 
                THEN 'JUST RELEASED' 
            ELSE '' 
        END AS RELEASETAG,
        movie_master.Synopsis,
        movie_language.NAME as Language,
        CASE 
            WHEN movie_master.DistributorID = 8 THEN 0 
            ELSE 9999 
        END AS distributororder,
        movie_distributor.name as distributor
    from movie_master 
    left join MOVIE_LANGUAGE 
        on MOVIE_MASTER.Language = movie_language.languageid
    left join movie_distributor 
        on MOVIE_MASTER.distributorid = movie_distributor.distributorid
    left join movie_version 
        on MOVIE_MASTER.MovieMasterID = MOVIE_VERSION.MovieMasterID
    left join movie_classification 
        on MOVIE_MASTER.classification = movie_classification.classificationid
    left join movie_itemdetails 
        on MOVIE_MASTER.MovieMasterID = movie_itemdetails.MovieMasterID 
    where MOVIE_VERSION.ReleaseDate  6
        and MOVIE_VERSION.Enabled = 1
        AND MOVIE_MASTER.Enabled = 1
        AND (MOVIE_MASTER.MovieMasterID in (SELECT DISTINCT MOVIE_MASTER.MovieMasterID FROM MOVIE_MASTER))

    ) T
    GROUP BY MovieMasterID,displayorder,mc_code,Title,BioCast,BioDirector,LinkWord,RELEASETAG,Synopsis,Language,distributororder,distributor
order by displayorder, ReleaseDate desc, distributororder, TITLE

答案 2 :(得分:0)

尝试此查询

select DISTINCT movie_master.MovieMasterID, 
                m.ReleaseDate, 
                movie_master.displayorder,movie_classification.mc_code,
                movie_master.title As Title,
                movie_master.BioCast,movie_master.BioDirector,movie_master.LinkWord,
CASE WHEN ((DATEDIFF(day,m.ReleaseDate,getdate())  <=7) and (DATEDIFF(day,m.ReleaseDate,getdate()) >=0)) THEN 'JUST RELEASED' ELSE '' END AS RELEASETAG,
movie_master.Synopsis,movie_language.NAME as Language,
CASE WHEN movie_master.DistributorID = 8 THEN 0 ELSE 9999 END AS distributororder,
movie_distributor.name as distributor
from movie_master 
left join MOVIE_LANGUAGE on MOVIE_MASTER.Language = movie_language.languageid
left join movie_distributor on MOVIE_MASTER.distributorid = movie_distributor.distributorid
left join movie_version m on MOVIE_MASTER.MovieMasterID = m.MovieMasterID
left join movie_classification on MOVIE_MASTER.classification = movie_classification.classificationid
left join movie_itemdetails on MOVIE_MASTER.MovieMasterID = movie_itemdetails.MovieMasterID 
where m.ReleaseDate <= convert(varchar, GETDATE(), 101) 
and m.MovieMasterID = movie_master.MovieMasterID
and movie_master.classification = movie_classification.classificationid
and movie_master.MovieMasterID = movie_itemdetails.MovieMasterID and movie_itemdetails.fieldid = 1 and movie_itemdetails.Type ='TRAILER'
and ((m.FilmCode in (SELECT DISTINCT FILMID FROM MOVIE_TEMP)) OR (m.Title in (SELECT DISTINCT TITLE FROM MOVIE_TEMP)))  
AND LEFT(m.FILMCODE,1) <> 6
and m.Enabled = 1
AND MOVIE_MASTER.Enabled = 1
AND (MOVIE_MASTER.MovieMasterID in (SELECT DISTINCT MOVIE_MASTER.MovieMasterID FROM MOVIE_MASTER))
AND EXISTS (
            SELECT 1
            FROM movie_version m2
            WHERE MOVIE_MASTER.MovieMasterID = m2.MovieMasterID
            HAVING MAX(m2.ReleaseDate) = m.ReleaseDate
            )            
order by MOVIE_MASTER.displayorder, m.ReleaseDate desc, distributororder, MOVIE_MASTER.TITLE