1Z0-051 QUESTION 14 DISTINCT的用法

SQL查询技巧
Using the CUSTOMERS table, you need to generate a report that shows 50% of each credit amount in each income level. The report should NOT show any repeated credit amounts in each income level.
Which query would give the required result?
A. SELECT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50% Credit Limit"
    FROM customers;
B. SELECT DISTINCT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50% Credit Limit"
    FROM customers;
C. SELECT DISTINCT cust_income_level ||' '|| cust_credit_limit * 0.50 AS "50% Credit Limit"
    FROM customers;
D. SELECT cust_income_level|| ' ' ||cust_credit_limit * 0.50 AS "50% Credit Limit"

    FROM customers;


答案:C

解析:

--A选项报错
SQL> SELECT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50% Credit
Limit"
  2      FROM customers;
SELECT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50% Credit Limit
"
                          *
ERROR at line 1:
ORA-00936: missing expression

--B选项报错
SQL> SELECT DISTINCT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50
% Credit Limit"
  2      FROM customers;
SELECT DISTINCT cust_income_level, DISTINCT cust_credit_limit * 0.50 AS "50% Cre
dit Limit"
                                   *
ERROR at line 1:
ORA-00936: missing expression

--C选项得出正确的结果
SQL> SELECT DISTINCT cust_income_level ||' '|| cust_credit_limit * 0.50 AS "50%
Credit Limit"
  2      FROM customers;

50% Credit Limit
--------------------------------------------------------------------------------

level4 2500
level12 25000
level2 25000
level3 250000
level1 2500
--D选项把重复的结果都显示出来了
SQL> SELECT cust_income_level|| ' ' ||cust_credit_limit * 0.50 AS "50% Credit Li
mit"
  2      FROM customers;

50% Credit Limit
--------------------------------------------------------------------------------

level1 2500
level2 25000
level3 250000
level12 25000
level1 2500
level4 2500

6 rows selected.

#include <iostream> #include <fstream> #include <vector> using namespace std; // 预计算组合数表 C[i][j]: 从 i 个元素中取 j 个的组合数 vector<vector<long long>> precomputeCombinations(int max_n, int max_k) { vector<vector<long long>> C(max_n + 1, vector<long long>(max_k + 1, 0)); for (int i = 0; i <= max_n; i++) { for (int j = 0; j <= min(i, max_k); j++) { if (j == 0) C[i][j] = 1; else C[i][j] = C[i-1][j-1] + C[i-1][j]; } } return C; } long long calculateCode(const string& s, const vector<vector<long long>>& C) { int L = s.length(); long long code = 0; // 1. 累加所有长度小于L的字符串数量 for (int len = 1; len < L; len++) { code += C[26][len]; // 26个字母选len个的组合数 } // 2. 累加相同长度但字典序更小的字符串数量 int prev = -1; // 上一个字符的索引(0-25),初始为-1(小于'a') for (int i = 0; i < L; i++) { int cur = s[i] - 'a'; // 当前字符索引 // 枚举所有可选的小于当前字符的可能字符 for (int c = prev + 1; c < cur; c++) { int remaining = 25 - c; // 剩余可用字母数 int needed = L - i - 1; // 还需选择的字母数 if (remaining >= needed) { code += C[remaining][needed]; // 累加组合数 } } prev = cur; // 更新上一个字符索引 } return code + 1; // 加上当前字符串本身 } int main() { ifstream inFile("input.txt"); ofstream outFile("output.txt"); // 预计算组合数表(最大26选6) auto C = precomputeCombinations(26, 6); int k; inFile >> k; // 读取字符串数量 inFile.ignore(); // 忽略换行符 for (int i = 0; i < k; i++) { string s; getline(inFile, s); long long code = calculateCode(s, C); outFile << code << endl; } inFile.close(); outFile.close(); return 0; } 代码思路
10-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值