0

I would like to check whether a record with the same ID (Product_ID) has more than one record with a different date (I would like to check if the product was received in different loots or in just one day), so if it just returns one row it means that it was delivered all the same day, and if more than one result is return its the other way round.

Table PRODUCT

ID (PK) | Product_ID | Type | Deliver_Date | Amount

I've tried with a group by and distinct with no result.

EDIT: Query I had so far...

    SELECT DISTINCT ,
                count(*)
FROM            PUBLIC.product
WHERE           product_id = ?
AND             deliver_date = ?
HAVING          count() = 1 
4
  • 1
    Well, what did you try with group by and distinct? Nov 28, 2015 at 12:35
  • @RealSkeptic sorry for the late reply... the query I had was "select distinct , count() from public.product where product_id = ? and deliver_date = ? having count() = 1" trying to retrieve all the rows which product_id just had one value for the deliver_date, and "count() > 1" for those with more than one date... But it seems to be not working properly I don't know why...
    – Niconoid
    Nov 29, 2015 at 14:37
  • Never put code in the comments. Edit the question and add the code to it. You can then use a comment to alert someone to the fact that you made a change. Nov 29, 2015 at 14:49
  • @RealSkeptic Updated, thanks for the heads up.
    – Niconoid
    Nov 29, 2015 at 15:21

1 Answer 1

4

Your use of the DISTINCT keyword is incorrect.

You can either use DISTINCT with a list of fields following it, telling which fields you want to have distinct, or use an aggregate function like COUNT, SUM, MIN etc. together with grouping fields.

E.g.

SELECT DISTINCT Product_Id, Deliver_date
FROM ...
WHERE ...

means "give me all the distinct combinations of product id and deliver date". This is not actually what you need, as DISTINCT Product_id will simply tell you which product IDs there are, but not how many of them and in which dates, and DISTINCT Product_id, Deliver_date will give you all possible combinations of product ID and deliver date, but you'll need to count them manually.

The GROUP BY construct is more informative

SELECT count(*), Product_Id
FROM ...
WHERE ...
GROUP BY Product_Id

Groups the rows by the product id. It tells you how many rows are there for each product ID.

But what if you have several rows with the same product ID and the same date? You'll get a number greater than 1 in that query, but it won't help you because you wanted to distinguish between products delivered on two different days and products delivered all on the same day.

To do this, you need to use COUNT(DISTINCT(Deliver_date)):

SELECT COUNT(DISTINCT(Deliver_date)), Product_ID
FROM ...
WHERE ...
GROUP BY Product_ID

This means: * Separate the rows into groups by the Product_ID. * Inside each such group (all the rows that have that Product_ID), find all the distinct Deliver_Date values. Count how many such Delivery_Date values there are inside the group.

So if the product was delivered 10 times within the same day, you'll just have one distinct delivery date for that product ID. The COUNT will return 1. If it was delivered 5 times on day x, and 5 times on day y, then you'll have two distinct delivery dates (x and y), and the COUNT will return 2.

Now, if you want to eliminate all the ones that were all delivered on the same day (the count of distinct dates is 1), you add a HAVING clause to your query:

SELECT COUNT(DISTINCT(Deliver_date)), Product_Id
FROM PRODUCT
GROUP BY Product_Id
HAVING COUNT(DISTINCT(Deliver_date)) > 1

This will give you a list of all the products that were delivered on at least two separate dates.

Of course, if you just want to check if a particular product was delivered on more than one date, it's simpler:

SELECT COUNT(DISTINCT(Deliver_date))
FROM PRODUCT
WHERE Product_ID = ?

This will give you the number of distinct days on which this product was delivered. If it delivered on just one day, the result will be 1. If it was delivered on more than one day, the result will be a number greater than one.


To sum up:

  • There is no such thing as a DISTINCT,. DISTINCT is always followed by the name of a field or fields that are supposed to be distinct.
  • But there is a COUNT(DISTINCT(field_name)) which counts how many distinct values the field has in a group or a result set.
6
  • distinct is not a function. Writing DISTINCT(field_name) makes no sense.
    – user330315
    Nov 29, 2015 at 15:36
  • @a_horse_with_no_name I admit that I don't know what HSQL does, my experience is more in PostgreSQL. Now, in PostgreSQL, if you have more than one field in the DISTINCT inside COUNT, it thinks that you are using a two-argument COUNT function, that doesn't exist. So you need to write COUNT(DISTINCT(a,b)) rather than COUNT(DISTINCT a,b). So I just always use parentheses. Nov 29, 2015 at 16:38
  • (a,b) is not the same as a,b in Postgres (a,b) creates an anonymous "record type" - a single column with two fields. It's a short version of row(a,b). Because of that count(distinct(a,b)) happens to work in Postgres - you are still counting a single "column expression". I won't work in any other DBMS. It's still count(distinct some_expression) where some_expression happens to be (a,b). Writing distinct(..) as a function call is simply wrong.
    – user330315
    Nov 29, 2015 at 17:03
  • I checked it and it works in hsql-db, with or without parentheses. Including COUNT( DISTINCT a,b ) and COUNT( DISTINCT (a,b)). Apparently, different vendors have different ideas about SQL syntax. So it's not a function. But it's also not wrong. Nov 29, 2015 at 20:05
  • HSQLDB accepts COUNT( DISTINCT a,b ) as it is Standard SQL. The DISTINCT (a,b) form is counting distinct rows from a row constructor and gives the same result.
    – fredt
    Nov 29, 2015 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.