merge TWO results of SQL select - mysql

There are two types of data in the same mysql table.
Some data's specific column is set to '1' and another data's column is set to '0' (0 and 1 in the same column)
How can I mix them at once like below? (with select query) - every six '0' row is followed by one '1' row.

Well, you could use window functions for sorting - if you are running MySQL 8.0:
select code, val
from mytable
order by
row_number() over(partition by val order by code)
* (case when val = 0 then 1 else 5 end),
val,
code
The logic is to rank records having the same value ; then simple artithmetics can be used to handle the alternation (5 0s, then 1 1).
Demo on DB Fiddle:
code | val
:----- | --:
data1 | 0
data10 | 0
data11 | 0
data12 | 0
data13 | 0
data2 | 1
data14 | 0
data15 | 0
data3 | 0
data4 | 0
data5 | 0
data8 | 1
data6 | 0
data7 | 0
data9 | 0

I would phrase this as:
order by ( floor(row_number() over (partition by column order by data) - 1) /
(case when column = 1 then 1 else 5 end)
)
),
column,
data

Related

Loop through MySQL table in order. If value is 0 store cell value next to it. If not 0 update to stored value

I've tried a few PROCEDURES and LOOPS, but i'm having trouble getting my results.
I have a table of 19,000 records that looks like this:
id | seq | custom_id
1 | 0 | 123
2 | 0 | 124
3 | 1 | NULL
4 | 0 | 125
5 | 1 | NULL
6 | 2 | NULL
7 | 3 | NULL
My goal is:
id | seq | custom_id
1 | 0 | 123
2 | 0 | 124
3 | 1 | 124-1
4 | 0 | 125
5 | 1 | 125-1
6 | 2 | 125-2
7 | 3 | 125-3
So if seq is 0 it will have a custom ID. if seq is not 0 I would like to get the custom id and concat -seq to the end.
In a Correlated Subquery, we can get the previous (and closest) custom_id value where the seq is 0.
Now, we simply need to use Concat() function to concatenate the previous custom_id value with the current row's seq value, to get the new custom_id.
Try the following query to Select the data (DB Fiddle DEMO):
SELECT
t1.id,
t1.seq,
CASE WHEN t1.seq = 0 THEN t1.custom_id
ELSE CONCAT((SELECT t2.custom_id
FROM your_table AS t2
WHERE t2.id < t1.id AND
t2.seq = 0
ORDER BY t2.id DESC LIMIT 1), '-', t1.seq)
END AS custom_id
FROM your_table AS t1
However, based on your comments, it seems that you are interested in Updating the custom_id column at once. In MySQL, using subquery on the same table (which is being updated as well), is possible via Derived Tables approach.
We determine the modified custom_id for id where seq <> 0 in a sub-select result set (Derived table), and then Join it back to the main table for update.
Try the following for Updating the data (DB Fiddle DEMO):
UPDATE
your_table AS tab
JOIN
(
SELECT
t1.id,
CONCAT((SELECT t2.custom_id
FROM your_table AS t2
WHERE t2.id < t1.id AND
t2.seq = 0
ORDER BY t2.id DESC LIMIT 1),
'-', t1.seq) AS custom_id
FROM your_table AS t1
WHERE t1.seq <> 0
) AS dtab ON dtab.id = tab.id
SET tab.custom_id = dtab.custom_id
WHERE tab.seq <> 0;

MYSQL count only those students with number 1

i would like construct a query to count the number of student who don't have number 0.
assuming i have 2 tables
students
*student_id
*name
Number
*number_id
*student_id
*number
| Student 1 |
1
1
1
1
| Student 2 |
1
1
1
1
| Student 3 |
1
0
1
0
|Student 4 |
0
1
1
1
So the result should be. student without zero = 2
student with zero = 2
This answer assumes that your table has the following structure:
id | value
1 | 1
1 | 1
1 | 1
1 | 1
2 | 1
etc...
SELECT id
FROM yourTable
GROUP BY id
HAVING SUM(CASE WHEN value = 0 THEN 1 ELSE 0 END) = 0
Something like this might return the specified result:
SELECT CONCAT('student without zero = '
, ( SELECT SUM(1)
FROM ( SELECT t.`some_identifier`
FROM `sometable` t
GROUP BY t.`some_identifier`
HAVING SUM(IF(t.`some_col`=0,1,0)) = 0
)
)
, '\n'
, 'student with zero = '
, ( SELECT SUM(1)
FROM ( SELECT z.`some_identifier`
FROM `sometable` z
WHERE z.`some_col` = 0
GROUP BY z.`some_identifier`
)
)
) AS `result`
Just replace some_table with the name of the table, some_col with the name of the column that contains the 1 and 0 values, and some_identifier with the column that contains the Student 1, Student 2, etc. values.
This should return a single row, something like this:
result
--------------------------------
student without zero = 2
student with zero = 2

Mysql Sum Conditional

I'm trying to get the total cost from the table below and if Include_Extra is set, use the value in the corresponding Extra_Seat_Cost column. I'm new to mysql, so any help would be greatly appreciated!
+-----------+-----------------+---------------+
| ID | Cost | Extra_Seat_Cost | Include_Extra |
+-----------+-----------------+---------------+
| 3 | 20 | 15 | 1 |
| 4 | 10 | 5 | 0 |
+----+------+-----------------+---------------+
The result should yield total = $45
You can use SUM with CASE WHEN:
SELECT SUM(Cost + CASE WHEN Include_Extra = 1 --if Include_Extra is bool delete = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
SqlFiddleDemo
I've added COALESCE in case Extra_Seat_Cost can be nullable. number + NULL produces NULL.
If you have grouping column use:
SELECT group_column, SUM(Cost + CASE WHEN Include_Extra = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
GROUP BY group_column;

How to fetch column name which contains 0 value

i have a table t1. which has the following value.
+----+-----+-----+-----+-----+-----+
| t1 | s1 | s2 | s3 | s4 | s5 |
+----+-----+-----+-----+-----+-----+
| 1 | 0 | 1 | 0 | 0 | 2 |
|----|-----|-----|-----|-----|-----|
| 2 | 1 | 0 | 0 | 1 | 2 |
+----+-----+-----+-----+-----+-----+
how to fetch column name. which has the value 0.
I think, you are trying to get the column/field name which has all the rows 0, or which has all its values as 0. For example: column s3 in your case.
One possible way to get the column is following:
You first find the sum of each column. Now if a column has all its values as 0, sum for this column would be zero. Having done that, you use an " if " condition to return only those column whose sum is 0. You could also use "concat_ws" to comma separated those columns, in case you have more than one. So, here is the query. Also, let me draw the table again. Its a bit confusing the way you have drawn.
You table name is t1. It has five columns s1,s2,s3,s4,s5 and a total of two rows. You look to find s3, which has all its values as 0.
# Table t1 #
s1 s2 s3 s4 s5
0 1 0 0 1
1 0 0 1 0
select concat_ws( ',' , if(sum(s1)=0 , 's1' , null) , if(sum(s2)=0 , 's2' , null) , if(sum(s3)=0, 's3' , null) , if(sum(s4)=0, 's4', null), if(sum(s5)=0, 's5', null) ) as empty_column from t1;

combining multiple different sql into one

cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
Is there a way of combing such two sql and return the results as one. Because want to avoid calling to DB everytime. I tried UNION of two statments, but only showing one result.
This is the shortest possible solution in MySQL.
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
SQLFiddle Demo
and this is the CASE version
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName
SQLFiddle Demo