根据一个日期月份的值对多个表进行多次添加

时间:2019-05-16 05:15:19

标签: sql-server

我正在尝试从下面的代码向我的第二段代码添加3个过滤器 因此我得到按状态ID <>以及地区和类型过滤的年份月份计数。我是SQL的新手,只是无法找到一种方法来合并两个代码以使其正常工作。我花了5个小时试图找到解决方案。任何帮助将非常感激 我需要从jd.OrganisationAddress创建的日期计数,但是要过滤它,我需要将jd.OrganisationAddress的OrganisationID字段链接到jd.Organisation表的字段ID,该表的字段StatusID,我需要为<> 4 。然后对于类型字段,我必须匹配jd.OrganisationOrganisationType表中的jd.OrganisationOrganisationType.OrganisationID,我需要通过jd.OrganisationOrganisationType.OrganisationTypeID = jd.OrganisationType.ID进行链接,然后再从jd.OrganisationType表中进行匹配.OrganisationType.Name)='Bank'));然后最后对于该区域,我必须通过jd.OrganisationAddress.AddressID = jd.Address.ID将其链接到地址表,然后从地址表中,我需要通过将jdAddress.countryID链接到JD.country_region.countryID然后是jd来匹配该区域country.region.regionID到jd_region表字段ID,然后如果jd_Region.name =“ North America”

SELECT
    jd.Organisation.StatusID
  , jd.Region.Name AS Region
  , jd.OrganisationType.Name AS Type
FROM
   jd.Region
  , ((((jd.Organisation 
INNER JOIN
   jd.OrganisationAddress 
ON
   jd.Organisation.ID = jd.OrganisationAddress.OrganisationID) 
INNER JOIN
   jd.Address 
ON
   jd.OrganisationAddress.AddressID = jd.Address.ID) 
INNER JOIN
   jd.Country 
ON
   jd.Address.CountryID = jd.Country.ID) 
INNER JOIN
   jd.OrganisationOrganisationType 
ON
   jd.Organisation.ID = jd.OrganisationOrganisationType.OrganisationID) 
INNER JOIN
   jd.OrganisationType 
ON
   jd.OrganisationOrganisationType.OrganisationTypeID = jd.OrganisationType.ID
WHERE
   (((jd.Organisation.StatusID)<>4) 
AND
   ((jd.Region.Name)='North America') 
AND
   ((jd.OrganisationType.Name)='Bank'));


select year(datecreated), 
   sum(case when month(datecreated) = 1 then 1 else 0 end) Jan,
   sum(case when month(datecreated) = 2 then 1 else 0 end) Feb,
   sum(case when month(datecreated) = 3 then 1 else 0 end) Mar,
   sum(case when month(datecreated) = 4 then 1 else 0 end) Apr,
   sum(case when month(datecreated) = 5 then 1 else 0 end) May,
   sum(case when month(datecreated) = 6 then 1 else 0 end) Jun,
   sum(case when month(datecreated) = 7 then 1 else 0 end) Jul,
   sum(case when month(datecreated) = 8 then 1 else 0 end) Aug,
   sum(case when month(datecreated) = 9 then 1 else 0 end) Sep,
   sum(case when month(datecreated) = 10 then 1 else 0 end) Oct,
   sum(case when month(datecreated) = 11 then 1 else 0 end) Nov,
   sum(case when month(datecreated) = 12 then 1 else 0 end) Dec
   from jd.OrganisationAddress
   group by year(datecreated)
   order 

按年份(创建日期)ASC

2 个答案:

答案 0 :(得分:0)

您的示例对我来说仍然有些陌生,但我认为您喜欢实现以下目标:

(基于AdventureWorks2014数据库)

SELECT 
  DATEPART(YEAR, ModifiedDate) AS [Year], 
  DATEPART(MONTH, ModifiedDate) AS [Month], 
  SUM(LineTotal) AS TurnOver 
FROM [Sales].[SalesOrderDetail]
GROUP BY DATEPART(YEAR, ModifiedDate), DATEPART(MONTH, ModifiedDate)

答案 1 :(得分:0)

我尝试了一些尝试,以了解您可能想要达到的目标,并为您提供另一个答案(抱歉,这有点像进入水晶球):

WITH PreSelection AS (
  SELECT 
    oa.datecreated
  FROM OrganisationAddress oa
  INNER JOIN Organisation o ON o.ID = oa.OrganisationID
  INNER JOIN [Address] a ON oa.AddressID = a.ID
  INNER JOIN Country c ON a.CountryID = c.ID
  INNER JOIN [Region] r ON a.RegionID = r.ID
  -- INNER JOIN [Region] r ON c.RegionID = r.ID
  INNER JOIN OrganisationType ot ON o.OrganisationTypeID = ot.ID
  WHERE o.StatusID <> 4
    AND r.Name = 'North America'
    AND ot.Name ='Bank'
)
SELECT 
  DATEPART(YEAR, datecreated) AS [Year], 
  DATEPART(MONTH, datecreated) AS [Month], 
  COUNT(*) AS RecordCount 
FROM PreSelection 
GROUP BY 
  DATEPART(YEAR, datecreated), 
  DATEPART(MONTH, datecreated);