Select Max and Select other column - mysql

I am trying to get the max + 1 value from one column, and all of the values from another column. However, my query does not give any results.
For example,
SectionItemID SectionItem
1 blue
2 red
The query should return
SectionItemID SectionItem
3 blue
red
Heres what I have
SELECT SectionItem,MAX(SectionItemID) + 1 AS SectionItemID FROM Core.SectionItem_Lkup

SELECT SectionItem,
(select MAX(SectionItemID)+1 FROM Core.SectionItem_Lkup) AS SectionItemID
FROM Core.SectionItem_Lkup

Whenever you GROUP BY, you should aggregate the other columns involved.
Mysql does allow to omit aggregation on other colums
MsSQL does not, cause the result is undefined for columns without Aggregation.
Best way is to aggregate other columns. For your szenario, you could use group_concat
SELECT MAX(SectionItemID)+1, Group_concat(SectionItem) FROM tbl
Note: The query does not contain any Group By, because you dont want to group on SectionItemId nor SectionItem. Omiting the Group By and using aggregate-functions will use them on the whole table.
Output:
MAX(SECTIONITEMID)+1 GROUP_CONCAT(SECTIONITEM)
3 blue,red
http://sqlfiddle.com/#!2/353bf3/6

select case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
as SectionItemID
, t2.SectionItem
from Core.SectionItem_Lkup t1
full outer join Core.SectionItem_Lkup t2 on 1 = 1
group by t2.SectionItem
order by
case when t2.SectionItem = 'blue'
then cast(max(t1.SectionItemID) + 1 as varchar(1))
else '' end
desc

Related

Creating new data table on existing one

Hello I've got a question, how (if it possible), I can create new datatables with close same rows but if In column value is in string "/" for example
ID
column_param
column_sym
column_value
column_val2
First
param_test1
ABC
11/12
test
Second
param_test2
CDE
22/11
test
Third
param_test3
EFG
44
teste
4'th
param_test4
HIJ
33/22
test
And here if I have param_test1 and param_test4 and if in this column value has "/" I want to create 2 other rows but if I will not set param_test2 then it stay as it is and everything should be in new datatable. Is any way to create this?
Thank you in advance.
Expected result:
As per Gordon's answer, I'm not sure what should be done with the your ID column.
I've replaced these with row numbers.
Depending on your version of MySQL/MariaDB, the ROW_NUMBER() window function may not be available. Depending on whether IDs in the output are necessary you may be able to simply omit this.
I've assumed the existence of a table called myNumbers which contains a single field num and is populated with positive integers from 1 to whatever you're likely to need.
I've included more in the output that you asked for, which will hopefully help you understand what's going on
SELECT
ROW_NUMBER() OVER (ORDER BY d.ID, n.num) as NewID,
d.ID as OriginalID,
n.num as,
d.column_param,
d.column_sym,
d.column_value as orig_value,
CASE WHEN column_param = 'param_test2' THEN d.column_value
ELSE substring_index(substring_index(d.column_value,'/',n.num),'/',-1) END as split_value,
d.column_val2
FROM
myData d
JOIN myNumbers n on char_length(d.column_value)-char_length(replace(d.column_value,'/','')) >= n.num-1
WHERE
n.num = 1 OR d.column_param <> 'param_test2'
ORDER BY
d.ID,
n.num
See this DB Fiddle (the columns output in a different order than I've specified, but I think that's a DB Fiddle quirk).
If you only want to "split" say param_test1 and param_test4 rows the code above code could be amended as follows:
SELECT
ROW_NUMBER() OVER (ORDER BY d.ID, n.num) as NewID,
d.ID as OriginalID,
d.column_param,
d.column_sym,
n.num,
d.column_value as orig_value,
CASE WHEN column_param NOT IN ('param_test1','param_test4') THEN d.column_value
ELSE substring_index(substring_index(d.column_value,'/',n.num),'/',-1) END as split_value,
d.column_val2
FROM
myData d
JOIN myNumbers n on char_length(d.column_value)-char_length(replace(d.column_value,'/','')) >= n.num-1
WHERE
n.num = 1 OR d.column_param IN ('param_test1','param_test4')
ORDER BY
d.ID,
n.num
I don't know how the id is being set, but you can do what you want using union all:
select column_param, column_sym,
substring_index(column_value, '/', 1) as column_value,
column_val2
from t
union all
select column_param, column_sym,
substring_index(column_value, '/', -1) as column_value,
column_val2
from t
where column_value = '%/%';

MySQL - SELECT until fixed number of rows

I would like to ask for help because I am not able to execute a SELECT statement in MySQL.
The SELECT should return 350 lines, as long as the first lines are filled according to a specific condition and if there is space left, the rest is filled with another condition.
Use expressions in the ORDER BY clause, and a LIMIT clause.
For example:
SELECT ...
FROM mytable t
ORDER
BY IF(( t.foo >= 117 AND t.bar IN (2,3,5) ),0,1)
, IF(( t.foo >= 0 ),0,1)
, IF(( t.bar = 42 ),0,1)
, ...
LIMIT 350
Rows that satisfy the condition t.foo >= 117 AND t.bar IN (2,3,5) will be ordered first. (Because the ORDER BY will operate on the return from the expression. That condition will be evaluated, and the IF function will return either 0 or 1. The subsequent expressions in the ORDER BY clause will be evaluated in the same way.
The LIMIT clause will restrict the number of rows returned, but doesn't guarantee that the query won't return fewer rows e.g. if mytable contains fewer than 350 rows.
You mean like:
SELECT column(s) FROM TABLE WHERE column = 42 LIMIT 350
Or do you want the first line where 'column = 42' and then read 350 lines?
You limit the number of returned rows with LIMIT. You use ORDER BY to say which rows you prefer.
Let's say you are looking for records where col = 'best', but if there are not enough (i.e. 350) you also want rows with col = 'okay':
select *
from mytable
where col in ('best', 'okay')
order by case when col = 'best' then 1 else 2 end
limit 350;

count comma-separated values from a column - sql

I want count the length of a comma separated column
I have use these
(LENGTH(Col2) - LENGTH(REPLACE(Col2,",","")) + 1)
in my select query.
Demo:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 5,Null,Null
query result for first two row is coming correctly.for 1 = 4 ,2 = 3 but for 3rd row it is calculating null value also.
Here is what I believe the actual state of your data is:
id | mycolumn
1 2,5,8,60
2 4,5,1
3 NULL
In other words, the entire value for mycolumn in your third record is NULL, likely from doing an operation involving a NULL value. If you actually had the text NULL your current query should still work.
The way to get around this would be to use COALESCE(val, "") when handling the NULL values in your strings.
Crude way of doing it is to replace the occurances of ',Null' with nothing first:-
SELECT a.id, (LENGTH(REPLACE(mycolumn, ',Null', '')) - LENGTH(REPLACE(REPLACE(mycolumn, ',Null', ''),",","")) + 1)
FROM some_table a
If the values refer to the id of rows in another table then you can join against that table using FIND_IN_SET and then count the matches (assuming that the string 'Null' is not an id on that other table)
SELECT a.id, COUNT(b.id)
FROM some_table a
INNER JOIN id_list_table b
ON FIND_IN_SET(b.id, a.mycolumn)
GROUP BY a.id

WHERE clause in SSRS expression for max function

I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result:

Creating a third alias from the remainder of two aliases in MYSQL?

Is it possible to create a third alias from the remainder of two aliases in MYSQL?
Here is my query:
SELECT DISTINCT
workcards.task,
workcards.program,
workcards.zone,
workcards.CYLcomp,
workcards.CYLint,
workcards.task,
workcards.CYLcomp + workcards.CYLint AS nextdue,
workcards.wcnumber,
(SELECT airtimes.total_cycles
FROM airtimes
WHERE airtimes.nnumber='xxxxx'
ORDER BY airtimes.total_cycles DESC LIMIT 1) AS current_cycle
FROM workcards
WHERE basis LIKE '%CYL%'
AND nnumber='xxxxx'
ORDER BY CASE nextdue
WHEN 0 THEN 1 ELSE -1 END ASC, nextdue ASC
What I want to do is create a third alias which would be the difference of the 'nextdue' minus 'current_cyle' aliases? Something like:
nextdue - current_cycle AS remainder?
Sorry if noob or whatever.
Thanks
You cannot use calculations results aliases to use with another calculations in select part (http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html). But you can get what you want by rebuilding your query so it will use joins and grouping:
SELECT DISTINCT
w.*,
w.CYLcomp + w.CYLint AS nextdue,
MAX(a.total_cycles) AS current_cycle
w.CYLcomp + w.CYLint - MAX(a.total_cycles) AS reminder
FROM workcards w
JOIN airtimes a ON a.nnumber = w.nnumber
WHERE w.basis LIKE '%CYL%' AND w.nnumber='xxxxx'
GROUP BY w.nnumber
ORDER BY CASE nextdue WHEN 0 THEN 1 ELSE -1 END ASC, nextdue ASC