Oracle query is-
select ROW_NUMBER () OVER(order by shortunitdesc) as SLNO,
om.shortunitdesc,oc.operparam as Parameter,
oc.tagno,oc.severity,lowlimit,highlimit, oc.operrange, IMPLICATION, DURATION from oeconfig oc, oeunitmaster om
where oc.unitcode = om.unitcode;
I want to convert in mysql, so I have tried
select #i:=#i+1 as slno, om.shortunitdesc, oc.operparam as Parameter,
oc.tagno,oc.severity,oc.lowlimit,oc.highlimit,oc.operrange , IMPLICATION, DURATION
from oeconfig oc oeunitmaster om , (select #i := 0)
where oc.unitcode = om.unitcode
order by shortunitdesc;
but it gives an error- Every derived table must have its own alias
select #i:=#i+1 as slno, om.shortunitdesc, oc.operparam as Parameter,
oc.tagno,oc.severity,oc.lowlimit,oc.highlimit,oc.operrange , IMPLICATION, DURATION
from oeconfig oc, oeunitmaster om, (select #i := 0) t
where oc.unitcode = om.unitcode
order by shortunitdesc;
Related
I am using MySQL work bench 8 :
I am not able to create this view as I am getting following error:
View's SELECT contains a variable or parameter
Here is my view:
Create view history as
select ShippedDate, round(previous_operation) as DayEnd, DayStart ,Reorderunits,Quantity,reorderlevel from (
select
y.*
, #prev AS previous_Operation
, #prev := DayStart
from
ExpectedHistory y
, (select #prev:=NULL) vars
order by ShippedDate desc
Note : #prev holds an integer value
If you want the previous operation in MySQL 8+, use lead():
create view history as
select ShippedDate, DayStart,
lead(day_start) over (partition by shippeddate) as as dayend,
Reorderunits, Quantity, reorderlevel,
from ExpectedHistory eh;
I have the following table on my DataBase:
TimeRank(user-id, name, time)
I would like to order the table by time and get the position of an specific ID on the table, for example:
The user nº 68 is on the 3rd position.
I only need to do a query that returns the position of the user.
MySQL don't have the function row_number, so I don't know how to do it.
SELECT x.user-id,
x.name,
x.time,
x.position
FROM (SELECT t.user-id,
t.name,
t.time,
#rownum := #rownum + 1 AS position
FROM TABLE TimeRank t
JOIN (SELECT #rownum := 0) r
ORDER BY t.time) x
WHERE x.user-id = 123
Alternative:
SELECT user-id,
(SELECT COUNT(*) FROM TimeRank WHERE time <= (SELECT time FROM TimeRank WHERE user-id = 123)) AS position,
time,
name
FROM TimeRank
WHERE user-id = 123
You can generate a position column with a variable
set #pos=0;
select pos,user_id
from (select #pos:=#pos+1 pos,user_id from TimeRank order by time) s
where user_id=68;
If indexing is a concern, you can add a column to your table and update it with
set #pos=0;
update TimeRank set position=(#pos:=#pos+1) order by time;
I have select query that selects student scores and ranks them from the highest to the lowest, this mysql query works the first time, but when I run it the second time on phpmyadmin, it returns NULL where it is supposed to show the rank of the student, below is the mysql query;
SELECT #rownum := #rownum + 1 AS rank
, student_name
, avga
FROM `averaga`
WHERE class="Form 1A"
ORDER
BY avga DESC
here is a part snapshot of the results from query results
You need to initialize the variable. I usually do this in query itself:
SELECT (#rownum := #rownum + 1) AS rank, student_name, avga
FROM `averaga` a CROSS JOIN
(SELECT #rownum := 0) params
WHERE class = 'Form 1A'
ORDER BY avga DESC;
I'm developing a query on phpmyadmin mysql in which I intend to display the running balance of a column say 'CurrentBalance'
The running balance of this column depends when the Activity is a deposit (+), a withdraw (-) , bet (-), payout (+)
What I come up is this
SELECT CONCAT("Trans ID ",`TransactionID`) AS ID,
`DateTime`,`Type` AS Activity, `Amount`, 0 AS Payout,
CASE WHEN (SELECT Activity) = "deposit" THEN `Amount`+ `playerdb`.`CurrentBalance`
ELSE CASE WHEN (SELECT Activity) = "withdraw" OR (SELECT Activity) = "bet"
THEN CASE WHEN (SELECT Payout) >0 THEN (SELECT Payout) + `playerdb`.`CurrentBalance`
ELSE `Amount` - `playerdb`.`CurrentBalance` END END END AS CurrentBalance
FROM `transactiondb` LEFT JOIN `playerdb` ON
`transactiondb`.`PlayerID` = `playerdb`.`PlayerID`
WHERE `transactiondb`.`PlayerID`=10078 UNION ALL
SELECT CONCAT("Bet ID ",`BetID`),`DateTime`,"Bet", `BetAmount`,`Payout`, (SELECT CurrentBalance) FROM `betdb` WHERE `PlayerID`=10078 ORDER BY `DateTime`
The Idea
http://postimg.org/image/x3fsxq2qz/
Doing the (SELECT CurrentBalance) on the 2nd SELECT statement yields this error
1054 - Unknown column 'CurrentBalance' in 'field list'
I need to get the CurrentBalance of the previous record so I tried using the alias
Is that possible?
This is a "teach a man to fish..." answer, since your question is not yet totally clear to me.
In general you can access the previous row with variables.
Have a look at this example:
SELECT
t.*
, #running_total := IF(#foo != foo, 0, #running_total + bar)
, #foo := foo
FROM a_table t
, (SELECT #running_total := 0, #foo := NULL) variable_initialization_subquery
ORDER BY foo;
As the subquery alias suggests, here
, (SELECT #running_total := 0, #foo := NULL) variable_initialization_subquery
we initialize the variables.
We ORDER BY foo because there's no reliable order in data in a database when you don't specify it clearly.
Then the SELECT clause is considered with one column after another. Here the order of the columns is important, too.
First we do this:
, #running_total := IF(#foo != foo, 0, #running_total + bar)
This calculates a running total for every foo. When the foo changes, the running total is reset to 0. The IF() function works like IF(<boolean condition>, <then>, <else>).
Here the variable #foo still has the value of the previous row. The value of the current row is assigned in this line:
, #foo := foo
I hope you get the idea, feel free to ask if anything is unclear. Oh, and you don't need those selects in the case...when... parts.
Just guessing... giving the man a fish - might be the wrong fish
SELECT CONCAT("Trans ID ",TransactionID) ID
, DateTime
, Type Activity
, Amount
, 0 Payout
, CASE WHEN Activity = "deposit"
THEN Amount + playerdb.CurrentBalance
WHEN Activity IN("withdraw","bet")
THEN
CASE WHEN Payout >0
THEN Payout + playerdb.CurrentBalance
ELSE Amount - playerdb.CurrentBalance
END
END CurrentBalance
FROM transactiondb
LEFT
JOIN playerdb
ON transactiondb.PlayerID = playerdb.PlayerID
WHERE transactiondb.PlayerID = 10078
UNION ALL
SELECT CONCAT("Bet ID ",BetID)
, DateTime
, "Bet"
, BetAmount
, Payout
, CurrentBalance
FROM betdb
WHERE PlayerID = 10078
ORDER
BY DateTime;
I have a list of players. The players are sorted by points. What I'd like to know is how do I get the ranking number of a CERTAIN player?
This is my code so far (which doesn't work because it seems to have some bugs):
$rank = mysql_query (SET #rank := 0;
SELECT *, #rank := #rank + 1
FROM ava_users
WHERE id = '".$id."'
ORDER BY points DESC);
$rank_res = mysql_fetch_array($rank);
When I try to use my query I get an error message:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Users/***/Documents/Arcades/Arc_development/arc_projects/***/arc_dev_website/arc_offline/includes/profile/profile_main.inc.php
$rank = mysql_query (
"SELECT a.*,
(
SELECT COUNT(1)
FROM ava_users b
WHERE (b.points, b.id) >= (a.points, a.id)
) AS rank
FROM ava_users a
WHERE a.`user` = '$id'"
);
Try this:
SELECT `user`, rank
FROM (
SELECT `user`, ( #rank := #rank + 1 ) as rank
FROM ava_users, ( select (#rank := 0 ) ) rnk
ORDER BY points DESC
) ranks
WHERE `user` = '".$id."'
user is a key word, therefore use user in order to check parameters equality.
Also, mysql_query can only execute 1 query at a time.