so i have a query like this :
SELECT * FROM my_db.my_order where product_id = 395 order by id desc;
and the output of this table is like this
+----------+----------+----------+---------------------+------------+--------+
| order_id | buyer_Id | quantity | createdAt | product_id | status |
+----------+----------+----------+---------------------+------------+--------+
| 6232 | 89450 | 1 | 2020-05-06 17:44:41 | 395 | 1 |
| 6232 | 89450 | 1 | 2020-05-06 17:44:41 | 395 | 1 |
| 6232 | 23048 | 2 | 2020-05-06 17:44:41 | 395 | 1 |
| 6232 | 89464 | 1 | 2020-05-06 17:44:40 | 395 | 1 |
| 6232 | 89463 | 1 | 2020-05-06 17:44:40 | 395 | 1 |
| 6232 | 89463 | 2 | 2020-05-06 17:43:25 | 395 | 0 |
| 6232 | 89464 | 2 | 2020-05-06 17:43:19 | 395 | 0 |
+----------+----------+----------+---------------------+------------+--------+
so i want to count total of quantity for this product_id where the status are = 1, so i made this query
SELECT
SUM(grouped_my_order_tbl.quantity)
FROM
(
SELECT
mo.order_seller_id AS order_seller_id,
mo.quantity AS quantity,
mo.status AS status,
mo.product_id AS product_id
FROM my_order mo
) grouped_my_order_tbl
WHERE
grouped_my_order_tbl.product_id = 395
AND grouped_my_order_tbl.order_seller_id = 6232
AND grouped_my_order_tbl.status = 1;
and the output for the count is 6 instead of 5
where's my wrong at?
expected result : sum = 5
update : the exact sum should be 5 because there's duplicate buyer_id and createdAt so the system read the data twice, instead it's just only 1 record not 2 record
Related
I am setting up a virtual classroom in PHP and MySQL. This classroom consists of courses and each course contains different subjects or modules. The student has to examine each module and, finally, a summary (evaluation board) is made to know if the student has passed the course or not.
Having said that, I have a table in which I store the evaluations of each student, in which I keep inscripcion_id (student - inscription_id), modulo_id (module_id), fecha (date_of_examination), aciertos (number_of_right_answers), ultima_convocatoria (evaluation_last_convocatory) and estado (status).
SQL Fiddle -> here
Through some previously established rules that tell me if a student has passed a module or not, I get the following set of data:
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| inscripcion_id | modulo_id | fecha | aciertos | ultima_convocatoria | estado | ev |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| 890 | 1 | 2018-01-24 22:26:09 | 8 | 2 | 1 | aprobado |
| 890 | 2 | 2018-01-24 22:36:58 | 3 | 3 | 0 | suspendido |
| 890 | 5 | 2018-01-24 22:38:50 | 3 | 1 | 0 | suspendido |
| 890 | 6 | 2018-01-24 22:44:20 | 7 | 3 | 0 | suspendido |
| 891 | 1 | 2018-01-25 09:24:42 | 8 | 1 | 1 | aprobado |
| 891 | 2 | 2018-01-25 10:01:55 | 4 | 8 | 0 | suspendido |
| 891 | 4 | 2018-01-25 10:51:49 | 5 | 3 | 1 | suspendido |
| 891 | 5 | 2018-01-25 10:23:45 | 9 | 1 | 1 | aprobado |
| 891 | 6 | 2018-01-25 11:21:20 | 7 | 3 | 0 | suspendido |
| 896 | 1 | 2018-01-25 11:55:48 | 1 | 1 | 1 | suspendido |
| 898 | 1 | 2018-01-25 14:01:51 | 6 | 1 | 1 | suspendido |
| 907 | 1 | 2018-03-25 16:06:18 | 3 | 1 | 0 | suspendido |
| 907 | 2 | 2018-03-25 16:07:34 | 3 | 1 | 0 | suspendido |
| 907 | 3 | 2018-03-25 16:09:04 | 3 | 1 | 0 | suspendido |
| 907 | 4 | 2018-03-25 16:08:13 | 3 | 1 | 0 | suspendido |
| 907 | 5 | 2018-03-25 16:10:37 | 2 | 1 | 0 | suspendido |
| 907 | 6 | 2018-03-25 16:08:44 | 3 | 1 | 0 | suspendido |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
This data is obtained through the following query:
SELECT e1.inscripcion_id,
e1.modulo_id,
e1.fecha,
e1.aciertos,
e1.convocatoria AS ultima_convocatoria,
e1.estado,
if (
( e1.modulo_id in (SELECT modulo.modulo_id FROM modulo WHERE modulo.curso_id = 1 AND modulo.categoria_id = 1)
AND e1.aciertos <= 7 )
OR ( e1.modulo_id = (SELECT modulo.modulo_id FROM modulo WHERE modulo.curso_id = 1 AND modulo.categoria_id = 2)
AND e1.aciertos <= 11 ),
"suspendido",
"aprobado"
) AS ev
FROM (
SELECT inscripcion_id,
modulo_id,
MAX(convocatoria) AS max_convocatoria
FROM `evaluacion`
GROUP BY inscripcion_id,
modulo_id
ORDER BY `inscripcion_id` ASC,
`modulo_id` ASC,
`convocatoria` ASC
) AS e2
INNER JOIN evaluacion AS e1
ON e1.inscripcion_id = e2.inscripcion_id
AND e1.modulo_id = e2.modulo_id
AND e1.convocatoria = e2.max_convocatoria
As you can see, the student 890, has made modules 1, 2, 5 and 6. What I want to achieve is that the modules that are still pending, I also get as a result in the previous data set. The exemplification:
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| inscripcion_id | modulo_id | fecha | aciertos | ultima_convocatoria | estado | ev |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
| 890 | 1 | 2018-01-24 22:26:09 | 8 | 2 | 1 | aprobado |
| 890 | 2 | 2018-01-24 22:36:58 | 3 | 3 | 0 | suspendido |
| 890 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 890 | 4 | NULL | NULL | NULL | NULL | pendiente |
| 890 | 5 | 2018-01-24 22:38:50 | 3 | 1 | 0 | suspendido |
| 890 | 6 | 2018-01-24 22:44:20 | 7 | 3 | 0 | suspendido |
| 891 | 1 | 2018-01-25 09:24:42 | 8 | 1 | 1 | aprobado |
| 891 | 2 | 2018-01-25 10:01:55 | 4 | 8 | 0 | suspendido |
| 891 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 891 | 4 | 2018-01-25 10:51:49 | 5 | 3 | 1 | suspendido |
| 891 | 5 | 2018-01-25 10:23:45 | 9 | 1 | 1 | aprobado |
| 891 | 6 | 2018-01-25 11:21:20 | 7 | 3 | 0 | suspendido |
| 896 | 1 | 2018-01-25 11:55:48 | 1 | 1 | 1 | suspendido |
| 896 | 2 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 3 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 4 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 5 | NULL | NULL | NULL | NULL | pendiente |
| 896 | 6 | NULL | NULL | NULL | NULL | pendiente |
| ... | | | | | | |
+----------------+-----------+---------------------+----------+---------------------+--------+------------+
The result is that the modules that the student has not yet done have been added, with the new value "pending" for the ev column.
I have no idea how to do this ... I've tried, I searched the internet and nothing :(
What is the final objective? What I want is to obtain a final list with all those students who have the pending course (that is, they have some pending module/s), to send them a reminder email that they have to examine themselves of the remaining modules. To those who have approved or suspended, no email will be sent to them.
Can you help me?
SQL Fiddle -> here
THANK YOU VERY MUCH
Suppose you want to get students with no module in evaluation table as 'pending' like what you provided in the example. The way to get student joining all module is to do a full join on modulo and evaluacion to get full set of distinct inscripcion_id modulo_id. Then left join with your existing query will provide the result you want.
sqlfiddle
SELECT fs.inscripcion_id,
fs.modulo_id,
e3.fecha,
e3.aciertos,
e3.ultima_convocatoria,
e3.estado,
IF(e3.ev IS NULL, "pendiente", e3.ev) AS ev
FROM (SELECT m.modulo_id,
e.inscripcion_id
FROM modulo m,
evaluacion e
GROUP BY m.modulo_id,
e.inscripcion_id) AS fs
LEFT JOIN (SELECT
e1.inscripcion_id,
e1.modulo_id,
e1.fecha,
e1.aciertos,
e1.convocatoria
AS
ultima_convocatoria
,
e1.estado,
IF (( e1.modulo_id IN (SELECT modulo.modulo_id
FROM modulo
WHERE modulo.curso_id = 1
AND modulo.categoria_id =
1)
AND e1.aciertos <= 7 )
OR ( e1.modulo_id = (SELECT modulo.modulo_id
FROM modulo
WHERE
modulo.curso_id = 1
AND modulo.categoria_id = 2)
AND e1.aciertos <= 11 ), "suspendido",
"aprobado")
AS ev
FROM (SELECT inscripcion_id,
modulo_id,
Max(convocatoria) AS max_convocatoria
FROM `evaluacion`
GROUP BY inscripcion_id,
modulo_id
ORDER BY `inscripcion_id` ASC,
`modulo_id` ASC,
`convocatoria` ASC) AS e2
INNER JOIN evaluacion AS e1
ON e1.inscripcion_id = e2.inscripcion_id
AND e1.modulo_id = e2.modulo_id
AND e1.convocatoria = e2.max_convocatoria)
AS e3
ON fs.modulo_id = e3.modulo_id
AND fs.inscripcion_id = e3.inscripcion_id
ORDER BY fs.inscripcion_id,
fs.modulo_id;
For the further question,
You may want to use
SELECT inscripcion_id,
SUM(case when ev = 'aprobado' then 1 else 0 end) as approved_cnt,
SUM(case when ev = 'suspendido' then 1 else 0 end) as suspended_cnt,
SUM(case when ev = 'pendiente' then 1 else 0 end) as pending_cnt
From --the above query...
Group by inscripcion_id
to get the count of status for each student, and then do the logic using those count.
After reviewing the new sqlfiddle
I wrote the query below, I think it should cover what you want
Notice that you more than one evaluation per module, meaning that you'll get more than one status per module
To solve that, you can add a group statement (in comment now)
Or you the different evaluations to your needs...
SELECT
i.inscripcion_id,
c.curso_id,
c.titulo AS curso_titulo,
m.modulo_id,
m.titulo AS modulo_titulo,
IFNULL(ev.estado,0) AS estado,
m.*
FROM
inscripcion i
INNER JOIN curso c ON c.curso_id = i.curso_id
INNER JOIN modulo m ON m.curso_id = c.curso_id
LEFT JOIN evaluacion ev ON ev.modulo_id = m.modulo_id
WHERE
(ev.estado = 0 OR ev.estado IS NULL)
/*
GROUP BY
m.modulo_id
*/
;
I have two table one is order_details and second is orders.
1) order_details
id | order_id | item | order_units | is_cancelled |
------------------------------------------------------------------
1 | 00001 | Mobile | 2 | 0 |
2 | 00001 | Headphone | 2 | 0 |
3 | 00001 | cover | 5 | 0 |
4 | 00002 | charger | 1 | 0 |
5 | 00002 | mobile | 1 | 0 |
6 | 00004 | Tablet | 2 | 0 |
7 | 00005 | Mobile | 1 | 0 |
8 | 00005 | Battery | 2 | 1 |
9 | 00006 | Mobile | 1 | 0 |
10 | 00006 | speaker | 1 | 0 |
11 | 00006 | Motinor | 1 | 0 |
12 | 00007 | Laptop | 2 | 0 |
2) orders
order_id | date | time |total_amount| round |discount|refund
-----------------------------------------------------------------------
00001 | 2017-06-16 |10:10:45 | 456.12 |-0.12 | 0 | 0
00002 | 2017-06-16 |10:25:45 | 600.00 | 0.00 | 10 | 50
00004 | 2017-06-16 |11:10:45 | 300.55 |-0.05 | 0 | 0
00005 | 2017-06-16 |12:10:45 | 200.45 | 0.05 | 20 | 0
00006 | 2017-06-16 |12:40:45 | 685.00 | 0.00 | 50 | 0
00007 | 2017-06-24 |14:10:45 | 888.35 | 0.15 | 0 | 0
I want to join the "order_details" with "orders" and the result should be as below:
---------------------------------------------------------------------
date | time | hour |order_count| total_units| net_amount
---------------------------------------------------------------------
| 2017-06-16 |10:10:45 | 10 | 2 | 11 | 996
| 2017-06-16 |11:10:45 | 11 | 1 | 2 | 300.50
| 2017-06-16 |12:40:45 | 12 | 2 | 4 | 180.50
--------------------------------------------------------------------
I have created a sql query for the above result format and all columns outputs are correct except the "total_units", its showing null.
The following query i have used:
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, dtl.total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(qty) AS total_units
FROM order_details
WHERE is_cancelled=0) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
Please help me to correct this query and generate the exact output as above.
Sorry, One correction in my query..
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, dtl.total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(order_units) AS total_units
FROM order_details
WHERE is_cancelled=0) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
I have made some corrections in the query and it is working fine now:
SELECT hdr.date,hdr.time, LPAD(HOUR(hdr.time),2,'0') AS hour, COUNT(hdr.`order_id`) AS order_count, SUM(dtl.order_units) AS total_units, SUM((hdr.total_amount+hdr.round-hdr.discount)-hdr.refund) AS net_amount
FROM orders hdr
LEFT JOIN (
SELECT order_id, SUM(order_units) AS order_units
FROM order_details
WHERE is_cancelled=0 GROUP BY order_id) dtl ON dtl.order_id = hdr.order_id
WHERE DATE(hdr.date) = '2017-06-16 ' AND (HOUR(hdr.time) BETWEEN ('10') AND ('12'))
GROUP BY hdr.date, HOUR(hdr.time)
Thank you.
I have three tables:
ITEMS
+----+--------------------------+----------+
| id | nome | quantity |
+----+--------------------------+----------+
| 1 | Pantaloni beige | 10 |
| 2 | Camicia cotone e seta | 1 |
| 3 | Camicia da notte | 5 |
| 4 | Completo notte | 3 |
+----+--------------------------+----------+
TRANSACTIONS
+----+---------------------+----------+-------------+
| id | data | quantity | id_articolo |
+----+---------------------+----------+-------------+
| 1 | 2016-07-19 15:28:09 | 3 | 1 |
| 2 | 2016-07-19 15:29:50 | 1 | 1 |
| 3 | 2016-07-19 15:59:34 | 1 | 2 |
| 4 | 2016-07-19 16:00:59 | 1 | 3 |
| 5 | 2016-07-19 16:01:10 | 1 | 188 |
| 6 | 2016-07-19 16:11:15 | 1 | 193 |
| 7 | 2016-07-19 16:11:24 | 1 | 194 |
| 8 | 2016-07-19 16:11:55 | 1 | 195 |
| 9 | 2016-07-19 16:51:14 | 1 | 204 |
+----+---------------------+----------+-------------+
RETURNED_ITEMS
+----+---------+-------------+----------+
| id | id_reso | id_articolo | quantity |
+----+---------+-------------+----------+
| 1 | 54 | 1 | 6 |
| 2 | 54 | 3 | 1 |
| 3 | 54 | 392 | 1 |
| 4 | 54 | 398 | 1 |
+----+---------+-------------+----------+
joined on "transactions.id_articolo" = "returned_items.id_articolo" = "items.id"
I want to retrieve a complete list containing only available products in which
(items.quantity) - (transactions.quantity) - (returned_items.quantity) > 0
eg. In the data above
item 1 = 0 [excluded]
item 2 = 0 [excluded]
item 3 = 3 [included in the list]
item 4 = 3 [included]
Any idea?
Thanks a lot!
V.
Looks like you will need to use inline views to aggregate quantity from the transactions table and the returned items tables
SELECT i.id
, i.quantity
, IFNULL(t.quantity,0) AS t_quantity
, IFNULL(r.quantity,0) AS r_quantity
, i.quantity - IFNULL(t.quantity,0) + IFNULL(r.quantity,0) AS calc_qty
FROM items i
LEFT
JOIN ( SELECT tt.id_articolo
, SUM(tt.quantity) AS quantity
FROM transactions tt
GROUP BY tt.id_articolo
) t
ON t.id_articolo = i.id
LEFT
JOIN ( SELECT rr.id_articolo
, SUM(rr.quantity) AS quantity
FROM returned_items rr
GROUP BY rr.id_articolo
) r
ON r.id_articolo = i.id
HAVING calc_qty > 0
ORDER BY i.id
For testing, omit the HAVING clause.
Note that the expression for calc_qty in the query above includes an addition operation. Go ahead and combine the quantity values with whatever arithmetic operations you need. Go ahead and do a subtraction is that satisfies the requirements.
invoice table
SELECT id, fname, gtotal, `date` FROM invoice WHERE id = 1;
| id | fname | gtotal | date |
|----|---------|--------|-----------------------|
| 1 | Brandon | 860 | May, 11 2016 00:00:00 |
invoice_contents table,
SELECT * FROM invoice_contents WHERE invoice_id = 1;
| id | invoice_id | item | price | quantity | discount | total |
|----|------------|------------|-------|----------|----------|-------|
| 1 | 1 | Dextrose | 10 | 10 | 5 | 95 |
| 2 | 1 | Nescaine | 20 | 30 | 10 | 540 |
| 3 | 1 | Anticavity | 30 | 10 | 25 | 225 |
This JOIN query
SELECT invoice.id, invoice.fname, invoice_contents.item,
invoice_contents.price, invoice_contents.quantit,
invoice_contents.discount, invoice_contents.total,
invoice.gtotal
FROM invoice_contents
INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1;
gives this result.
| id | fname | item | price | quantity | discount | total | gtotal |
|----|---------|------------|-------|----------|----------|-------|--------|
| 1 | Brandon | Dextrose | 10 | 10 | 5 | 95 | 860 |
| 1 | Brandon | Nescaine | 20 | 30 | 10 | 540 | 860 |
| 1 | Brandon | Anticavity | 30 | 10 | 25 | 225 | 860 |
I need this result.
| id | fname | item | price | quantity | discount | total | gtotal |
|----|---------|------------|-------|----------|----------|-------|--------|
| 1 | Brandon | Dextrose | 10 | 10 | 5 | 95 | 860 |
| | | Nescaine | 20 | 30 | 10 | 540 | |
| | | Anticavity | 30 | 10 | 25 | 225 | |
I am just a beginner in MySQL. I have been trying from this morning to get this kind of output by experimenting on different combinations please help me out.
#Rex, Your select is correct. You should make desired output using some script e.g. PHP.
try this in SQL:
in this Query i save everytime fname in a variable is not equal and at the next row i compare it and return a empty string is it equal. and the same for gtotal.
the cross join is only to initialize the variables.
in this case it is important that the rows are order by fname to ensure that the same name is behind each other
SELECT
invoice.id,
IF(#last_fname = invoice.fname, '', (#last_fname:=invoice.fname)) as fname,
invoice_contents.item,
invoice_contents.price,
invoice_contents.quantity,
invoice_contents.discount,
IF(#last_gtotal = invoice.gtotal, '', (#last_gtotal:=invoice.gtotal)) as gtotal
FROM invoice_contents
INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1
CROSS JOIN ( select #last_fname := '' , #last_gtotal := '' ) AS parameter
ORDER BY invoice.fname;
Sample
MariaDB [bb]> SELECT
-> invoice.id,
-> IF(#last_fname = invoice.fname, '', (#last_fname:=invoice.fname)) AS fname,
-> invoice_contents.item,
-> invoice_contents.price,
-> invoice_contents.quantity,
-> invoice_contents.discount,
-> IF(#last_gtotal = invoice.gtotal, '', (#last_gtotal:=invoice.gtotal)) AS gtotal
-> FROM invoice_contents
-> INNER JOIN invoice ON invoice_contents.invoice_id=1 AND invoice.id=1
-> CROSS JOIN ( SELECT #last_fname:='' , #last_gtotal:='' ) AS parameter
-> ORDER BY invoice.fname;
+----+---------+------------+-------+----------+----------+--------+
| id | fname | item | price | quantity | discount | gtotal |
+----+---------+------------+-------+----------+----------+--------+
| 1 | Brandon | Dextrose | 10.00 | 10 | 5.00 | 860.00 |
| 1 | | Nescaine | 20.00 | 30 | 10.00 | |
| 1 | | Anticavity | 30.00 | 10 | 25.00 | |
+----+---------+------------+-------+----------+----------+--------+
3 rows in set, 1 warning (0.00 sec)
MariaDB [bb]>
I have 2 tables:
S_tbl
====================
id | barcode | qty |
====================
1 | 1234 | 10 |
1 | 111 | 5 |
1 | 1234 | 10 |
K_tbl
=============================
id | barcode | qty | newQty |
=============================
1 | 1234 | 10 | 20 |
I run this query:
SELECT K_Tbl.id, K_Tbl.barcode, K_Tbl.Qty, K_Tbl.NewQty
FROM K_Tbl where K_Tbl.id = 1
UNION ALL
SELECT S_Tbl.id, S_Tbl.barcode, S_Tbl.Qty, '0' as NewQty
FROM S_Tbl where S_Tbl.id = 1
and I get this:
=============================
id | barcode | qty | newQty |
=============================
1 | 1234 | 10 | 20 |
1 | 1234 | 10 | 20 |
1 | 1234 | 10 | 0 |
1 | 111 | 5 | 0 |
How to merge same lines that the result will be like this:
=============================
id | barcode | qty | newQty |
=============================
1 | 1234 | 10 | 20 |
1 | 1234 | 10 | 20 |
1 | 111 | 5 | 0 |
EDIT:
i add the line: 1 | 1234 | 10 | to S_Tbl
what i need to change in the query:
SELECT K_Tbl.id, K_Tbl.barcode, K_Tbl.Qty, K_Tbl.NewQty
FROM K_Tbl where K_Tbl.id = 1
UNION
SELECT S_Tbl.id, S_Tbl.barcode, S_Tbl.Qty, K_Tbl.NewQty
FROM S_Tbl
left join K_Tbl on S_Tbl.id=K_Tbl.id and S_Tbl.barcode=K_Tbl.barcode
where S_Tbl.id = 1
that i'll see
=============================
id | barcode | qty | newQty |
=============================
1 | 1234 | 10 | 20 |
1 | 1234 | 10 | 20 |
1 | 111 | 5 | 0 |
Right now I see:
=============================
id | barcode | qty | newQty |
=============================
1 | 1234 | 10 | 20 |
1 | 111 | 5 | 0 |
SELECT K_Tbl.id, K_Tbl.barcode, K_Tbl.Qty, K_Tbl.NewQty
FROM K_Tbl where K_Tbl.id = 1
UNION
SELECT S_Tbl.id, S_Tbl.barcode, S_Tbl.Qty, K_Tbl.NewQty
FROM S_Tbl
left join K_Tbl on S_Tbl.id=K_Tbl.id and S_Tbl.barcode=K_Tbl.barcode
where S_Tbl.id = 1
SQL FIDDLE
I am sure this Query will work from MS-ACCES
Use UNION instead of UNION ALL - slower, but cares about the duplicates and removes them. Refer to MSDN doc: http://msdn.microsoft.com/en-us/library/office/bb208962(v=office.12).aspx
By default, no duplicate records are returned when you use a UNION
operation; however, you can include the ALL predicate to ensure that
all records are returned. This also makes the query run faster.
And it is also same in Oracle or MSSQL.