osclass Counting number of Sales of specifed id products

无人久伴 提交于 2020-01-06 17:39:27

问题


I have this table item_log:


fk_i_item_id product amount currency status


 $sales = $conn->osc_dbFetchResult("SELECT COUNT(status) FROM %st_item_log WHERE  fk_i_item_id = '%s' AND status = 'COMPLETED'", DB_TABLE_PREFIX,osc_logged_user_id());  

 echo $sales['COUNT(status)']           

I want to count total number of sales of an product in the same echo.

ex

***********************************************************
fk_i_item_id      product     amount   currency    status
***********************************************************
20                Book 1       10          USD     COMPLETED
21                Book 2       12          USD     COMPLETED
20                Book 1       10          USD     COMPLETED
23                Book 3       11          USD     COMPLETED

I want an query to count the number or records.

*******************************************************************
fk_i_item_id      product     amount   currency    status     Sales
*******************************************************************
20                Book 1       10          USD     COMPLETED    2
21                Book 2       12          USD     COMPLETED    1
23                Book 3       11          USD     COMPLETED    1

回答1:


You already appear to know how to use aggregate functions such as count(), albeit over the entire table.

It's also possible to use these aggregate functions over groups of rows within the table. For example, to get the number of rows for each item ID, it's a simple matter of:

select   item_id,
         count(*)
from     some_table
group by item_id

So all you really need to do is use group by to specify a finer level of grouping since, without that, it will group over all the selected rows.

With regard to your desired output, it would probably make more sense to sum the amount (so that id 20 has an amount of 20 rather than 10) though this may well be complicated by the fact you appear to allow multiple currencies.

I'm not going to tell you how to fix that other than to state that you may need to convert the amounts to a baseline currency before summing them.

So, as a first cut, I'd be looking at something like:

select   fk_i_item_id,
         max(product)   as product,
         sum(amount)    as amount,
         max(currency)  as currency,
         max(status)    as status,
         count(*)       as sales
from     st_item_log
where    status = 'COMPLETED'
group by fk_i_item_id

Note the use of the "dummy" max aggregate function on the fields you're not grouping by. This is to ensure that the rows still group on the item ID even if they have different product descriptions, currencies, or status values (though the last is impossible given the where clause).



来源:https://stackoverflow.com/questions/35149792/osclass-counting-number-of-sales-of-specifed-id-products

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!