使用非标准 Group by in 子句进行选择

发布于 2024-11-08 14:37:40 字数 1629 浏览 0 评论 0原文

抱歉,如果我重新发明轮子(以及我的英语),也许用普通的查询是不可能的,但我有“无意义的想法”,可以将行块移动到结果集顶部,我希望这个 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 技术交流群。

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

发布评论

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

评论(2

或十年 2024-11-15 14:37:40

目前尚不清楚你想要什么。也许这个?:

SELECT
    *
FROM
    account
WHERE
    idCustomerNumber = '000001'
ORDER BY
    Currency = 'USD'  DESC
  , Currency
  , AccountNumber ;

It's not clear what you want. Perhaps this?:

SELECT
    *
FROM
    account
WHERE
    idCustomerNumber = '000001'
ORDER BY
    Currency = 'USD'  DESC
  , Currency
  , AccountNumber ;
久随 2024-11-15 14:37:40

我认为这应该对您有帮助:

 SELECT * FROM account
 WHERE idCustomerNumber = '000001'
 ORDER BY AccountNumber, CASE WHEN Currency = 'USD' THEN 0 WHEN Currency = 'EUR' THEN 1 WHEN Currency = 'JPY' THEN 2 ELSE 0 END

通常当您尝试分组时,您需要考虑要对组合的列做什么,对于分组,您需要使用汇总值,例如:让我们假设您有一个金额列:

 SELECT AccountNumber, Currency, SUM(Amount) AS TotalAmount FROM account
 WHERE idCustomerNumber = '000001'
 GROUP BY AccountNumber, Currency
 ORDER BY AccountNumber, CASE WHEN Currency = 'USD' THEN 0 WHEN Currency = 'EUR' THEN 1 WHEN Currency = 'JPY' THEN 2 ELSE 0 END

至于按美元、欧元、日元等排序,最好在CurrencySortOrder 表中定义,以便您可以添加更多值,而无需更改所有查询等。

I think this should help you:

 SELECT * FROM account
 WHERE idCustomerNumber = '000001'
 ORDER BY AccountNumber, CASE WHEN Currency = 'USD' THEN 0 WHEN Currency = 'EUR' THEN 1 WHEN Currency = 'JPY' THEN 2 ELSE 0 END

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:

 SELECT AccountNumber, Currency, SUM(Amount) AS TotalAmount FROM account
 WHERE idCustomerNumber = '000001'
 GROUP BY AccountNumber, Currency
 ORDER BY AccountNumber, CASE WHEN Currency = 'USD' THEN 0 WHEN Currency = 'EUR' THEN 1 WHEN Currency = 'JPY' THEN 2 ELSE 0 END

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文