在foxpro中计数有条件

发布于 2024-09-24 09:48:01 字数 290 浏览 8 评论 0原文

我有一个表,

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

i have a table follow

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

i want count database.
with table below with result follow:

2003 = 2
2004 = 1
2007 = 0 because c has in 2004-year
2008 = 0 because a has in 2003-year

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2024-10-01 09:48:01

首先,获取名称及其出现的最早年份:

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

First, get the name and the earliest year it appears in:

select name, min(year(date)) as year from table
group by name into cursor temp

Then get the count from within that year:

select count(table.name) 
from table join temp on table.name = temp.name 
and year(table.date) = temp.year
月寒剑心 2024-10-01 09:48:01

我可能正在解决一个不同的问题,但此代码给出了每年未出现在前几年的名称的计数:

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

I may be solving a different problem but this code gives the count in each year for names that have not appeared in previous years:

*-- 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
苍景流年 2024-10-01 09:48:01

*/ 首先,在他们进行交易的第一年按姓名计算
*/ 对于...除了此人拥有的总交易量之外
*/ 年度.. 例如:“a”和“c”人的两个重叠

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

*/ 现在您已经根据每个人的第一年及其总数得出了总计
*/ 不管年份的条目,获取年份和 HAVE 的总数
*/ 给定年份的条目....然后对所有原始年份的可能性进行联合
*/ 不在 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 )

*/ First, get on a per name basis the first year they have a transaction
*/ for... in addition to the total transactions this person has regardless
*/ of the year.. ex: your two overlap of "a" and "c" persons

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

*/ Now that you have totals based on the first year per person with their total
*/ entries regardless of the year, get the year and sum of totals that HAVE
*/ entries for a given year.... THEN UNION for all original year possibilities
*/ that were NOT IN the C_ByNameTotals result set. (hence your 2007 and 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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文