I have the below 2 MYSQL weekly tables:
TableA_Wk1
Id | Price
----------
1 | 1
1 | 2
2 | 5
TableA_Wk2
Id | Price
----------
1 | 2
4 | 2
I can group the price in one table:
SELECT Id, SUM(Price) FROM TableA_Wk1
GROUP BY Id
and get
Id | Price
----------
1 | 3
2 | 5
I would like to also consider the table TableA_Wk2 to get the below:
Id | Price
----------
1 | 5
2 | 5
4 | 2
If i use UNION ALL the result is separated:
SELECT Id, SUM(Price) FROM TableA_Wk1
GROUP BY Id
UNION ALL
SELECT Id, SUM(Price) FROM TableA_Wk2
GROUP BY Id
First get all the <Id,price> tuples from those two tables. Then Use SUM and GROUP BY Id on the resultant table.
SELECT
t.Id,
SUM(t.price)
FROM
(
SELECT Id, Price FROM TableA_Wk1
UNION ALL
SELECT Id, Price FROM TableA_Wk2
) AS t
GROUP BY t.Id
Note
UNION removes duplicates.
UNION ALL doesn't.
First UNION , then GROUP BY :
SELECT t.id,sum(t.price) as price
FROM (
SELECT id,price from TableA_Wk1
UNION ALL
SELECT id,price from TableA_Wk2
) t
GROUP BY t.id
Union the result set and then perform summation to get the summed value.
select sum(price) from (
select price from TableA_Wk1
union all
select price from TableA_Wk1
) as alais
Related
I have the following table in MySQL:
id | item_id | uom | uom1
1 1 kg box
When i select all from the table, it will display one row.
I want to make it to display two row if the uom and uom1 is different.
For example, uom = kg and uom1 = box then result will show
id | item_id | uom
1 1 kg
2 1 box
if both uom and uom1 is same(kg) then only one row will show
id | item_id | uom
1 1 kg
Is it possible to do it using sql? Then i will just loop it at my view.
One simple way of doing it would be
select id, item_id, uom from tbl union
select id, item_id, uom1 from tbl
UNION will by default filter out duplicate lines. There is no need for further "tricks" ...
#Neeraj Wadhwa suggested the second line to be changed to:
select id, item_id, uom1 as uom from tbl
This is possible and will produce the same result, but it is not really necessary. The column names in a UNION construct will be determined by the first select statement anyway.
select
#rownum := #rownum + 1 AS id,
T.*
from (
select `item_id`, `uom`
from Table1
union
select `item_id`, `uom1` as `uom`
from Table1
) T , (SELECT #rownum := 0) r
| id | item_id | uom |
|----|---------|-----|
| 1 | 1 | kg |
| 2 | 1 | box |
DEMO SQL Fiddle
I think the simple union should help here:
select distinct * from (
select id, item, uom1 as uom from stuff
union
select id, item, uom2 as uom from stuff
) as unionised
order by unionised.id
Hre's the fiddle: http://sqlfiddle.com/#!9/ece687/1
I need a query that requires me to return something like below.
data table
--------------------------------
product name | quantity
apple | 3
egg | 2
query expected result
--------------------------------
product name
apple
apple
apple
egg
egg
Thanks for feedback
UPDATE #1
ok my bad the question is unclear. I want to display my result by looping my product by quantity. Probably my expected result on quantity makes some confusion. Therefore, is that possible to loop my record by quantity in mysql?
If you really need this in SQL you can leverage a tally(numbers) table, which you can create and populate with values 1-100 like so
CREATE TABLE tally(n INT NOT NULL AUTO_INCREMENT PRIMARY KEY);
INSERT INTO tally (n)
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n;
Then your query would look
SELECT product_name
FROM data_table d JOIN tally t
ON t.n <= d.quantity
Note: Make sure that you have MAX(n) value in your tally table >= MAX(quantity) in your data_table for this to work properly
Output:
+--------------+
| product_name |
+--------------+
| apple |
| apple |
| apple |
| egg |
| egg |
+--------------+
Here is SQLFiddle demo
the "t_example" table :
id | date
---------------
1 | 2001-05-09
1 | 2005-11-05
1 | 2000-08-19
2 | 2010-10-30
2 | 2002-12-10
2 | 2009-07-29
3 | 2003-02-15
3 | 2012-04-20
I would like to create a view that returns the following result (the max date for each id):
id | date_id
---------------
1 | 2005-11-05
2 | 2010-10-30
3 | 2012-04-20
MySQL don't allow to do a subquery with order by in view, and when I use an other view for the subquery, the group by ignore the order by in the subquery.
The following query returns the expected result:
select id, date
from (select id, date from t_example order by id asc, date_id desc) p
group by p.id
But when I use it in a views it does't work:
view1 (subquery) : select id, date from t_example order by id asc, date_id desc;
view2 : select id, date from view1 group by view1.id;
Is there any other solution?
This should work for you
SELECT id, MAX(date) AS date FROM t_example GROUP BY id;
This is using the AS syntax to keep your column name succinct (otherwise it would be MAX(date))
select id,max(date)
from your_table
group by id
I have two mySQL tables as follows:
[product] table
P_id | Name | Quantity
1 | B | 10
2 | C | 15
3 | A | 8
[attribute] table
P_id | Name | Quantity
1 | Black | 5
1 | Red | 5
2 | Blue | 6
2 | Black | 9
How can I write an SQL query so that it can show the result from the above two tables as follows:
Report:
P_id | Name | Quantity
3 | A | 8
1 | B | 10
1 | Black | 5
1 | Red | 5
2 | C | 15
2 | Black | 9
2 | Blue | 6
These should be sorted on [Name] column, but these should be grouping on P_id column as above. By "grouping" on P_id, I mean "keeping all records with the same P_id next to each other". IS it possible to retrieve as above arrangement using a single SQL query.
SELECT P_id, Name, Quantity FROM (
SELECT P_id, Name, Quantity, Name as parent, 1 as level
FROM product
UNION
SELECT a.P_id, a.Name, a.Quantity, p.Name as parent, 2 as level
FROM attribute a JOIN product p ON a.P_id = p.P_id
) combined ORDER BY parent, level, Name
It should be Union operation, I believe like this:
select * from product union select * from attribute order Name;
Not sure why you wrote you need `group by' since your output is not grouped by any column.
Do you mean that you just need an union of both tables?
You can try this:
Select P_id, Name, Quantity
From product
Union
Select P_id, Name, Quantity
From attribute
Order by 2
SELECT * FROM
(
SELECT P_id, Name , Quantity FROM product
UNION ALL
SELECT P_id, Name , Quantity FROM attribute
) Order by Name
You will have to use grouping my friend. Then you can order on Both of you required columns like this:
SELECT * FROM product UNION SELECT * FROM attribute ORDER BY Name, P_id;
If you want them ordered by P_id and then Name, this should work.
(SELECT P_id, Name, Quantity FROM product)
UNION
(SELECT P_id, Name, Quantity FROM attribute)
ORDER BY P_id, Name;
i have table structure like this
sn | person_id | image_name |
1 | 1 | abc1.jpb
2 | 1 | aa11.jpg
3 | 11 | dsv.jpg
4 | 11 | dssd.jpg
5 | 11 | sdf.jpg
I need distinct person_id newest row as following
2 | 1 | aa11.jjpb
5 | 11 | sdf.jpg
IT is possible ?
SELECT * FROM yourtable GROUP BY person_id ORDER BY sn DESC
Essentially you want to select all records from your table. Then it is grouped by the person_id (limiting the result to 1 per person id)... Ordering by SN decending means that it will return the most recent (highest) sn
Update: (and verified)
SELECT * FROM (SELECT * FROM stackoverflow ORDER BY sn DESC) a GROUP BY person_id ORDER BY sn
SELECT * FROM table GROUP BY person_id HAVING MAX(sn)
EDIT
SELECT f.*
FROM (
SELECT person_id, MAX(sn) as maxval
FROM table GROUP BY person_id
) AS x INNER JOIN table AS f
ON f.person_id = x.person_id AND f.sn = x.maxval;
where table is your table name.
SELECT * FROM table a WHERE a.`id` = ( SELECT MAX(`id`) FROM table b WHERE b.`person_id` = a.`person_id` );
What you are doing inside the parenthesis is selecting the max id for the rows that have that distinct person_id. So for each unique person_id you are getting the most recent entry.