我有一個表遵循計數有狀態的FoxPro
id name date
1 a 08/09/2003
2 b 02/03/2003
3 c 10/08/2004
4 c 25/08/2007
5 a 01/01/2008
我想指望數據庫。 與見下表與結果如下:
2003 = 2
2004 = 1
2007 = 0 because c has in 2004-year
2008 = 0 because a has in 2003-year
我有一個表遵循計數有狀態的FoxPro
id name date
1 a 08/09/2003
2 b 02/03/2003
3 c 10/08/2004
4 c 25/08/2007
5 a 01/01/2008
我想指望數據庫。 與見下表與結果如下:
2003 = 2
2004 = 1
2007 = 0 because c has in 2004-year
2008 = 0 because a has in 2003-year
首先,獲取名稱和它出現在最早的一年:
select name, min(year(date)) as year from table
group by name into cursor temp
然後從一年之內獲得數:
select count(table.name)
from table join temp on table.name = temp.name
and year(table.date) = temp.year
我可能正在解決一個不同的問題,但是這個代碼給出了在往年沒有出現的名字的每年的計數:
*-- Get the firstyear for each name
Select Name, Min(Year(Date)) As firstyear ;
From table1;
Group By Name Into Cursor temp1
*-- Get the year of the date for each entry
Select Id, Name, Year(Date) As yr From table1 Into Cursor temp2
*-- Identify those rows that appear for the first time
Select temp2.*, temp1.firstyear, Iif(temp2.yr = temp1.firstyear, 1, 0) As countme ;
FROM temp2 INNER Join temp1 ;
ON temp2.Name = temp1.Name Into Cursor temp3
*-- Add up the "CountMe" fields to get the sum.
Select yr, Sum(countme) From temp3 Group By yr
* /第一,得到每名基礎的第一年,他們有一個交易 */...爲除了總交易此人無論 年..恩* /的:您的「一」和「c」的人兩個重疊
SELECT ;
YT.Name,;
MIN(YEAR(YT.DATE)) as FirstYear,;
COUNT(*) as TotalPerName;
FROM ;
YourTable YT;
GROUP BY ;
1;
INTO ;
CURSOR C_ByNameTotals
* /現在你已經根據每個人的第一年,其總 * /條目無論一年的總和,獲得一年總和總計有一個給定年份的 * /條目....然後所有原始年份可能性的聯盟 * /不在C_ByNameTotals中集合。 (因此你的2007年和2008年)
SELECT;
FirstYear as FinalYear,;
SUM(TotalPerName) as YrCount;
FROM ;
C_ByNameTotals;
GROUP BY ;
1;
INTO ;
CURSOR C_FinalResults;
UNION;
SELECT DISTINCT;
YEAR(Date) as FinalYear,;
0 as YrCount;
FROM ;
YourTable ;
WHERE ;
YEAR(Date) NOT IN ;
(select FirstYear FROM C_ByNameTotals)
非常感謝你。 – maolddv 2010-09-21 10:01:39