Am trying this query to get result. but am not able to find any results. I don't know where i did the mistake.
Select * from loadcell_transaction_log where id =
(SELECT max(id) as id1
FROM [addpro].[dbo].[loadcell_transaction_log] group by line2_count);
This is the error am getting:
Msg 512, Level 16, State 1, Line 2
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Try this:
Select * from loadcell_transaction_log where id IN
(SELECT max(id) as id1
FROM [addpro].[dbo].[loadcell_transaction_log] group by line2_count);
I replace '=' with the keyword 'IN' which allows for multiple results in the subquery.
using group by with max(id) will return max(id) for each line2_count :
select * from
(select log.*,row_number() over (partition by line2_count order by id desc ) rn FROM [addpro].[dbo].[loadcell_transaction_log] as log)
where rn = 1;
If you want to check only for max(id) in the full table data then dont use group by clause else use in clause as referred by #farbiondriven.
Related
Is it possible to order when the data comes from many select and union it together? Such as
In this statement, the vouchers data is not showing in the same sequence as I saved on the database, I also tried it with "ORDER BY v_payments.payment_id ASC" but won't be worked
( SELECT order_id as id, order_date as date, ... , time FROM orders WHERE client_code = '$searchId' AND order_status = 1 AND order_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
UNION
( SELECT vouchers.voucher_id as id, vouchers.payment_date as date, v_payments.account_name as name, ac_balance as oldBalance, v_payments.debit as debitAmount, v_payments.description as descriptions,
vouchers.v_no as v_no, vouchers.v_type as v_type, v_payments.credit as creditAmount, time, zero as tax, zero as freightAmount FROM vouchers INNER JOIN v_payments
ON vouchers.voucher_id = v_payments.voucher_id WHERE v_payments.client_code = '$searchId' AND voucher_status = 1 AND vouchers.payment_date BETWEEN '$start_date' AND '$end_date' ORDER BY v_payments.payment_id ASC , time )
UNION
( SELECT return_id as id, return_date as date, ... , time FROM w_return WHERE client_code = '$searchId' AND w_return_status = 1 AND return_date BETWEEN '$start_date' AND '$end_date' ORDER BY time)
Wrap the sub-select queries in the union within a SELECT
SELECT id, name
FROM
(
SELECT id, name FROM fruits
UNION
SELECT id, name FROM vegetables
)
foods
ORDER BY name
If you want the order to only apply to one of the sub-selects, use parentheses as you are doing.
Note that depending on your DB, the syntax may differ here. And if that's the case, you may get better help by specifying what DB server (MySQL, SQL Server, etc.) you are using and any error messages that result.
You need to put the ORDER BY at the end of the statement i.e. you are ordering the final resultset after union-ing the 3 intermediate resultsets
To use an ORDER BY or LIMIT clause to sort or limit the entire UNION result, parenthesize the individual SELECT statements and place the ORDER BY or LIMIT after the last one. See link below:
ORDER BY and LIMIT in Unions
(SELECT a FROM t1 WHERE a=10 AND B=1)
UNION
(SELECT a FROM t2 WHERE a=11 AND B=2)
ORDER BY a LIMIT 10;
I need to find intersection between the following queries in MYSQL
SELECT *
FROM project.backup_table
where project.backup_table.date <= (SELECT date FROM project.main_inout_table ORDER BY date desc LIMIT 1)
and project.backup_table.date >= (SELECT date FROM project.main_inout_table ORDER BY date asc LIMIT 1)
SELECT *
FROM project.backup_table
WHERE concat(empid,date) not IN (SELECT concat(empid,date) FROM project.main_inout_table
The tables are:
maintable
backuptable
My atttempt:
SELECT * FROM project.backup_table
where project.backup_table.date <= (SELECT date FROM project.main_inout_table
ORDER BY date desc LIMIT 1) and project.backup_table.date >= (SELECT date FROM project.main_inout_table
ORDER BY date asc LIMIT 1) and exists (SELECT * FROM project.backup_table
WHERE concat(empid,date) not IN (SELECT concat(empid,date)
FROM project.main_inout_table));
Problem: the details of tid 4 is present shouldn't it be filter out by second select query ?
The intersection would be the rows that meet both conditions. So, just bring the conditions together:
SELECT bt.*
FROM project.backup_table bt
WHERE bt.date <= (SELECT MAX(date) FROM project.main_inout_table mit) AND
bt.date >= (SELECT MIN(date) FROM project.main_inout_table mit) AND
NOT EXISTS (SELECT 1
FROM project.main_inout_table mit
WHERE mit.empid = bt.empid AND mit.date = bt.date
);
Note the following changes:
The tables are given aliases, which are abbreviations for the table names.
The columns are all qualified with the table aliases.
The first two subqueries simply use MIN() and MAX(). These could be combined into one subquery or join, but this follows your original formulation.
The last subquery uses EXISTS rather than CONCAT(). Actually, this could also use IN with tuples (something that MySQL supports, but not all databases).
I want to write a select like this
if(`order` > 0)
Order BY category_id, arder
if(`order` = 0)
Order BY id
But in this select does not work
SELECT * from post
ORDER BY if(`order` > 0, ( category_id, `order`) , id)
"If" is an expression returns a value, I don't think you can use it to change the "order by" statement itself.
You can try something like this:
select *, case when order>0 then category_id else 0 end cat_id, id
from post
order by cat_id, order, id
I'm using an union statement in mysql but i've some problems sorting the results. The ORDER statement doesn't works at all, the results comes out always sorted by the id field.
Here an example query:
SELECT a.* FROM ( ( select * from ticket_ticket AS t1 WHERE ticket_active=1 ORDER BY t1.ticket_date_last_modified DESC )
UNION ( select * from ticket_ticket AS t2 WHERE ticket_active=0 ORDER BY t2.ticket_date_last_modified DESC, t2.ticket_status_id DESC ) )
AS a LIMIT 0,20;
I want to order the results of the first SELECT by last_modified time, and the second SELECT by time and status. But the ORDER statement get just skipped. The results always come out ordered by the ticket_id ( the PRIMARY KEY ).
What's wrong in this query ?
Thanks!
Ok, i've fixed it writing the query this way:
SELECT a.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=1
ORDER BY ticket_date_last_modified DESC) AS a
UNION ALL
SELECT b.*
FROM
(SELECT *
FROM ticket_ticket
WHERE ticket_active=0
ORDER BY ticket_date_last_modified DESC, ticket_status_id DESC) AS b LIMIT 0,
20;
You are using a UNION query that will return distinct values, and the order of the returned rows is not guaranteed.
But you don't need an union query for this:
select *
from ticket_ticket AS t1
ORDER BY
ticket_active!=1,
ticket_date_last_modified DESC,
ticket_status_id DESC
LIMIT 0,20;
This error occurs while I run the select query, please advise.
Error No : 1111
Error : Invalid use of group function
Query:
SELECT cm.contactid, cm.firstname, cm.surname, cm.mobile, COUNT( * ) total
FROM sales_master as sm, contact_master as cm
WHERE sm.contactid = cm.contactid
AND cm.mobile != '' AND orderdate>='2012-12-18' AND orderdate<='2013-03-18'
GROUP BY sm.contactid
HAVING COUNT(*) >= 1
ORDER BY COUNT(*) DESC, cm.firstname ASC
Change your query like this. Use join instead of cartisian. If type of cm.orderdate is DAte than remove DATE() from the below query. And use alias total in GROUP BY and ORDER BY
instead of Count(*) .
SELECT
cm.contactid,
cm.firstname,
cm.surname,
cm.mobile,
COUNT(cm.contactid) total
FROM sales_master as sm
LEFT JOIN contact_master as cm
ON sm.contactid = cm.contactid
WHERE
AND cm.mobile != ''
AND DATE(cm.orderdate) >= '2012-12-18'
AND DATE(cm.orderdate) <= '2013-03-18'
GROUP BY cm.contactid
HAVING total >= 1
ORDER BY total DESC, cm.firstname ASC
There will be two reason:
1) Version compatibility Problem
2) Your query syntax is not ok,I am sending you the solution of second option.
For Version compatibility Problem you must go to the link mentioned by some one on
comment.
You have to use subquery concept for that.
Modify your query something like that:
SELECT sm.contactid ,cm.contactid as contactid , cm.firstname as firstname, cm.surname as surname , cm.mobile as mobile , COUNT( * ) total
FROM (
SELECT sm.contactid,contactid , firstname,surname ,mobile ,total
FROM sales_master as sm, contact_master as cm
WHERE sm.contactid = cm.contactid
AND cm.mobile != '' AND orderdate>='2012-12-18' AND orderdate<='2013-03-18'
GROUP BY sm.contactid
) q
GROUP BY sm.contactid HAVING COUNT(*) >= 1
ORDER BY COUNT(*) DESC, cm.firstname ASC
I am not sure that i made exact query or not but concept is some thing like that means
"Subquery"...