PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 29, 2022

[FIXED] How to write SQL sub-query in SQL?

 October 29, 2022     left-join, sql, sqlite, sum     No comments   

Issue

Here is sample data I am looking for total buying trade value and total selling trades value based on country.

Here are two tables, country, and trades Table [companies]:

  +-------------+--------------------+
  |         name|            country |
  +-------------+--------------------+
  |  Alice s.p. |         Wonderland |
  |      Y-zap  |         Wonderland |
  |    Absolute |          Mathlands |
  |  Arcus t.g. |          Mathlands |
  | Lil Mermaid | Underwater Kingdom |
  | None at all |        Nothingland |
  +-------------+--------------------+

Table [trades]:

trades:
  +----------+-------------+------------+-------+
  |       id |      seller |      buyer | value |
  +----------+-------------+------------+-------+
  | 20121107 | Lil Mermaid | Alice s.p. |    10 |
  | 20123112 |  Arcus t.g. |      Y-zap |    30 |
  | 20120125 |  Alice s.p. | Arcus t.g. |   100 |
  | 20120216 | Lil Mermaid |   Absolute |    30 |
  | 20120217 | Lil Mermaid |   Absolute |    50 |
  +----------+-------------+------------+-------+

Expected Output:

  +--------------------+--------+--------+
  |             country|  buyer |  seller|
  +--------------------+--------+--------+
  |          Mathlands |    180 |     30 |
  |        Nothingland |      0 |      0 |
  | Underwater Kingdom |      0 |     90 |
  |         Wonderland |     40 |    100 |
  +--------------------+--------+--------+

I am trying this: It gives only one value column and it doesn't show the 0 trade country that I want to show also.

select country, sum(value), sum(value) 
from
(select a.buyer as export, a.seller as import, value, b.country as country
from trades as a
join companies as b 
on a.seller=b.name)

group by country 
order by country

Solution

Join country to distinct rows of trades which contain only buyer or seller and aggregate conditionally:

SELECT c.country,
       SUM(CASE WHEN buyer IS NOT NULL THEN value ELSE 0 END) buyer,
       SUM(CASE WHEN seller IS NOT NULL THEN value ELSE 0 END) seller
FROM country c 
LEFT JOIN (
  SELECT buyer, null seller, value FROM trades
  UNION ALL
  SELECT null, seller, value FROM trades
) t ON c.name IN (t.buyer, t.seller)
GROUP BY c.country

Or, with SUM() window function:

SELECT DISTINCT c.country,
       SUM(CASE WHEN c.name = t.buyer THEN value ELSE 0 END) OVER (PARTITION BY c.country) buyer,
       SUM(CASE WHEN c.name = t.seller THEN value ELSE 0 END) OVER (PARTITION BY c.country) seller
FROM country c LEFT JOIN trades t
ON c.name IN (t.buyer, t.seller)

See the demo.



Answered By - forpas
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing