update
table1 a
inner join table2 b on a.unique_id = b.unique_id
set
a.menu_return = sum(
case when b.value1 = 'mainmenu'
and b.value2 = '0' then 1 else 0 end
)
where
a.time_stamp >= '2021-12-17 00:00:00'
and a.time_stamp < '2021-12-18 00:00:00';
ERROR 1111 (HY000): Invalid use of group function
What cause this issue?
I'm not really understand what you try to achieve by this query, but looks as you need to use join with pre-aggregated query like:
update table1 a
inner join (
select
unique_id,
sum( case when value1 = 'mainmenu' and value2 = '0' then 1 else 0 end ) menu_return
from table2
group by unique_id
) b on a.unique_id = b.unique_id
set
a.menu_return = b.menu_return
where
a.time_stamp >= '2021-12-17 00:00:00' and a.time_stamp < '2021-12-18 00:00:00';
You can play with MySQL online here
Related
Tried troubleshooting this for a bit but thought it might be easy for someone more experienced... but it keeps turning up Error 1054. The goal is to sum both up and present a single number.
select sum(next_price)
from
(
select sum(sub.next_price)
from sub
left join account on account.acctid = sub.acctid
where sub.date_created >= curdate()
and sub.date_created < curdate()+1
and account.type <> 'internal'
UNION ALL
select sum(sub.next_price)*-1
from sub
left join account on account.acctid = sub.acctid
where sub.date_closed >= curdate()
and sub.date_closed < curdate()+1
and account.type <> 'internal'
) as Temp
group by next_price
There's no column with the name next_price in the temporary table Temp, that's why you're getting that message.
sum(sub.next_price) does not create you a column with the name next_price.
Try this query :
select sum(t.price) as next_price
from
(
select sum(sub.next_price) as price
from sub
left join account on account.acctid = sub.acctid
where sub.date_created >= curdate()
and sub.date_created < curdate()+1
and account.type <> 'internal'
UNION ALL
select sum(sub.next_price)*-1 as price
from sub
left join account on account.acctid = sub.acctid
where sub.date_closed >= curdate()
and sub.date_closed < curdate()+1
and account.type <> 'internal'
) as Temp as t
group by t.next_price
I'm trying to select if a user rating (user.rating) is greater then 6 or if the user has more then 100 transactions (transaction table count). Basically count how many transactions the user has then where (transaction count >= 100 OR user rating >= 6).
SELECT *
FROM `user`
JOIN (SELECT COUNT(*)
FROM transaction
WHERE transaction.user_id=user.id
AND type='L'
AND status='S') AS tcount
WHERE (user.rating >= '6' OR tcount >= '100')
Just another possible answer. I've created simplified schemas to test it, please try it and let me know the result.
SELECT *
FROM user
WHERE user.rating >= 6 OR (SELECT COUNT(*) FROM transaction WHERE user_id = user.id and type = 'L' and status = 'S') >= 100;
Use an alias on COUNT(*)
SELECT *
FROM `user`
JOIN (SELECT user_id, COUNT(*) cnt
FROM transaction
WHERE type='L'
AND status='S'
GROUP BY user_id) AS tcount
ON user.id = tcount.user_id
WHERE (user.rating >= '6' OR tcount.cnt >= '100')
You can write that without the subquery, like this
SELECT u.id
FROM `user` u
JOIN `transaction` t
ON t.user_id=u.id
WHERE t.type = 'L' AND t.status = 'S'
GROUP BY u.id
HAVING sum(case when u.rating >= 6 then 1 end) > 0 OR count(*) >= 100
I am trying to collaborate 3 queries to perform arithmetic operation. The queries are shown in
(SELECT ITEM_ID,ISNULL(SUM(REC_GOOD_QTY),0)
FROM INVENTORY_ITEM
WHERE COMPANY_ID = 1
AND INVENTORY_ITEM.COMPANY_BRANCH_ID = 1
AND INVENTORY_ITEM.INV_ITEM_STATUS = 'Inward'
AND GRN_DATE < CAST('2017-01-10 00:00:00.0' AS DATETIME)
GROUP BY INVENTORY_ITEM.ITEM_ID) -
(SELECT ITEM_ID, SUM ( TOTAL_LITRE )
FROM STOCK_REQUISITION_ITEM B, STOCK_REQUISITION A
WHERE A.ID = B.REQUISITION_ID
AND A.COMPANY_ID = 1
AND A.REQ_FROM_BRANCH_ID = 1
AND A.REQUISITION_DATE < CAST('2017-01-10 00:00:00.0' AS DATETIME)
GROUP BY B.ITEM_ID) +
(SELECT ITEM_ID, SUM ( RETURN_QUANTITY )
FROM STOCK_RETURN_ITEM B, STOCK_RETURN A
WHERE A.ID = B.STOCK_RETURN_ID
AND A.COMPANY_ID = 1
AND A.COMPANY_BRANCH_ID = 1
AND A.RETURN_DATE <= CAST('2017-01-10 00:00:00.0' AS DATETIME)
GROUP BY B.ITEM_ID)
I am getting this error.
[Err] 42000 - [SQL Server]Incorrect syntax near '-'.
42000 - [SQL Server]Incorrect syntax near '+'
Not much to go on here for details. And we aren't actually sure if you are using mysql or sql server but pretty sure you are using sql server. I think you can accomplish what you are trying to do with something along these lines.
with Iventory as
(
SELECT i.ITEM_ID
, GoodQty = ISNULL(SUM(i.REC_GOOD_QTY), 0)
FROM INVENTORY_ITEM i
WHERE COMPANY_ID = 1
AND i.COMPANY_BRANCH_ID = 1
AND i.INV_ITEM_STATUS = 'Inward'
AND i.GRN_DATE < '2017-01-10'
GROUP BY i.ITEM_ID
)
, StockRequisition as
(
SELECT ITEM_ID
, TotalLitre = SUM(TOTAL_LITRE)
FROM STOCK_REQUISITION_ITEM B
JOIN STOCK_REQUISITION A ON A.ID = B.REQUISITION_ID
WHERE A.COMPANY_ID = 1
AND A.REQ_FROM_BRANCH_ID = 1
AND A.REQUISITION_DATE < '2017-01-10'
GROUP BY B.ITEM_ID
)
StockReturn as
(
SELECT ITEM_ID
, ReturnQuantity = SUM(RETURN_QUANTITY)
FROM STOCK_RETURN_ITEM B
JOIN STOCK_RETURN A ON A.ID = B.STOCK_RETURN_ID
WHERE A.COMPANY_ID = 1
AND A.COMPANY_BRANCH_ID = 1
AND A.RETURN_DATE <= '2017-01-10'
GROUP BY B.ITEM_ID
)
select i.ITEM_ID
, MyCalculation = i.GoodQty - isnull(Req.TotalLitre, 0) + isnull(sr.ReturnQuantity, 0)
from Inventory i
left join StockRequisition sr on sr.ITEM_ID = i.ITEM_ID
left join StockReturn Req on Req.ITEM_ID = i.ITEM_ID
In your queries you always returns two fields ITEM_ID and a numeric field.
To apply an arithmetical operation you must return one numeric field
The first query:
SELECT ITEM_ID,ISNULL(SUM(REC_GOOD_QTY),0)
becomes
SELECT ISNULL(SUM(REC_GOOD_QTY),0)
The second query:
SELECT ITEM_ID, SUM ( TOTAL_LITRE )
becomes
SELECT SUM ( TOTAL_LITRE )
The third query:
SELECT ITEM_ID, SUM ( RETURN_QUANTITY )
becomes
SELECT SUM ( RETURN_QUANTITY )
So the GROUP BY returns more than one row per query
UPDATE
i try to rewrite your query:
SELECT DISTINCT ii.item_id,
ISNULL(
(SELECT SUM(ii2.rec_good_qty)
FROM inventory_item ii2
WHERE ii2.item_id = ii.item_id
AND ii.company_id = 1
AND ii.company_branch_id = 1
AND ii.inv_item_status = 'Inward'
AND ii.grn_date < CAST('2017-01-10 00:00:00.0' AS DATETIME))
,0) -
ISNULL(
(SELECT SUM(total_litre)
FROM stock_requisition_item b
JOIN stock_requisition a
ON a.id = b.requisition_id
WHERE a.company_id = 1
AND a.req_from_branch_id = 1
AND a.requisition_date < CAST('2017-01-10 00:00:00.0' AS DATETIME))
,0) +
ISNULL(
(SELECT SUM(return_quantity)
FROM stock_return_item b
JOIN stock_return a
ON a.id = b.stock_return_id
WHERE a.company_id = 1
AND a.company_branch_id = 1
AND a.return_date <= CAST('2017-01-10 00:00:00.0' AS DATETIME))
,0) AS result
FROM inventory_item ii
WHERE ii.company_id = 1
AND ii.company_branch_id = 1
AND ii.inv_item_status = 'Inward'
AND ii.grn_date < CAST('2017-01-10 00:00:00.0' AS DATETIME)
I have a table like this:
<table border="1" cellspacing="0" cellpadding="5">
<tr><td>ID</td><td>status</td><td>start_date</td><td>end_date</td></tr>
<tr><td>1</td><td>A</td><td>2015-01-01</td><td>2015-12-31</td></tr>
<tr><td>1</td><td>B</td><td>2016-01-01</td><td>NULL</td></tr>
<tr><td>2</td><td>C</td><td>NULL</td><td>2016-12-31</td></tr>
<tr><td>3</td><td>D</td><td>NULL</td><td>NULL</td></tr>
</table>
I must do a trigger in MySQL to update a main table with the last status of a subject.
To do this I must select for every ID the last status based on the period between start_date and end_date that can be NULL if didn't known.
I write this SQL statement test:
SELECT *
FROM My_Table AS T
WHERE T.start_date Is Null AND
T.end_date In(SELECT MAX(end_date) FROM My_Table WHERE ID = T.ID)
OR
T.start_date In(SELECT MAX(start_date) FROM My_Table WHERE ID = T.ID) AND
T.end_date Is Null
OR
T.start_date In(SELECT MAX(start_date) FROM My_Table WHERE ID = T.ID) AND
T.end_date In(SELECT MAX(end_date) FROM My_Table WHERE ID = T.ID
);
Is there a shorten method to do this?
In practice I want a WHERE clause that catch each of these cases (a = start_date, b=end_date):
a = MAX AND b = MAX OR
a = NULL AND b = MAX OR
a = MAX AND b = NULL OR
a = NULL AND b = NULL
Here's a slightly simplified query which returns exactly the same result set as the query in your question. It uses only two subqueries instead of four (no duplicated subqueries):
SELECT *
FROM My_Table AS T
WHERE
(T.start_date Is Null OR T.start_date = (SELECT MAX(start_date) FROM My_Table WHERE ID = T.ID)) AND
(T.end_date Is Null OR T.end_date = (SELECT MAX(end_date) FROM My_Table WHERE ID = T.ID)) AND
(T.start_date Is NOT Null OR T.end_date Is NOT Null)
;
Note that it does NOT include the a = NULL AND b = NULL case. To include it as well, the last AND-clause must be removed, i.e.:
SELECT *
FROM My_Table AS T
WHERE
(T.start_date Is Null OR T.start_date = (SELECT MAX(start_date) FROM My_Table WHERE ID = T.ID)) AND
(T.end_date Is Null OR T.end_date = (SELECT MAX(end_date) FROM My_Table WHERE ID = T.ID))
;
I have data with begin and end date, sometimes the end date had a null and is now
'0000-00-00'
I have a query that says:
UPDATE w_preletter as L INNER JOIN w_prov_sensitive AS P ON L.IRS_TAX_ID = P.TAX_ID
set L.sensitive_provider = '1', L.OFFSET_RESTRICTED = P.OFFSET_RESTRICTED
where L.DATE_OF_SERVICE_BEG between P.BEGIN_DATE AND P.END_DATE;
but if the date is '0000-00-00' it means it is still effective so I need it to temporarily consider it the sysdate , otherwise it will not consider the date being in between. what would the syntax be for that?
I don't know if its a case statement and if so how I would write it out.
try this
UPDATE w_preletter as L INNER JOIN w_prov_sensitive AS P ON L.IRS_TAX_ID = P.TAX_ID
set L.sensitive_provider = '1', L.OFFSET_RESTRICTED = P.OFFSET_RESTRICTED
where L.DATE_OF_SERVICE_BEG between P.BEGIN_DATE AND P.END_DATE
or (L.DATE_OF_SERVICE_BEG >= P.BEGIN_DATE AND P.END_DATE = '0000-00-00')
Create a temporary table containing all the rows in w_prov_sensitive (CREATE TABLE ... SELECT syntax)
update the temporary table setting all END_DATE's to NOW() if they are 0/NULL/whatever
Do the UPDATE by joining against the temporary table
You could also put boolean logic to work and use a convoluted WHERE clause.
where
( P.END_DATE != 0 and L.DATE_OF_SERVICE_BEG between P.BEGIN_DATE AND P.END_DATE )
OR ( P.END_DATE = 0 and L.DATE_OF_SERVICE_BEG between P.BEGIN_DATE AND NOW() )