查看SQL代码以进行验证

时间:2014-02-25 01:01:30

标签: sql sql-server tsql

我有两个看似相同的查询。我们今天试图获得季度。一个使用硬编码日期,一个不使用。一个查询返回2100条记录。第一个和最后一个记录是相同的。 2月的所有比赛都在2月和1月的最后5天比赛。

使用硬编码日期查询1.

SELECT c.Account_RecordType
       ,timewait
       ,a.convotime
       ,DATENAME(Month, timewait) AS 'Mnth'
       ,DATENAME(year, timewait) AS 'yr'
       ,DATENAME(quarter, timewait) AS 'qrt'
    FROM SalesForce.dbo.SalesForceContact AS b
    INNER JOIN Dossier_Replication.dbo.vwSF_DATA_Contact c
        ON b.ContactID = c.CONTACTID__C
    RIGHT OUTER JOIN satVRS.dbo.rptNECACallHistory AS a
        ON b.UserID = a.UserID_Caller
    WHERE ( b.Platform = 'HandsonVRS' )
        AND ( a.timeWait BETWEEN '2014-01-01' AND '2014-02-24' )
        AND ( a.isReport = '1' )
        AND ( a.NECA_isReport = '1' )
        AND ( a.ConvoTime > '0' )
        AND ( c.Account_RecordType = 'Enterprise Account' )
    GROUP BY c.Account_RecordType
       ,timewait
       ,a.convotime

第二个查询是从相关日期字段中提取季度,年和日。

 SELECT c.Account_RecordType
       ,timewait
       ,a.convotime
       ,DATENAME(Month, timewait) AS 'Mnth'
       ,DATENAME(year, timewait) AS 'yr'
       ,DATENAME(quarter, timewait) AS 'qrt'
    FROM SalesForce.dbo.SalesForceContact AS b
    INNER JOIN Dossier_Replication.dbo.vwSF_DATA_Contact c
        ON b.ContactID = c.CONTACTID__C
    RIGHT OUTER JOIN satVRS.dbo.rptNECACallHistory AS a
        ON b.UserID = a.UserID_Caller
    WHERE ( b.Platform = 'HandsonVRS' )
        AND ( a.timeWait BETWEEN '2014-01-01' AND '2014-02-24' )
        AND ( a.isReport = '1' )
        AND ( a.NECA_isReport = '1' )
        AND ( a.ConvoTime > '0' )
        AND ( c.Account_RecordType = 'Enterprise Account' )
    GROUP BY c.Account_RecordType
       ,timewait
       ,a.convotime

有没有人看到这两个明显错误的东西。有任何建议,我提前谢谢你。我们正在运行sql 2012

1 个答案:

答案 0 :(得分:0)

抱歉我的不好,这是第二个...

选择     c.Account_RecordType,timewait,a.convotime,(datename(year,timewait))为twy,(datename(quarter,timewait))为twq

FROM SalesForce.dbo.SalesForceContact AS b INNER JOIN       Dossier_Replication.dbo.vwSF_DATA_Contact c ON b.ContactID = c.CONTACTID__C RIGHT OUTER JOIN       satVRS.dbo.rptNECACallHistory AS a ON b.UserID = a.UserID_Caller

WHERE(b.Platform ='HandsonVRS')                                   AND(datename(year,timewait))=(datename(year,getdate() - 1))AND(datename(quarter,timewait))=(datename(quarter,getdate() - 1))                                   AND(a.isReport ='1')                                   AND(a.NECA_isReport ='1')                                   AND(a.ConvoTime>'0')                                   AND(c.Account_RecordType ='企业帐户') 按c.Account_RecordType分组,timewait,a.convotime 按时间顺序