算在foxpro有条件

时间:2010-09-21 03:54:13

标签: 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

3 个答案:

答案 0 :(得分:1)

首先,获取它出现的名称和最早年份:

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

答案 1 :(得分:1)

我可能正在解决另一个问题但是这段代码每年都会计算前几年没有出现过的名字:

*-- 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

答案 2 :(得分:0)

* /首先,在他们进行交易的第一年,以每个名字为基础 * / for ...除了此人所拥有的总交易额外 * / of the year .. ex:你的“a”和“c”人的两个重叠

SELECT ;
        YT.Name,;
        MIN( YEAR( YT.DATE )) as FirstYear,;
        COUNT(*) as TotalPerName;
    FROM ;
        YourTable YT;
    GROUP BY ;
        1;
    INTO ;
        CURSOR C_ByNameTotals

* /现在您的总数基于每人的第一年总数 * /条目无论年份,获得年份和总数的总和 * /给定年份的参赛作品....那么所有原创年份的UNION UNION * /不在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 )