I have mysql query :-
select id,CustomerName, Scenario,StepNo,InTransit,IsAlef,Min(RunNo) as run,IsDominant,IsActive from RequestInfo
group by CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo
having concat(CustomerName,Scenario,StepNo,InTransit,IsAlef,count(RunNo)) in
(select concat(CustomerName,Scenario,StepNo,InTransit,IsAlef,Min(Total)) from
(select CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo, count(RunNo) Total from RequestInfo
group by CustomerName, Scenario,StepNo,InTransit,IsAlef,RunNo
) b
group by CustomerName, Scenario,StepNo,InTransit,IsAlef);
which gives output :-
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
| id | CustomerName | Scenario | StepNo | InTransit | IsAlef | run | IsDominant | IsActive |
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
| 1 | Cleartrip | SearchFlight | 1 | No | No | 1 | NULL | NULL |
| 327 | HotStar | SearchTv | 1 | No | No | 1 | NULL | NULL |
| 836 | NDTV | LiveTv | 1 | No | No | 2 | NULL | NULL |
| 1090 | YATRA | SearchFlight | 1 | No | No | 2 | NULL | NULL |
+------+--------------+--------------+--------+-----------+--------+------+------------+----------+
i want to set the last two columns as "Yes" only for the rows which are been shown in the above result.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I had created two Tables 'tbl_book_info' and 'tbl_books' (table descriptions
are below) in MySQL Database
what should I change in my SQL query
I had used subquery inside WHERE clause.
All Records of 'tbl_books'-
+-------+-------+--------+
| accid | accno | status |
+-------+-------+--------+
| 10001 | 101 | I |
| 10001 | 102 | I |
| 10001 | 103 | A |
| 10002 | 101 | A |
| 10002 | 102 | A |
| 10002 | 103 | I |
| 10002 | 104 | I |
| 10002 | 105 | I |
| 10003 | 101 | A |
| 10003 | 102 | A |
| 10003 | 103 | A |
| 10003 | 104 | I |
| 10003 | 105 | I |
| 10004 | 101 | A |
| 10004 | 102 | I |
| 10004 | 103 | A |
| 10004 | 104 | A |
| 10004 | 105 | A |
| 10005 | 101 | A |
| 10005 | 102 | A |
| 10005 | 103 | A |
| 10005 | 104 | A |
| 10005 | 105 | A |
+-------+-------+--------+
23 rows in set (0.00 sec)
All Records of 'tbl_book_info'-
+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+
| b_acc_id | b_name | b_qty | b_type | b_auth1 | b_auth2 | b_pub | b_pages | b_rack | b_price | b_about |
+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+
| 10001 | Java | 3 | Programming | lala | - | kallo | 800 | 1 | 799.00 | - |
| 10002 | Cpp | 5 | Programming | Kallo | - | Mehta group | 400 | 2 | 300.00 | - |
| 10003 | VB.net | 5 | Programming | lalaji | - | amam co. | 479 | 3 | 100.00 | - |
| 10004 | DBMS | 5 | prog | lalal | - | kallo | 888 | 3 | 499.00 | - |
| 10005 | computer network | 5 | Networking | Mirabai | - | kabirdas | 789 | 2 | 800.00 | - |
+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+
desc tbl_books;
+--------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+ | accid | int(5) | NO | PRI | 0 | | | accno | int(3) | NO | PRI | 0 | | | status | varchar(1) | YES | | A | |
+--------+------------+------+-----+---------+-------+
desc tbl_book_info;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| b_acc_id | int(5) | NO | PRI | NULL | auto_increment |
| b_name | varchar(50) | NO | | NULL | |
| b_qty | int(2) | NO | | NULL | |
| b_type | varchar(30) | NO | | NULL | |
| b_auth1 | varchar(50) | NO | | NULL | |
| b_auth2 | varchar(50) | YES | | NULL | |
| b_pub | varchar(50) | NO | | NULL | |
| b_pages | int(4) | NO | | NULL | |
| b_rack | int(5) | NO | | NULL | |
| b_price | decimal(6,2) | NO | | NULL | |
| b_about | text | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Here tbl_books.accid REFERS tbl_book_info.b_acc_id
My query is:
select b_name, b_qty, b_acc_id , (count(*)) Issued, (count(*)) Available
From tbl_book_info
where tbl_book_info.b_acc_id in (select accid from tbl_books
where status = 'I'
GROUP BY status) ;
I want to perform join between the tables 'tbl_book_info' and 'tbl_books' to print the Result as
Desired Output:
+--------+-------+----------+--------+-----------+
| b_name | b_qty | b_acc_id | Issued | Available |
+--------+-------+----------+--------+-----------+
| Java | 3 | 10001 | 2 | 1 |
+--------+-------+----------+--------+-----------+
| Cpp | 5 | 10002 | 3 | 2 |
+--------+-------+----------+--------+-----------+
...(and more)
Output Came:
+--------+-------+----------+--------+-----------+
| b_name | b_qty | b_acc_id | Issued | Available |
+--------+-------+----------+--------+-----------+
| Java | 3 | 10001 | 4 | 4 |
+--------+-------+----------+--------+-----------+
If you want to do a Join, then you should do it, not doing a strange subquery in a where. You can do a Join and a conditional case to sum in two different columns the Issued and the Avalilables
Select a.b_name, a.b_qty, a.b_acc_id , sum(case
when b.status='I' then 1
else 0
end) as Issued,
sum(case
when b.status='A' then 1
else 0
end) as Available
From tbl_book_info a left join tbl_books b on a.b_acc_id=b.accid
group by a.b_name, a.b_qty, a.b_acc_id
In MySQL, you can take a short-cut for this type of query:
select bi.b_name, bi.b_qty, bi.b_acc_id,
sum( b.status = 'I' ) as Issued,
sum( b.status = 'A' ) as Available
from tbl_book_info bi left join
tbl_books b
on bi.b_acc_id = b.accid
group by bi.b_name, bi.b_qty, bi.b_acc_id;
MySQL treats boolean expressions as integers in a numeric context (such as SUM()). Also note that the table aliases are abbreviations for the table name.
With an index on books(acc_id, status), this might be faster using a subqueries:
select bi.b_name, bi.b_qty, bi.b_acc_id,
(select count(*)
from tbl_books b
where b.accid = bi.b_acc_id and
b.status = 'I'
) as Issued,
(select count(*)
from tbl_books b
where b.accid = bi.b_acc_id and
b.status = 'A'
) as Available
from tbl_book_info bi ;
The performance gain is by avoiding the work for the outer group by (typically a sort). The index can be used instead.
I have not tried this solution yet, but I can say that you need to do Group By accid column of tbl_books table. So please make following change in your query:
select b_name, b_qty, b_acc_id , (count(*)) Issued, (count(*)) Available
From tbl_book_info
where tbl_book_info.b_acc_id in (select accid from tbl_books
where status = 'I'
GROUP BY accid) ;
I have a table where the data of measured temperature and humidity is stored. Records with temperature have the field meas_kind set to "T", and for humidity it is set to "H".
mysql> describe meteo;
+-----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| meas_time | timestamp | YES | | NULL | |
| room | varchar(10) | NO | | NULL | |
| meas_kind | char(1) | NO | | NULL | |
| value | double | NO | | NULL | |
+-----------+---------------------+------+-----+---------+----------------+
mysql> select * from meteo;
+----+---------------------+------+-----------+-------+
| id | meas_time | room | meas_kind | value |
+----+---------------------+------+-----------+-------+
| 35 | 2017-05-24 16:51:47 | 123 | T | 22.61 |
| 36 | 2017-05-24 16:51:47 | 123 | H | 36.93 |
| 37 | 2017-05-24 16:51:51 | 123 | T | 22.61 |
| 38 | 2017-05-24 16:51:51 | 123 | H | 36.94 |
| 39 | 2017-05-24 16:51:56 | 123 | T | 22.61 |
| 40 | 2017-05-24 16:51:56 | 123 | H | 36.94 |
+----+---------------------+------+-----------+-------+
Temperature and humidity are measured in the same time, so I want this table to be like this:
+---------------------+------+-------+-------+
| meas_time | room | Temp | Humid |
+---------------------+------+-------+-------+
| 2017-05-24 16:51:47 | 123 | 22.61 | 36.93 |
| 2017-05-24 16:51:51 | 123 | 22.61 | 36.94 |
| 2017-05-24 16:51:56 | 123 | 22.61 | 36.94 |
+---------------------+------+-------+-------+
I've tried to make this query, but it gives me an error:
ERROR 1242 (21000): Subquery returns more than 1 row
Please help me to get a correct result.
select
m.meas_time,
m.room,
(select value from meteo m1 where m1.meas_kind='T' and m.meas_time=m1.meas_time) as Temp,
(select value from meteo m2 where meas_kind='H' and m.meas_time=m2.meas_time) as Humid
from meteo m
join meteo m1
on m.meas_time=m1.meas_time
and m.room=m1.room
join meteo m2
on m.meas_time=m2.meas_time
and m.room=m2.room
group by m.room, m.meas_time;
You are overcomplicating things. A simple pivot query can give you the results you want.
SELECT
meas_time,
room,
MAX(CASE WHEN meas_kind = 'T' THEN value END) AS Temp,
MAX(CASE WHEN meas_kind = 'H' THEN value END) AS Humid
FROM meteo
GROUP BY meas_time, room
I have two column with the same name in different tables.
I want to join them into one column in a view.
Here my first table stocks:
+----------+------------+------------+---------+----------------+--------+
| stock_id | stock_cost | stock_left | item_id | purchasedtl_id | trx_id |
+----------+------------+------------+---------+----------------+--------+
| 1 | 1000 | 0 | 1 | 1 | 1 |
| 2 | 1000 | 5 | 1 | 2 | 2 |
| 3 | 1000 | 1 | 1 | 3 | 4 |
+----------+------------+------------+---------+----------------+--------+
Second table stocks_out
+-------------+----------------+--------------+---------+----------+------------+--------+
| stockout_id | stockout_price | stockout_qty | item_id | stock_id | saledtl_id | trx_id |
+-------------+----------------+--------------+---------+----------+------------+--------+
| 1 | 2000 | 1 | 1 | 1 | 1 | 3 |
+-------------+----------------+--------------+---------+----------+------------+--------+
And I want to join them to be like this trx_id, trx_no, trx_closetime, trx_type stock_id, stock_cost, stockout_id, stock_out_cost, stockout_price, item_id
item_id is the field I want to join in one column.
The current Query is :
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price` from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the current result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+
Found it guys.
I just need to add the following query as the column
COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id`
So the query will be like
select `transactions`.`trx_id` AS `trx_id`,`transactions`.`trx_no` AS `trx_no`,`transactions`.`trx_closetime` AS `trx_closetime`,`transactions`.`trx_type` AS `trx_type`,`stocks`.`stock_id` AS `stock_id`,`stocks`.`stock_cost` AS `stock_cost`,`stock_out`.`stockout_id` AS `stockout_id`,`stock_out`.`stockout_price` AS `stockout_price`, COALESCE(`stocks`.`item_id`, `stocks_out`.`item_id`) AS `item_id from ((`transactions` left join `stocks` on(`stocks`.`trx_id` = `transactions`.`trx_id`)) left join `stock_out` on(`stock_out`.`trx_id` = `transactions`.`trx_id`)) order by `transactions`.`trx_closetime`;
And the result:
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| trx_id | trx_no | trx_closetime | trx_type | stock_id | stock_cost | stockout_id | stockout_price | item_id |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
| 1 | 02002-02-170415-001 | 2017-04-15 19:40:03 | 2 | 1 | 1000 | NULL | NULL | 1 |
| 2 | 02002-02-170415-002 | 2017-04-15 19:40:13 | 2 | 2 | 1000 | NULL | NULL | 1 |
| 3 | 02002-01-170415-001 | 2017-04-15 19:40:57 | 1 | NULL | NULL | 1 | 2000 | 1 |
| 4 | 02002-02-170415-003 | 2017-04-15 19:41:14 | 2 | 3 | 1000 | NULL | NULL | 1 |
+--------+---------------------+---------------------+----------+----------+------------+-------------+----------------+---------+
i have two tables,
1.Orders,
2.Items.
1.Orders Skeleton
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2.Items Skeleton
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| order_id | int(11) | NO | MUL | NULL | |
| name | varchar(50) | NO | | NULL | |
| price | int(11) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
Records :
1.Orders Table with Data
+----+--------+
| id | name |
+----+--------+
| 1 | Order1 |
| 2 | Order2 |
| 3 | Order3 |
+----+--------+
2.Items Table with Data
+----+----------+---------+-------+
| id | order_id | name | price |
+----+----------+---------+-------+
| 1 | 1 | Mobile1 | 25000 |
| 2 | 1 | Mobile2 | 30000 |
| 3 | 1 | Mobile3 | 6500 |
| 4 | 2 | Mobile4 | 10000 |
+----+----------+---------+-------+
I want to get items which are having same order_id,
Select id[Orders], order_id[items], name[items]1, name[items]2, name[items]3...
Suggestions Pls...
It seems that you only need to access table items to get what you need.
SELECT order_id, GROUP_CONCAT( name SEPARATOR ', ') item_names
FROM items
GROUP BY order_id;
I get solution for only known number of fields....
Row to Column:
STEP : 1
select #orderID:=order_id,
#name:=GROUP_CONCAT(name) item_names,
#price:=GROUP_CONCAT(price) item_price
from item group by order_id limit 1;
OUTPUT :
+----------+-------------------------+------------------+
| order_id | item_names | item_price |
+----------+-------------------------+------------------+
| 1 | Mobile1,Mobile2,Mobile3 | 25000,30000,6500 |
+----------+-------------------------+------------------+
STEP : 2
select #orderID,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 1),',',-1) as Name1,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 1),',',-1) as Price1,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 2),',',-1) as Name2,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 2),',',-1) as Price2,
SUBSTRING_INDEX(SUBSTRING_INDEX(#name, ',', 3),',',-1) as Name3,
SUBSTRING_INDEX(SUBSTRING_INDEX(#price, ',', 3),',',-1) as Price3;
OUTPUT :
+----------+---------+--------+---------+--------+---------+--------+
| #orderID | Name1 | Price1 | Name2 | Price2 | Name3 | Price3 |
+----------+---------+--------+---------+--------+---------+--------+
| 1 | Mobile1 | 25000 | Mobile2 | 30000 | Mobile3 | 6500 |
+----------+---------+--------+---------+--------+---------+--------+
I wanted to make a query with Rails, something like this:
filters = Filter.joins(:category_filters).where("category_id IN (?)", params[:categories]).group("filters.id")
And the MySQL statement that is making is this:
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id
At first sight, this query is ok, but when I look the results, its wrong. Let me explain you.
First, this is a query to the table filters:
select * from filters;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
| 3 | xyz | 0 | 0 | NULL | NULL |
| 4 | 123a | 0 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
Second, this is a query to the table category_filters:
select * from category_filters;
+----+-----------+-------------+------------+------------+
| id | filter_id | category_id | created_at | updated_at |
+----+-----------+-------------+------------+------------+
| 1 | 1 | 1 | NULL | NULL |
| 2 | 2 | 1 | NULL | NULL |
| 3 | 1 | 9 | NULL | NULL |
| 4 | 2 | 9 | NULL | NULL |
| 5 | 1 | 4 | NULL | NULL |
| 6 | 3 | 4 | NULL | NULL |
+----+-----------+-------------+------------+------------+
And now, the query generated by Rails (the first query):
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
Why is this happening?
But now, this is similar query, instead of using IN I used OR, like this:
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_filters.category_id=9 or category_filters.category_id=4) GROUP BY filters.id;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
| 3 | xyz | 0 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
What is happening?
In your WHERE clause, try this (assuming category_id is a number):
category_id IN (9,4)
else this (assuming category_id is a string)
category_id IN ('9','4')
instead of this (in your original query)
category_id IN ('9,4')
If you just do
where(:category_id => params[:categories]
Rails will create the proper SQL syntax for you