SQL*Plus查询帮助从不同的表中求和和计数,并获得正确答案

SQL*Plus查询帮助从不同的表中求和和计数,并获得正确答案,sql,oracle,Sql,Oracle,这是我的问题,给了我错误的金额。我怎样才能修好它?它应该给167700,但它给我251550的贷款总额。计数正常=15。我不能改变桌子上的任何东西 create table loan (loan_number varchar(15) not null, branch_name varchar(15) not null, amount number not null, primary key(loan_number)); creat

这是我的问题,给了我错误的金额。我怎样才能修好它?它应该给167700,但它给我251550的贷款总额。计数正常=15。我不能改变桌子上的任何东西

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
简单规则:切勿在
FROM
子句中使用逗号。始终使用明确、正确的
JOIN
语法和
ON
子句中的条件。那你就不会忘记他们了

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
因此:

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;

当然,我编造了用于连接的列的名称,因为您的问题没有描述表的信息。

Duh!两个表中没有公用键。怎么样?您如何跟踪哪些客户接受了哪些贷款

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
为什么要对来自两个不相关表的数据运行一个查询?您创建的交叉联接(对两个表的枚举没有任何条件)只是将第一个表中的每一行联接到第二个表中的每一行

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
customer表似乎有15行,所有15个名称都是不同的。当您计算DISTINCT时,您得到了正确的数字,即使在交叉联接中,每个customer_名称出现了很多次

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
另一方面,每个贷款金额重复15次。167700 x 15=2515500

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
如果需要在一行中同时显示贷款总额和(不同的)客户数,则需要

    create table loan
   (loan_number     varchar(15) not null,
    branch_name varchar(15) not null,
    amount      number not null,
    primary key(loan_number));

    create table customer
   (customer_name   varchar(15) not null,
    customer_street     varchar(12) not null,
    customer_city   varchar(15) not null,
    primary key(customer_name));


select SUM(amount),
COUNT( distinct customer_name)
from loan,customer;
select (select sum(amount) from loan) as total_amount, 
       (select count (distinct customer_name) from customer) as distinct_customers
from   dual
;

我知道如何加入一个表,但我没有一个共同的关键在他们。我将编辑我的问题。@d_PI。嗯,贷款是给某人的。所以,我想象在某个地方有一个表,在贷款和客户之间有一个链接。有另一个表用来跟踪客户和贷款。它不起作用-可能我在错误的空格中有一个括号。不是每个来这里问问题的人都是懒惰的,在他们提问之前不尝试。你不应该假设。我可能不擅长提问和描述问题,但我知道如何使用谷歌。@d_PI-你说的“它不工作”是什么意思?你收到错误消息了吗?您是否完全按照编写的方式使用查询?剩下的,我不知道你指的是什么(用“懒惰”和“谷歌”之类的词)-那是关于什么的?只是现在尝试你的查询-它确实有效。就像我说的“也许我把括号放错地方了”。我在上找到了相同的示例,但我只是缺少正确的括号顺序。-不管怎么说,谢谢,其他的事情只是我感到沮丧,因为我觉得你们认为我一点都不知道我在说什么,但实际上只是因为我提出了一个不充分的问题。