简体   繁体   English

Oracle SQL Developer(4.0.0.12)

[英]Oracle SQL Developer(4.0.0.12)

First time posting here, hopes it goes well. 第一次在这里发布,希望一切顺利。

I try to make a query with Oracle SQL Developer, where it returns a customer_ID from a table and the time of the payment from another. 我尝试使用Oracle SQL Developer进行查询,该查询从表中返回customer_ID,并从另一个表中返回付款时间。 I'm pretty sure that the problems lies within my logicflow (It was a long time I used SQL, and it was back in school so I'm a bit rusty in it). 我很确定问题出在我的逻辑流程之内(这是我使用SQL的时间很长了,而且又回到学校了,所以我对此有些生锈)。 I wanted to list the IDs as DISTINCT and ORDER BY the dates ASCENDING, so only the first date would show up. 我想将ID列出为DISTINCT和ORDER BY日期(升序),因此只会显示第一个日期。

However the returned table contains the same ID's twice or even more in some cases. 但是,在某些情况下,返回的表包含相同ID的两倍甚至更多。 I even found the same ID and same DATE a few times while I was scrolling through it. 滚动浏览时,我甚至发现了相同的ID和相同的DATE几次。

If you would like to know more please ask! 如果您想了解更多,请询问!

SELECT DISTINCT
    FIRM.customer.CUSTOMER_ID,
    FIRM.account_recharge.X__INSDATE FELTOLTES
FROM
    FIRM.customer
        INNER JOIN FIRM.account
        ON FIRM.customer.CUSTOMER_ID = FIRM.account.CUSTOMER
            INNER JOIN FIRM.account_recharge
            ON FIRM.account.ACCOUNT_ID = FIRM.account_recharge.ACCOUNT
WHERE
    FIRM.account_recharge.X__INSDATE BETWEEN TO_DATE('14-01-01', 'YY-MM-DD') AND TO_DATE('14-12-31', 'YY-MM-DD')
ORDER
    BY FELTOLTES

SELECT DISTINCT FIRM.customer.CUSTOMER_ID, FIRM.account_recharge.X__INSDATE FELTOLTES 选择DISTINCT FIRM.customer.CUSTOMER_ID,FIRM.account_recharge.X__INSDATE FELTOLTES

Distinct is applied to both the columns together, which means you will get a distinct ROW for the set of values from the two columns. 不同的是应用到列在一起,这意味着你会得到一个不同的ROW从两列值的集合 So, basically the distinct refers to all the columns in the select list. 因此,基本上,非重复是指选择列表中的所有列。

It is equivalent to a select without distinct but a group by clause. 它等效于一个没有唯一但由group by子句组成的选择。

It means, 它的意思是,

select distinct a, b....

is equivalent to, 等价于

select a, b...group by a, b

If you want the desired output, then CONCATENATE the columns. 如果需要所需的输出,则CONCATENATE列。 The distict will then work on the single concatenated resultset. 然后,Distict将在单个串联的结果集上工作。

Your select works like this because a CUSTOMER_ID indeed has more than one X__INSDATE , therefore the records in the result will be distinct. 您的选择之所以这样工作,是因为CUSTOMER_ID实际上具有多个X__INSDATE ,因此结果中的记录将是不同的。 If you need only the first date then don't use DISTINCT and ORDER BY but try to select for MIN(X__INSDATE) and use GROUP BY CUSTOMER_ID . 如果只需要第一个日期,则不要使用DISTINCTORDER BY而是尝试选择MIN(X__INSDATE)并使用GROUP BY CUSTOMER_ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM