我有许多客户的Microsoft SQL数据库,列为 CustomerID,CustomerName,Locations,SQFT,EditDate, DateRecorded
每天,我们记录客户使用的平方英尺。因此,数据库应具有每月的每一天的值。我想为每个客户选择使用平方英尺最多的日期。下面的代码仅返回一个客户,而不是所有客户。如何为每个客户返回当月最高的SQFT?
我的代码:
// $db_month_start = the first of the current month.
// $db_month_end = the end of the current month.
$query = "SELECT CustomerID,CustomerName,SQFT,EditDate
FROM SquareFootage
WHERE DateRecorded >= '{$db_month_start}'
AND DateRecorded <= '{$db_month_end}'
AND SQFT = (Select Max(SQFT) From SquareFootage WHERE DateRecorded >= '{$db_month_start}' AND DateRecorded <= '{$db_month_end}') ";
每个客户每月的最大SQFT:
select CustomerID
, CustomerName
, convert(varchar(7), DateRecorded, 120) as [Month]
, max(SQFT) as MaxSQFTThisMonth
from SquareFootage
group by
CustomerID
, CustomerName
, convert(varchar(7), DateRecorded, 120)
它可以工作数月,并且连续几天获得最高SQFT的客户仅在一行中列出。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句