Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I list the data with id x first and then followed by numerical order?
For instance, I have data like this:
0,
1,
1,
0,
1,
2,
56,
4
and I want the following result:
1,
1,
1,
0,
0,
2,
4,
56
In addition, I want the order for items with id 1 to be a column x, while the order for items with id not 1 to be a column y.
You can do this with conditionals in the order by clause. Here is a simple way in MySQL:
order by (id = 1) desc, id;
The expression id = 1 evaluates to 1 when the id takes on the specified value. It evaluates to 0 otherwise.
The order by is saying to order by this expression first, and then by the id.
EDIT:
If you want to order everything else by another column and the columns are of the same type:
order by (id = 1) desc,
(case when id = 1 then x else y end)
Otherwise, you can split it:
order by (id = 1) desc,
(case when id = 1 then x end),
(case when id = 1 then NULL else y end)
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 11 months ago.
Improve this question
I expected that this query will not output 0 values, but it does. I thought that and (...) > 0 will not output 0 values. So how I can prevent the output of 0 values?
select lot.*, sum(movement.quantity) as value
from lot
left join lot_movement as movement on lot.id = movement.lot_id
where lot.item_id = 8 and movement.storage_id = 3
and (select sum(lot_movement.quantity)
from lot_movement
where lot_movement.lot_id = lot.id
) > 0
group by lot.id;
I tried to add and sum(lot_movement.quantity) \> 0, but this gives error invalid use of group function.
lots in database
lot_movements in database
output with 0 values
I see that
and (select sum(lot_movement.quantity)
from lot_movement
where lot_movement.lot_id = lot.id
group by lot_movement.lot_id) > 0
is redundant. It doesn't affect the result.
Your query doesn't give the expected result because you're filtering by lot.item_id = 8 and movement.storage_id = 3 in the where clause, but you're not applying that same filtering in the subselect.
I'm not exactly sure what you're trying to achieve, but I suspect adding a having clause instead of the subselect solves your problem:
select lot.id, sum(movement.quantity) as value
from lot
left join lot_movement as movement on lot.id = movement.lot_id
where lot.item_id = 8 and movement.storage_id = 3
group by lot.id
having sum(movement.quantity) > 0
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 2 years ago.
Improve this question
I have a column itemId that contains unique IDs in the format US001, US002, US003 etc.
The trailing numbers of these IDs are not consecutive as rows may have been deleted.
I am looking for a way to find the first number that DOES NOT EXIST in my column.
Example 1:
Column: US001, US002, US004, US006.
Expected result: 3.
Example 2:
Column: US001, US002, US003, US004, US007.
Expected result: 5.
Example 3:
Column: US001, US002, US003, US004, US005.
Expected result: 6.
I tried the following but this returns NULL or no results (also no error):
SELECT MIN((RIGHT(i.itemId, 3)) + 1) AS nextAvailableId
FROM items i
WHERE RIGHT(i.itemId, 3) NOT IN
(
SELECT * FROM
(
SELECT RIGHT(i2.itemId, 3) AS itemNo
FROM items i2
) AS x
)
Can anyone please help me with this ?
Thanks,
Tim
Here is one option using window functions and string functions:
select min(id) + 1 as nextAvailableId
from (
select substr(itemid, 2) as id,
lead(substr(itemid, 2)) over(order by substr(itemid, 2)) as lead_id
from mytable t
) t
where lead_id > id + 1 or lead_id is null
In MySQL 5.x, where string functions are not available, you can use a subquery:
select min(substr(itemid, 2)) + 1 as nextAvailableId
from mytable
where not exists (
select 1
from mytable t1
where substr(t1.itemid, 2) = substr(t.itemid, 2) + 1
)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a Table with Column Headings:
ID Student_Name
Roll_Number
Subject1MarksObtained
Subject1TotalMarks
Subject2MarksObtained
Subject2TotalMarks
Subject3MarksObtained
Subject3TotalMarks
Subject4MarksObtained
Subject4TotalMarks
I want to write a query to output the results for individual student who have pass at least three of the subjects.
Without seeing the data, lets make some assumptions:
A pass is awarded for a subject if the marks obtained for that subject are equal to or more than 50% of the total marks available for that subject.
The name of the table is called Enrollment
To return a list of students who have passed at least 3 subjects we can use a query similar to the following:
This solution uses CASE to evaluate a 1 for a pass and a 0 for fail for each subject, then we sum those results and only return rows that have a score of 3 or more.
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( CASE WHEN (Subject1MarksObtained / Subject1TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject2MarksObtained / Subject2TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject3MarksObtained / Subject3TotalMarks) >= 0.5 THEN 1 ELSE 0 END
+ CASE WHEN (Subject4MarksObtained / Subject4TotalMarks) >= 0.5 THEN 1 ELSE 0 END
) >= 3
There are different way to approach this, but this query is simple to read and gets the job done.
If you are querying an access table, then CASE WHEN is not supported but you can use IIF or SWITCH to achieve the same results:
SELECT ID, Student_Name, Roll_Number
FROM Enrollment
WHERE
( IIF( (Subject1MarksObtained / Subject1TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject2MarksObtained / Subject2TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject3MarksObtained / Subject3TotalMarks) >= 0.5, 1, 0)
+ IIF( (Subject4MarksObtained / Subject4TotalMarks) >= 0.5, 1, 0)
) >= 3
Let's instead start by fixing your broken schema. A normalised design might look somewhat as follows:
Student
ID
Student_Name
Roll_Number
Results
StudentID
Subject
Mark
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have 4 columns one has id and other columns (A,B,C) has binary values.
I want the results like, columns(A,B,C) which has value = 1.Then particular Column name must be displayed in new column(D) with comma separated values.
I want the results like below. Can anyone please help me?
Id A B C D
1 1 1 0 A,B
2 0 1 0 B
3 0 1 0 B
4 1 0 1 A,C
5 1 0 1 A,C
A simple CASE expression will do it:
SELECT *,
D =
STUFF((
CASE
WHEN A = 1 THEN ',A'
ELSE ''
END +
CASE
WHEN B = 1 THEN ',B'
ELSE ''
END +
CASE
WHEN C = 1 THEN ',C'
ELSE ''
END
), 1, 1, '')
FROM tbl
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't seem to find/create a working query which will update the x column with an extra zero when value after the dot is below .10 - I've got column x with values;
7.3
5.3
0.14
I am trying to update the column so when the value after the dot is below .10 it adds a zero, above example will eventually need to look like;
7.03
5.03
0.14
Is there a way I can do this using an mysql UPDATE-query?
Many thanks.
First of all, 0.03 is not equal to 0.30, so i believe what you want is 7.30 and not 7.03. To do this, you need to alter your table column to display two decimal digits:
my_magic_column float(3,2)
where 3 is the total number of digits and 2 is the number of decimals to show.
I don't really get idea behind such operation but it's pretty simple.
SELECT CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END as result
FROM table_name
UPDATE table_name SET id = CASE
WHEN INSTR(id, '.') = 0 THEN id
ELSE CASE
WHEN CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED) < 10 THEN
CONCAT(SUBSTRING_INDEX(id, '.', 1), '.0', CAST(SUBSTRING_INDEX(id, '.', -1) AS UNSIGNED))
ELSE id
END
END
But before using this, think once again about your schema and what are you trying to do. I don't think you'll need this then.
Looks like what you want to do is:
update TABLE set YOUR_NUMBER = (FLOOR(NUMBER) + (NUMBER-FLOOR(NUMBER))/10)
where FLOOR(NUMBER*10) = NUMBER*10
FLOOR(NUMBER) - take the integer part and add to it:
(NUMBER-FLOOR(NUMBER))/10 - the fraction part of the number - divided by 10
The where condition checks that there's only one digit after the dot