查询日期和状态之间的计数 [英] Query count between dates and status
问题描述
我有一个看起来像这样的产品表:
ProductId ( pKey ) < span class ="pln"style ="margin:0px;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336"> ProductTypeId
1 3
2 3
3 2
4 1
5 1
每次插入此表时,它都会销售新产品。
我只有3 ProductTypeId
那个
实际上代表了我的客户可以取消和重新计算或处于暂停状态的服务器,这使我们了解了状态产品表看起来像这样:
StatusId ( pKey ) < span class ="pln"style ="margin:0px;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336"> ProductId StatusDate ProductStatus
1 , 1 , 1-1-17 , 1
2 , 1 , 2-12-17 , 2
3 , 2 , 21-5-17 , 1
4 , 3 , 11-5-18 , 1
5 , 2 , 18-5-18 , 2
6 , 2 , 10-8-18 , 3
我只有3种类型的状态("ProductStatus"列)1,2,3。
我的目标是每个月计算多少 ProducsType
在状态1中为
。
列 StatusDate
代表
日期状态发生变化,因此客户第一次购买产品/服务时他/她将拥有 < code style ="margin:0px;填充:1px 5px;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;字体大小:13像素;垂直对齐:基线;背景色:#eff0f1; white-space:预包装"> ProductTypeId (1,2
或3)永远不会改变, ProductStatus
1但可以在将来的任何时间更改。
查询结果看起来应该是这样的:
年份productId ( 1 ) < span class ="pln"style ="margin:0px;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336"> productId ( 2 ) < span class ="pln"style ="margin:0px;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336"> productId ( 3 ) < span class ="pln"style ="margin:0px;填充:0像素;边框:0像素;字体风格:继承;字体变:继承;字体重量:继承;行高:继承; FONT-FAMILY:继承;垂直对齐:基线;颜色:#303336">
2017 1 0 0 1
2017 2 0 0 1
2017 3 0 0 1
2017 4 0 0 1
2017 5 0 0 2
2017 6 0 0 2
等......
嗨nd0911,
根据你的描述,我担心我一团糟。这显然不符合你的逻辑。
你能更清楚地解释一下吗?
在您的查询结果中,2017年productId(3)的值为1和2.然而,在您的原始表格中,您没有提及。您能否分享我们已完成的预期结果或原始文件数据?
最好的问候,
拉结
I have a products table that looks like that:
ProductId(pKey) ProductTypeId
1 3
2 3
3 2
4 1
5 1
every time a row inserted to this table it mains that a new product was sold.
I have only 3 ProductTypeId
that
which actually represents a servers that my customer can cancel and recancel or be on hold status, and that leads us to the history of status products table that looks like that:
StatusId(pKey) ProductId StatusDate ProductStatus
1 ,1 ,1-1-17 ,1
2 ,1 ,2-12-17 ,2
3 ,2 ,21-5-17 ,1
4 ,3 ,11-5-18 ,1
5 ,2 ,18-5-18 ,2
6 ,2 ,10-8-18 ,3
I have only 3 types of status ("ProductStatus" column) 1,2,3.
my goal is to count every month how many ProducsType
were
in status 1.
the column StatusDate
represents
the date were the status changed, so the first time a customer buy the product/serves he/she will have a ProductTypeId
(1,2
or 3) that will never change, and ProductStatus
of
1 but can change any time in the future.
the query results should look something like that:
year month productId(1) productId(2) productId(3)
2017 1 0 0 1
2017 2 0 0 1
2017 3 0 0 1
2017 4 0 0 1
2017 5 0 0 2
2017 6 0 0 2
and so on....
Hi nd0911,
Per your description , I am afraid that I am in a mess. It is not clearly for your logic. Could you please explain more clearly?
In your query result , in 2017 the value of productId(3) is 1 and 2 . However, in your original table ,you didn't mention. Could you please share us completed expected result or original data?
Best Regards,
Rachel
这篇关于查询日期和状态之间的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!