使用非标准 Group by in 子句进行选择
抱歉,如果我重新发明轮子(以及我的英语),也许用普通的查询是不可能的,但我有“无意义的想法”,可以将行块移动到结果集顶部,我希望这个 SQL 转储(MySql)为了更好地理解,我将其转换为帐户及其货币,也许这些会更好地说明我尝试的内容,实际上是子句 Group by 和 Order by 不是我的 java
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`idAccount` int(10) unsigned NOT NULL AUTO_INCREMENT,
`idCustomer` int(10) unsigned DEFAULT NULL,
`idCustomerNumber` varchar(6) DEFAULT NULL,
`AccountNumber` varchar(12) DEFAULT NULL,
`Currency` varchar(3) DEFAULT NULL,
PRIMARY KEY (`idAccount`)
)
INSERT INTO `account` (`idAccount`,`idCustomer`,`idCustomerNumber`,`AccountNumber`,`Currency`) VALUES
(1,1,'000001','000001JPY101','JPY'),
(2,1,'100002','100002GBP101','GBP'),
(3,2,'003333','003333EUR201','EUR'),
(4,1,'100002','100002GBP102','GBP'),
(5,1,'000001','000001EUR101','EUR'),
(6,1,'000001','000001USD101','USD'),
(7,1,'000001','000001USD102','USD'),
(8,1,'100002','100002EUR101','EUR'),
(9,1,'100002','100002USD101','USD'),
(10,2,'003333','003333EUR202','EUR'),
(11,2,'003333','003333JPY201','JPY');
(Select * From AccountWhere CustomerNumber = '000001'
)
编辑:现在我希望会更好...
我如何使用按货币分组和按帐户编号排序的表创建选择,并可以创建以美元货币的帐户编号开头的结果集,其余行将按货币字母顺序分组,并按 AccountNumber 排序
,以提取按 USD - EUR - JPY 顺序排序的行
编辑 2:
我的错,抱歉,真的是时候开始学习主要语言
了如果我调用 SELECT * FROM AccountWhere idCustomerNumber = '000001' Order bycurrency, AccountNumber 然后 SQL 解释器返回,
5,1,"000001","000001EUR101","EUR"
1,1,"000001","000001JPY101","JPY"
6,1,"000001","000001USD101","USD"
7,1,"000001","000001USD102","USD"
但现在我想以编程方式确定结果集以美元帐户开头,帐户的其余部分将按货币分组然后按帐号订购
sorry if I reinventing the wheel (and for my English too), and maybe is not possible with plain vanilla query but I have "nonsence Idea" that is possible move block of Rows to Resulsets top, I hope that this SQL dump (MySql) and for better undestood I converted that to the Account and its Currency, maybe these would be better to ilustrate what I trying, really clause Group by with Order by not my cup of java
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`idAccount` int(10) unsigned NOT NULL AUTO_INCREMENT,
`idCustomer` int(10) unsigned DEFAULT NULL,
`idCustomerNumber` varchar(6) DEFAULT NULL,
`AccountNumber` varchar(12) DEFAULT NULL,
`Currency` varchar(3) DEFAULT NULL,
PRIMARY KEY (`idAccount`)
)
INSERT INTO `account` (`idAccount`,`idCustomer`,`idCustomerNumber`,`AccountNumber`,`Currency`) VALUES
(1,1,'000001','000001JPY101','JPY'),
(2,1,'100002','100002GBP101','GBP'),
(3,2,'003333','003333EUR201','EUR'),
(4,1,'100002','100002GBP102','GBP'),
(5,1,'000001','000001EUR101','EUR'),
(6,1,'000001','000001USD101','USD'),
(7,1,'000001','000001USD102','USD'),
(8,1,'100002','100002EUR101','EUR'),
(9,1,'100002','100002USD101','USD'),
(10,2,'003333','003333EUR202','EUR'),
(11,2,'003333','003333JPY201','JPY');
(Select * From Account Where CustomerNumber = '000001'
)
EDIT: now I hope that would be better ...
how can I create Select from table with Group by Currenncy and Order by AccountNumber, and with possibility to create Resultset starts with for example AccountNumbers with USD currency, rest of rows would be alphabetically grouped by currency and ordered by AccountNumber
to extract a rows that would be sorted with follows order USD - EUR - JPY
Edit 2:
my bad, sorry for that, really time to start to learn the major language
basically if I call SELECT * FROM Account Where idCustomerNumber = '000001' Order by Currency, AccountNumber
then SQL interpreter returns
5,1,"000001","000001EUR101","EUR"
1,1,"000001","000001JPY101","JPY"
6,1,"000001","000001USD101","USD"
7,1,"000001","000001USD102","USD"
but now I want to programmatically determine that Resulset starts with USD accounts and rest of account would be Group by Currency then Order by AccountNumber
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前尚不清楚你想要什么。也许这个?:
It's not clear what you want. Perhaps this?:
我认为这应该对您有帮助:
通常当您尝试分组时,您需要考虑要对组合的列做什么,对于分组,您需要使用汇总值,例如:让我们假设您有一个金额列:
至于按美元、欧元、日元等排序,最好在CurrencySortOrder 表中定义,以便您可以添加更多值,而无需更改所有查询等。
I think this should help you:
Usually when you are trying to group on you need to consider what you are going to do with the columns that you combine on, for grouping you need to use summary values, eg: lets assume you have an Amount column:
As for the sorting by USD then EUR then JPY and so on, this would best be defined in a CurrencySortOrder table so that you can add further values without having to change all your queries and such.