Code Monkey home page Code Monkey logo

advanced-mysql-queries's Introduction

Combine MySQL queries without Union - Advanced SQL

Union queries is the most used solution for combining two queries. It is better to use a single query method even if the structure is complex. As an example, when we have a table consists of thousands of rows sometimes it is enough to show the top few rows.

CREATE TABLE trick
(name varchar(7), value int);

INSERT INTO trick
(name, value)
VALUES
('Ahmad', 10),
('peter', 20),
('kevin', 30),
('karim', 15),
('andriy', 23),
('hoa', 17),
('yuzi', 19),
....
('Denika', 22);

Table with thousands number of rows:
-----------
name value
-----------
Ahmad 10
peter 20
kevin 30
karim 15
andriy 23
hoa 17
yuzi 19
.....
Denika 22

Simplified table with top 5 rows and new summary row (Others) sum up the rest at the end instead:

-----------
name value
-----------
Ahmad 10
peter 20
kevin 30
karim 15
andriy 23
Others 6754


In case of using Union, it is easy to call two queries: 'one to get the top rows' and 'second to get the last summary row'.
This example returns the same result but in single query without using SQL union

set @row_number=0;
set @sumval=0;
select b.name,
COALESCE(b.value, b.sumvalue) AS MySummarizedValue
from(
select (@row_number:=@row_number + 1) AS num,
IFNULL(name,'others') name,
value,
sum( Case
when @row_number<=5
then @sumval:=@sumval+value and null
else value
end
) as sumvalue
from trick
group by name,value
with rollup
) b
where (
(b.value is not null and b.sumvalue is null) or (b.value is null and b.sumvalue is not null) &&
(b.num<5 or b.name='others')
);

advanced-mysql-queries's People

Contributors

karawash avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.