我有一张这样的表:
year | revenue | organisation
2010 | 83863 | asdf
2011 | 5463 | asdf
2012 | 45345 | asdf
2009 | 32463 | ghjk
2010 | 352667 | ghjk
我需要搜索所有组织的最新收入,到目前为止,我可以构建一个像
这样的查询Select new tablename(v.organisation, MAX(v.year), v.revenue)
from tablename v
where 1=1 group by v.name
但是我还需要另外一件事,那就是收入与上一年相比的变化百分比。
我怎么能做这两个功能?最好是在一个查询中?
由于
答案 0 :(得分:2)
试试这个:
let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController;
let favoritesViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController;
子选择获得上一年的收入数字,然后执行以下公式:
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
// how to send page to ContentViewController
//delegate.pageChanged(page)
}
哪个应该给出百分比差异。我用你的假日期在我的环境中测试了它。
结果:
protocol PageChangedDelegate {
func pageChanged(page: Int)
}
var delegate: PageChangedDelegate!
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let page = Int(scrollView.contentOffset.x / scrollView.frame.size.width)
delegate.pageChanged(page) // delegate is nil here
}
显示当前收入和上一年收入的结果:
SELECT v.organization,
MAX(v.year) AS currentYear,
v.revenue,
((v.revenue -
(SELECT i.revenue
FROM tablename i
WHERE i.organization = v.organization
AND i.year = MAX(v.year) - 1)) /
(SELECT i.revenue
FROM tablename i
WHERE i.organization = v.organization
AND i.year = MAX(v.year) - 1) * 100) AS percentDifference
FROM tablename v
WHERE 1 = 1
GROUP BY v.organization,
v.year;
答案 1 :(得分:1)
这是单程......
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(year INT NOT NULL
,revenue INT NOT NULL
,organisation VARCHAR(12) NOT NULL
,PRIMARY KEY(year,organisation)
);
INSERT INTO my_table VALUES
(2010 ,83863 ,'asdf'),
(2011 ,5463 ,'asdf'),
(2012 ,45345 ,'asdf'),
(2009 ,32463 ,'ghjk'),
(2010 ,352667 ,'ghjk');
SELECT x.*
, ((x.revenue - z.revenue)/z.revenue) * 100 pct_diff
FROM my_table x
JOIN
( SELECT organisation
, MAX(year) max_year
FROM my_table
GROUP
BY organisation
) y
ON y.organisation = x.organisation
AND y.max_year = x.year
LEFT
JOIN my_table z
ON z.organisation = x.organisation
AND z.year = x.year - 1;
+------+---------+--------------+----------+
| year | revenue | organisation | pct_diff |
+------+---------+--------------+----------+
| 2010 | 352667 | ghjk | 986.3660 |
| 2012 | 45345 | asdf | 730.0384 |
+------+---------+--------------+----------+
这是运行历史的一个(慢)版本......
SELECT x.*
, ((x.revenue - y.revenue)/y.revenue) * 100 pct_diff
FROM my_table x
LEFT
JOIN my_table y
ON y.organisation = x.organisation
AND y.year = x.year - 1
ORDER
BY organisation
, year;
......如果花一点时间(/记得如何),我会发布更快的替代方案