Oracle SQL命令用于HAVING COUNT。

huangapple go评论61阅读模式
英文:

Oracle SQL command for HAVING COUNT

问题

以下是翻译好的部分:

问题是

> 任何艺术品的详细信息,以及创作该作品的艺术家的全名,这些作品在数据库中至少有两个副本记录。

我尝试使用这段代码,但没有显示任何内容:

SELECT w.WorkID, w.Title, w.Copy, w.Medium, w.Description,
       a.FirstName || ' ' || a.LastName AS ArtistFullName
FROM dtoohey.Work w,
     dtoohey.artist a
WHERE w.ArtistID = a.ArtistID
AND w.WorkID IN (
    	SELECT w.WorkID
    	FROM dtoohey.Work w
    	GROUP BY w.WorkID
    	HAVING COUNT(w.copy) > 2
        );

我非常困惑要添加或更改“having count”的内容。

数据显示了副本,如下图所示。

副本的数据显示

有人可以帮助我解决这个问题吗?

英文:

The question is

> The details of any works of art along with the full name of the Artist who created the work that have at least two copies recorded in the database.

I tried using this code but it showed nothing:

SELECT w.WorkID, w.Title, w.Copy, w.Medium, w.Description,
       a.FirstName || ' ' || a.LastName AS ArtistFullName
FROM dtoohey.Work w,
     dtoohey.artist a
WHERE w.ArtistID = a.ArtistID
AND w.WorkID IN (
    	SELECT w.WorkID
    	FROM dtoohey.Work w
    	GROUP BY w.WorkID
    	HAVING COUNT(w.copy) > 2
        );

I'm very confused on what to add or change for having count.

The data shows the copy as in the picture below.

the data shown of the 'copy'

Can anyone help me in resolving this issue?

答案1

得分: 3

你需要分析你的数据并确定副本中的相同部分。如果WorkID是主键,那么只会有一个具有该标识符的行;在这种情况下,您需要使用其他方法来检测副本,例如TitleArtistID的组合。

此外,HAVING COUNT(w.copy) > 2 将检查是否有三个或更多副本。您想要使用 > 1>= 2 来检查两个或更多副本。

SELECT w.WorkID,
       w.Title,
       w.Copy,
       w.Medium,
       w.Description,
       a.FirstName ||' '|| a.LastName AS ArtistFullName
FROM   dtoohey.Work w
       INNER JOIN dtoohey.artist a
       ON w.ArtistID=a.ArtistID
WHERE  EXISTS (
         SELECT 1
         FROM   dtoohey.Work x
         WHERE  x.artistid = w.artistid
         AND    x.title    = w.title
         GROUP BY x.artistid, x.title
         HAVING COUNT(*) > 1
       );

您还可以使用分析函数来计算重复项:

SELECT w.WorkID,
       w.Title,
       w.Copy,
       w.Medium,
       w.Description,
       a.FirstName ||' '|| a.LastName AS ArtistFullName
FROM   ( SELECT w.*,
                COUNT(*) OVER (PARTITION BY artistid, title) AS cnt
         FROM   dtoohey.Work w
       ) w
       INNER JOIN dtoohey.artist a
       ON w.ArtistID=a.ArtistID AND cnt > 1;

对于示例数据:

CREATE TABLE dtooney.work (workid, title, copy, medium, description, artistid) AS
SELECT 1, 'Finger Painting', '1/5', 'Acrylic on Paper', 'Finger Painting', 1 FROM DUAL UNION ALL
SELECT 2, 'Finger Painting', '4/5', 'Acrylic on Paper', 'Finger Painting', 1 FROM DUAL UNION ALL
SELECT 3, 'The Scream', '1/1', 'Wool', 'Knitted version of The Scream', 2 FROM DUAL UNION ALL
SELECT 4, 'Half a Cow', '1/2', 'Taxidermy', 'Front Half', 3 FROM DUAL UNION ALL
SELECT 5, 'Half a Cow', '2/2', 'Taxidermy', 'Left Half', 3 FROM DUAL;

CREATE

<details>
<summary>英文:</summary>

You need to analyse your data and work out what is going to be the same for the copies. If `WorkID` is a primary key then there will only ever be one row with that identifier; in that case, you need to use some other method of detecting copies such as the combination of `Title` and `ArtistID`.

Additionally, `HAVING COUNT(w.copy) &gt; 2` will check to see if there are three-or-more copies. You want either `&gt; 1` or `&gt;= 2` to check for two-or-more copies.

```lang-sql
SELECT w.WorkID,
       w.Title,
       w.Copy,
       w.Medium,
       w.Description,
       a.FirstName ||&#39; &#39;|| a.LastName AS ArtistFullName
FROM   dtoohey.Work w
       INNER JOIN dtoohey.artist a
       ON w.ArtistID=a.ArtistID
WHERE  EXISTS (
         SELECT 1
         FROM   dtoohey.Work x
         WHERE  x.artistid = w.artistid
         AND    x.title    = w.title
         GROUP BY x.artistid, x.title
         HAVING COUNT(*) &gt; 1
       );

You could also use analytic functions to count the duplicates:

SELECT w.WorkID,
       w.Title,
       w.Copy,
       w.Medium,
       w.Description,
       a.FirstName ||&#39; &#39;|| a.LastName AS ArtistFullName
FROM   ( SELECT w.*,
                COUNT(*) OVER (PARTITION BY artistid, title) AS cnt
         FROM   dtoohey.Work w
       ) w
       INNER JOIN dtoohey.artist a
       ON w.ArtistID=a.ArtistID AND cnt &gt; 1;

Which, for the sample data:

CREATE TABLE dtooney.work (workid, title, copy, medium, description, artistid) AS
SELECT 1, &#39;Finger Painting&#39;, &#39;1/5&#39;, &#39;Acrylic on Paper&#39;, &#39;Finger Painting&#39;, 1 FROM DUAL UNION ALL
SELECT 2, &#39;Finger Painting&#39;, &#39;4/5&#39;, &#39;Acrylic on Paper&#39;, &#39;Finger Painting&#39;, 1 FROM DUAL UNION ALL
SELECT 3, &#39;The Scream&#39;, &#39;1/1&#39;, &#39;Wool&#39;, &#39;Knitted version of The Scream&#39;, 2 FROM DUAL UNION ALL
SELECT 4, &#39;Half a Cow&#39;, &#39;1/2&#39;, &#39;Taxidermy&#39;, &#39;Front Half&#39;, 3 FROM DUAL UNION ALL
SELECT 5, &#39;Half a Cow&#39;, &#39;2/2&#39;, &#39;Taxidermy&#39;, &#39;Left Half&#39;, 3 FROM DUAL;

CREATE TABLE dtooney.artist (artistid, firstname, lastname) AS
SELECT 1, &#39;Alice&#39;, &#39;Abbot&#39; FROM DUAL UNION ALL
SELECT 2, &#39;Betty&#39;, &#39;Baron&#39; FROM DUAL UNION ALL
SELECT 3, &#39;Carol&#39;, &#39;Count&#39; FROM DUAL;

Both outputs:

WORKID TITLE COPY MEDIUM DESCRIPTION ARTISTFULLNAME
1 Finger Painting 1/5 Acrylic on Paper Finger Painting Alice Abbot
2 Finger Painting 4/5 Acrylic on Paper Finger Painting Alice Abbot
4 Half a Cow 1/2 Taxidermy Front Half Carol Count
5 Half a Cow 2/2 Taxidermy Left Half Carol Count

fiddle

huangapple
  • 本文由 发表于 2023年6月15日 18:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定