SQL不对小写和大写电子邮件地址进行分组

时间:2017-12-11 02:01:49

标签: sql-server-2008

我正在进行SQL查询,其中查询不将大写和小写电子邮件地址分组。虽然sql不区分大小写但我不明白为什么会这样做。

$('#next-button').on('click', function() {
    $('.mdl-tabs__tab.is-active').next().find('span').click();
  });

$('#prev-button').on('click', function() {
   $('.mdl-tabs__tab.is-active').prev().find('span').click();
});

我得到了以下结果。 结果显示:

SELECT
Customers.EmailAddress,
o.TotalOrders AS 'overall NumOrders',
o.TotalOrdered AS 'overall TotalOrdered',
o1.TotalOrders AS '2017 NumOrders',
o1.TotalOrdered AS '2017 TotalOrdered',
o2.TotalOrders AS '2016 NumOrders',
o2.TotalOrdered AS '2016 TotalOrdered'
FROM Customers 
JOIN Orders
ON Customers.Customerid=Orders.Customerid
FULL  JOIN
(
SELECT DISTINCT Customers.EmailAddress,
COUNT(Orders.OrderID) as TotalOrders,
(SUM(Orders.PaymentAmount)) as TotalOrdered
FROM
Customers  WITH (NOLOCK),Orders  WITH (NOLOCK)
WHERE
 Customers.CustomerID = Orders.CustomerID 
AND Orders.OrderStatus NOT IN ('Cancelled','Payment Declined')
AND Orders.OrderDate BETWEEN '01/01/2016 00:00' AND '11/30/2017 23:59'
GROUP BY
Customers.EmailAddress
 ) AS o ON o.EmailAddress = Customers.EmailAddress
FULL  JOIN
(
SELECT DISTINCT
Customers.EmailAddress,
COUNT(Orders.OrderID) as TotalOrders,
SUM(Orders.PaymentAmount) as TotalOrdered
FROM
Orders  WITH (NOLOCK), Customers  WITH (NOLOCK)
WHERE
Orders.CustomerID = Customers.CustomerID
AND Orders.OrderStatus NOT IN ('Cancelled','Payment Declined')
AND Orders.OrderDate BETWEEN '01/01/2017 00:00' AND '11/30/2017 23:59'
GROUP BY
Customers.EmailAddress
) AS o1 ON o1.EmailAddress = Customers.EmailAddress
FULL JOIN
(
SELECT DISTINCT
Customers.EmailAddress,
COUNT(Orders.OrderID) as TotalOrders,
SUM(Orders.PaymentAmount) as TotalOrdered
FROM
Orders  WITH (NOLOCK), Customers  WITH (NOLOCK)
WHERE
Orders.CustomerID = Customers.CustomerID
AND Orders.OrderStatus NOT IN ('Cancelled','Payment Declined')
AND Orders.OrderDate BETWEEN '01/01/2016 00:00' AND '12/31/2016 23:59'
GROUP BY
Customers.EmailAddress
) AS o2 ON o2.EmailAddress = Customers.EmailAddress
WHERE Orders.Orderdate BETWEEN '1/1/2016 00:00' AND '11/30/2017 23:59'
 AND  Orders.OrderStatus NOT IN('Cancelled','Payment Declined')
GROUP BY
Customers.EmailAddress,
o.TotalOrders ,
o.TotalOrdered ,
o1.TotalOrders ,
o1.TotalOrdered ,
o2.TotalOrders ,
o2.TotalOrdered ,
o3.TotalOrders , 
o3.TotalOrdered 

我想将这两个电子邮件合并为一个:    emailaddress overallnumorders ..........    smithworks@gmail.com 4 ......................................

1 个答案:

答案 0 :(得分:0)

2个电子邮件地址的区别不仅仅是大小写(也有2个空格 SMITHWORKS @ GMAIL.COM <> smithworks@gmail.com所以他们将是&#34; distinct&#34;因此被视为2行。

您可以在select和group by子句中尝试lower(Customers.EmailAddress)。此外,通过使用&#34;条件聚合&#34;可以简化现有查询。 (在聚合函数中放置一个case表达式):

SELECT
      lower(Customers.EmailAddress)
    , COUNT(Orders.OrderID)   AS 'overall NumOrders'
    , SUM(Orders.PaymentAmount)  AS 'overall TotalOrdered'
    , COUNT(case when Orders.OrderDate >= '20170101' then Orders.OrderID       end) AS '2017 NumOrders'
    , SUM(  case when Orders.OrderDate >= '20170101' then Orders.PaymentAmount end) AS '2017 TotalOrdered'
    , COUNT(case when Orders.OrderDate <  '20170101' then Orders.OrderID       end) AS '2016 NumOrders'
    , SUM(  case when Orders.OrderDate <  '20170101' then Orders.PaymentAmount end) AS '2016 TotalOrdered'
    , o2.TotalOrders  AS '2016 NumOrders'
    , o2.TotalOrdered AS '2016 TotalOrdered'
FROM Customers
JOIN Orders ON Customers.Customerid = Orders.Customerid
WHERE Orders.OrderStatus NOT IN ('Cancelled', 'Payment Declined')
AND Orders.OrderDate >= '20160101' AND Orders.OrderDate < '20171201'
GROUP BY
      lower(Customers.EmailAddress)

注意:

  • 在SQL Server中,MM / dd / yyyy中最安全的日期文字是 NOT 在T-SQL中指定日期的最安全方式是 YYYYMMDD (没有分隔符)
  • AND '11/30/2017 23:59' NOT 是定义终点的好方法,它比12月1日短一分钟。 停止在之间使用日期范围要好得多。 &#34;少于2017年12月1日&#34;是定义截止点的最准确方法。因此,请使用此回答中显示的涉及>=<
  • 的样式,而不是介于两者之间
  • &#39;选择不同的&#39;在单个查询中执行group by时不需要。 (根据定义,group by子句会生成唯一的行,因此select distinct只是浪费时间。分组也在选择线索之前完成。)
  • FULL JOIN非常昂贵,而且不经常使用。现有查询中不需要它,因为所有数据都来自完全相同的表,因此子查询中不会有不匹配的行。