I'm trying to select all records in a table, but hide duplicate rows. So if 2 rows are completely the same (except for the auto increment ID of course), only 1 should be shown.
I thought this had to be done with distinct, but it still give me duplicate rows.
SELECT DISTINCT *
FROM tbllulog
WHERE lulogluserial = $commandlu
ORDER BY `tbllulog`.`tbllulogid` DESC
I also tried this:
SELECT DISTINCT lulogtimestamp,
lulogmoveemployee,
lulogsource,
lulogaction,
lulogluoutput0status,
lulogluinput0status
FROM tbllulog
WHERE lulogluserial = $commandlu
ORDER BY `tbllulog`.`tbllulogid` DESC
But this also give me duplicates
Anyone can point me out what i'm missing?
thanks!
Use DISTINCT and GROUP BY and add the tbllulogid to the SELECT
SELECT DISTINCT tbllulogid,
lulogtimestamp,
lulogmoveemployee,
lulogsource,
lulogaction,
lulogluoutput0status,
lulogluinput0status
FROM tbllulog
WHERE lulogluserial = $commandlu
GROUP BY `tbllulog`.`tbllulogid`
ORDER BY `tbllulog`.`tbllulogid` DESC
Try like this
DECLARE #type varchar(50);
DECLARE #num int;
SET #type = '';
SET #num = 1;
SELECT * FROM
(
SELECT lulogtimestamp,
lulogmoveemployee,
lulogsource,
lulogaction,
lulogluoutput0status,
lulogluinput0status
#num := if(#type = lulogmoveemployee, #num + 1, 1) as row_number,
#type := lulogmoveemployee As Dummy
FROM tbllulog
WHERE lulogluserial = $commandlu
ORDER BY `tbllulog`.`tbllulogid` DESC
) T WHERE row_number = 1
Related
I use this code in MySQL to order by 'anotherColumn' and then get the row number of 'myColumn' and then I perform a calculation and set 'myColumn' to the result:
SET #c = (SELECT COUNT(*) FROM myTable); SET #rownum = 0; UPDATE myTable SET myColumn = #c * (#rownum:= 1 + #rownum) ORDER BY anotherColumn DESC LIMIT 100000;
I'm trying to achieve the same thing in Postgresql but am getting a lot of errors. I have:
SET c = (SELECT COUNT(*) FROM myTable); SET rownum = 0; UPDATE myTable SET myColumn = c * (rownum:= 1 + rownum) ORDER BY anotherColumn DESC LIMIT 100000;
.. but it gives me an error at the first parenthesis. If I remove those parenthesis like this:
SET c = SELECT COUNT(*) FROM myTable; SET rownum = 0; UPDATE myTable SET myColumn = c * (rownum:= 1 + rownum) ORDER BY anotherColumn DESC LIMIT 100000;
.. then it gives me an error at the SELECT. If I just set c to equal 0, I get an error way down at the ORDER. Does anyone know how to convert my code from MySQL to PostgreSQL?
This "pattern" in MySQL is typically used to work around the absence of window function.
You don't need variables in Postgres to achieve something like that:
update my_table
set my_column = t.cnt + t.rn
from (
select pk_column,
(select count(*) from my_table) as cnt,
row_number() over (order by another_column) as rn
from my_table
limit 100000
) t
where t.pk_column = my_table.pk_column;
Where pk_column is the primary key column of your table. If you have more than one PK column, you need to use all of them.
I have a query that uses a variable in its where clause:
SET #id = 1;
SELECT
id,
value
FROM myTable
WHERE id = #id;
I would like to run the query for #id values 1 through 100, and then union (or somehow combine) all the loop results into one result set. Is this possible in MySQL, and if so, what is a good approach?
Why would use use a variable for this? Just use a simple where clause:
select id, value
from myTable
where id between 1 and 100;
If, instead, you really want the first 100 rows by id, then use order by and limit:
select id, value
from myTable
order by id
limit 100;
Just use a subquery to get the variable.
And put the where outside
SELECT *
FROM
(
SELECT
#rownumber := if(#rownumber, #rownumber + 1, #rownumber + 1) AS id,
value
FROM myTable
CROSS JOIN (select #rownumber := 0) r
) as T
WHERE id < 100;
Can I include an additional counter in a MySQL result set? I have the following query which gives me two columns back. I need an additional column (only in the result) indicating the row of each line in the result set.
select orderid, round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
order by 2 desc
limit 10
I need something like the following:
10865 1 17250.00
11030 2 16321.90
10981 3 15810.00
10372 4 12281.20
10424 5 11493.20
Try this:
SET #counter = 0;
Select sub.*
FROM
(
select orderid, (#counter := #counter +1) as counter,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
Try following
SET #counter = 0;
select orderid, (#counter:= #counter + 1) as counter, round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
order by 3 desc
limit 10
Hope it helps...
Based on the two answers I managed to get the following:
SET #counter = 0;
Select sub.orderid,sub.value,(#counter := #counter +1) as counter
FROM
(
select orderid,
round(sum(unitprice * quantity),2) as value
from order_details
group by orderid
) sub
order by 2 desc
limit 10
The original answers showed the IDs from the inner query resulting in larger ints with huge gaps. Using the modification I get just the '1 to x' range that I need for my pgfplots LaTeX plot.
Lets say I have these columns
uniqueID|Money|Quantity|MoneyOrder|QuantityOrder
1|23|12||
2|11|9||
3|99|100||
What I want to do is update MoneyOrder and QuantityOrder based on the value of ORDER BY.
So the results would be:
uniqueID|Money|Quantity|MoneyOrder|QuantityOrder
1|23|12|2|1
2|11|90|1|2
3|99|100|3|3
I want the update to operate like an identity column without actually making it an identity column. I know that I could just order by 'x' and the order would be the result but I want to generate a report where you can see the item line by line.
Is something like this possible update mytable set Moneyorder = 'imnotsure' order by MoneyOrder asc ?
SET #rownumber = 0;
update mytable set Moneyorder = (#rownumber:=#rownumber+1)
order by MoneyOrder asc
or to do it in a single query you can try
update mytable target
join
(
select id, (#rownumber := #rownumber + 1) as rownum
from mytable
cross join (select #rownumber := 0) r
order by MoneyOrder asc
) source on target.id = source.id
set Moneyorder = rownum
See answers to this question:
Updating column so that it contains the row position
SET #counter = 0;
UPDATE
my_table
SET MoneyOrder = #counter := #counter + 1
ORDER BY Money;
SET #counter = 0;
UPDATE
my_table
SET QuantityOrder = #counter := #counter + 1
ORDER BY Quantity;
First I want to make clear this is not a simple LIMIT x,y question. I want to know if it is possible to do a query like the following peuso query.
SELECT *, OFFSET_OF_ROW()
FROM `table`
WHERE `some_column` = someValue
ORDER BY `some_other_column`;
the pseudo function OFFSET_OF_ROW() should give the number of rows which would come before the selected row (+1) if there was no condition `some_column = someValue`
This isn't particularly efficient, but it will do what you want:
select #rownum := 0;
select * from (
select #rownum := #rownum + 1, id, some_column, sortcol
from `table`
order by `sortcol`
) all_rows
where `some_column` = someValue;