I am trying to create an UPDATE query that works off of a query
There is a 1->M relationship between ChargeTransaction and ChargeError
ChargeBody is a TEXT field containing json.
When I run the inner query I get a distinct list of chargetransactionids and account ids (1 2 and 3), but when I put it into an UPDATE statement, it assigns an accountid of 3 to all of the chargetransactions.
UPDATE ChargeTransaction
SET AccountId= a.AccountID
FROM
(
SELECT ChargeTransactionId, AccountID
FROM
(
SELECT
[ChargeTransactionId],
CASE WHEN ChargeBody LIKE '%"ReceivingFacility":"998001"%' THEN 1
WHEN ChargeBody LIKE '%"ReceivingFacility":"998002"%' THEN 2
WHEN ChargeBody LIKE '%"ReceivingFacility":"998003"%' THEN 3
ELSE 1
END AS AccountId,
ChargeBody
FROM [ChargesDashboard].[dbo].[ChargeError]
) b
GROUP BY ChargeTransactionId, AccountId
) a
WHERE ChargeTransactionId=a.ChargeTransactionId ;
This may not be the most elegant but I think it would work. I think the problem is that you wanted a join to your derived table.
with myCte as
(
SELECT ChargeTransactionId, AccountID
FROM
(
SELECT
[ChargeTransactionId],
CASE WHEN ChargeBody LIKE '%"ReceivingFacility":"998001"%' THEN 1
WHEN ChargeBody LIKE '%"ReceivingFacility":"998002"%' THEN 2
WHEN ChargeBody LIKE '%"ReceivingFacility":"998003"%' THEN 3
ELSE 1
END AS AccountId,
ChargeBody
FROM [ChargesDashboard].[dbo].[ChargeError]
) b
GROUP BY ChargeTransactionId, AccountId
)
UPDATE C
set AccountID = myCte.AccountID
from ChargeTransaction C
inner join myCte on C.ChargeTransactionId = myCte.ChargeTransactionId
Turns out the problem was that the ChargeTransaction table doesn't have a ChargeTransactionId field, it has an Id field
UPDATE ChargeTransaction
SET AccountId= a.AccountID
FROM
(
SELECT ChargeTransactionId, AccountID
FROM
(
SELECT
[ChargeTransactionId],
CASE WHEN ChargeBody LIKE '%"ReceivingFacility":"998001"%' THEN 1
WHEN ChargeBody LIKE '%"ReceivingFacility":"998002"%' THEN 2
WHEN ChargeBody LIKE '%"ReceivingFacility":"998003"%' THEN 3
ELSE 1
END AS AccountId,
ChargeBody
FROM [ChargesDashboard].[dbo].[ChargeError]
) b
GROUP BY ChargeTransactionId, AccountId
) a
WHERE ChargeTransaction.Id=a.ChargeTransactionId ;
Related
I need to update row which having same value in debit column & only row which having create_date ASC basically 2019-11-15
I am having transaction table with following data
table name - tbl_transactions
id client_id user_id debit add_date
1 9991101 1 7.69 2019-11-15
2 9991101 1 7.69 2019-11-30
3 9991101 2 28.9 2019-11-15
4 9991101 2 11.49 2019-11-30
Now i just want to UPDATE record which have same value in column "debit" & date "2019-11-15"
which means only id-1 will be get updated.
id client_id user_id debit add_date
1 999110100 1 7.69 2019-11-15
We can add additional 00 to client_id field, i have tried with below sql but seems its not working
UPDATE tbl_transactions
SET client_id=999110100
WHERE id IN (
SELECT *
FROM tbl_transactions
WHERE client_id=9991101
AND DATE(create_date)='2019-11-15'
GROUP BY user_id, debit HAVING COUNT(*) = 2 )
Thanks in advance.
From what I understood from your question, you can try a query like
UPDATE
`tbl_transactions`
SET
`COLUMN_NAME` = DESIRED_VALUE
WHERE
`debit` = 7.69
AND
`add_date` = '2019-11-15'
You just need to replace COLUMN_NAME in the above query with your desired column name and it's respective value ahead of it. Do let me know if that's what you're trying to achieve or something else.
Mysql cannot specify target table for update in FROM clause.
You need to use subquery select them in a temporary table and update it,
And you can use CONCAT to concat your client_id with 00,
According to your post, you need the same debit and order by created_date ASC,
however, mysql cannot just order a field in group, so you can do it like this:
UPDATE
`tbl_transactions`
SET
`client_id` = CONCAT(client_id, '00')
WHERE id IN (
SELECT id FROM (
SELECT t2.id, t2.user_id t2.created_date FROM
(
SELECT id, user_id, MIN(created_date) AS created_date
FROM tbl_transactions
WHERE client_id=9991101
GROUP BY user_id, debit
HAVING COUNT(*) >= 2
) AS t1
INNER JOIN tbl_transactions AS t2
ON t1.user_id = t2.user_id AND t1.debit = t2.debit AND t2.client_id = 9991101 AND t1.created_date = t2.created_date
GROUP BY t2.user_id
) AS temp_table
)
I have one Table Test in this table Id column, FuelId and FuelDesc columns are there.values are like below table,Based on first three columns i need to create out put table like below please help me to get.
ID FuelID FuelDesc
100 01 Elec
101 02 Gas
102 02 Gas
100 02 Gas
101 01 Elec
103 01 Elec
O/P:-
ID Pamenttype
100 Both
101 Both
102 Gas
103 Elec
You can use a CASE expression, with the help of COUNT and MAX functions:
SELECT
ID,
PaymentType = CASE WHEN COUNT(FuelId) > 1 THEN 'Both' ELSE MAX(FuelDESC) END
FROM test
GROUP BY ID
SELECT ID, CASE WHEN rowCount = 1 THEN FuelDesc ELSE 'Both' END AS Pamenttype
FROM
(SELECT ID, MAX(FuelDesc) AS FuelDesc, COUNT(*) AS rowCount
FROM mytable
GROUP BY ID)
You can define Both as having one record with FuelID = '01' and another with FuelID = '02'. Assuming {ID, FuelID} is unique in table Test:
with BothPaymentTypes as (
select t1.ID
from Test t1
join Test t2 on t1.ID = t2.ID
where t1.FuelID = '01'
and t2.FuelID = '02'
)
select ID, 'Both' as Pamenttype
from BothPaymentTypes
union all
select ID, FuelDesc
from Test
where ID not in (select ID from BothPaymentTypes)
[SQL Fiddle Demo]
Alternatively, you can avoid the self-join with a COUNT and HAVING clause - again assuming {ID, FuelID} is unique:
with BothPaymentTypes as (
select ID
from Test
group by ID
having count(*) = 2
)
select ID, 'Both' as Pamenttype
from BothPaymentTypes
union all
select ID, FuelDesc
from Test
where ID not in (select ID from BothPaymentTypes)
[SQL Fiddle Demo]
WITH fuelCount
AS
(
SELECT ID, COUNT(FuelID) AS fuelCount
FROM Test GROUP BY ID
)
SELECT fc.ID,
CASE
WHEN fc.fuelCount > 1 THEN 'BOTH'
ELSE (SELECT t.FuelDesc FROM Test t WHERE t.ID = fc.ID)
END AS Pamenttype
FROM fuelCount fc
I have a table like this:
recordid customerid product id count
1 2 12 3
2 4 10 1
3 2 3 3
4 3 12 2
5 3 10 2
6 2 7 3
7 5 3 1
8 ....
9 ....
I want an update query that will count the no of occurrence of each customer id and update the count column which will initially be empty.
the end result should be like above
The column names are dummy, my actual table is different.
It has data in millions of rows.The query should be speedy
I tried the query but it gets stuck...
update tablename, (select count(recordid) as count,customerid from tablename group by customerid) as temp set count=temp.count where customerid=temp.customerid
You can use JOIN in UPDATE.
Try this:
UPDATE TableName A
JOIN
(SELECT customerid,Count(customerid) as cnt
FROM TableName
GROUP BY customerid) as B ON A.customerid= B.customerid
SET A.count = B.cnt
This doesn’t see right:
update tablename, (select count(recordid) as count,customerid from tablename group by customerid) as temp set count=temp.count where customerid=temp.customerid
Why is there a comma after update tablename like this:
update tablename,
I am also reformatting for readability:
UPDATE tablename (
SELECT count(recordid) AS count, customerid
FROM tablename GROUP BY customerid
) as temp
SET count=temp.count
WHERE customerid = temp.customerid
I have constructed a junction table which goes like this.
Table Name: myTable
p_id | c_id
-----------
1 1
1 2
1 3
2 2
2 3
3 2
3 3
3 4
I wanted to SELECT p_id that doesn't have both c_id 3 and 4. In this case only p_id 3 has both c_id 3 and 4 so after the select statement the query should return both p_id 1 and 2.
The thing is that I try different kind of method but still it wouldn't work. I really need help.
my query
1.) SELECT DISTINCT p_id FROM myTable WHERE c_id != 3 AND course_id != 4;
Problem: It still returns 3 as one of the result since 3 has c_id of 2
Something like this:
SELECT DISTINCT p_id
FROM mytable
WHERE p_id NOT IN (SELECT p_id
FROM mytable
WHERE c_id IN ( 3, 4 )
GROUP BY p_id
HAVING Count(DISTINCT c_id) = 2)
SQLFiddle demo
Try this:
SELECT DISTINCT p_id
FROM myTable
WHERE c_id IN (3,4)
GROUP BY p_id HAVING COUNT(DISTINCT c_id)<2
The straightforward solution is to use exists:
select
distinct p_Id
from myTable t
where not (exists (select 1
from myTable
where (c_id = 3) and
(p_id = t.p_id)) and
exists (select 1
from myTable
where (c_id = 4) and
(p_id = t.p_id)))
Try this:
SELECT mytable.p_id
FROM mytable
LEFT OUTER JOIN (SELECT v1.p_id
FROM (SELECT p_id
FROM mytable
WHERE c_id = 3) v1
INNER JOIN (SELECT p_id
FROM mytable
WHERE c_id = 4) v2
ON v1.p_id = v2.p_id) v
ON mytable.p_id = v.p_id
WHERE v.p_id IS NULL
GROUP BY mytable.p_id
Try this:
select distinct mytable.p_id from mytable where c_id not in (3,4) and p_id <>3
This will give result which does not have 3 and 4
I have a table like:
ID | LABEL | SOME_VALUE
1 a rand_1
2 a NULL
3 b rand_9
4 c rand_3
5 c rand_3
6 c rand_3
7 d NULL
8 d rand_4
As you can see, ID is unique, label is not unique (can be 1 or more) and some_value is also not unique.
What I want to do is the following:
I want to get a unique list of LABELS, which exist in the database in more than one rows (min 2) and of which rows has SOME_VALUE not NULL.
So I would get:
ID | LABEL | SOME_VALUE
1 a rand_1
2 a NULL
7 d NULL
8 d rand_4
in return.
How can I achieve this?
There are two versions. First one does exactly as listed in results, eliminating rand_3 because even though it appears three times all the values are the same (I don't see distinct condition specified in question).
There must be a better way, but as they say I can't brain today, I have the dumb :-)
select *
from tbl
inner join
(
select label
FROM tbl
GROUP BY Label
HAVING count (distinct some_value)
+ sum(distinct case when some_value is null then 1 else 0 end) > 1
) a
on tbl.label = a.label
Second one retrieves C also following the requirements (some_value being not null for at least one of some_value).
select *
from tbl
inner join
(
select label
FROM tbl
GROUP BY Label
HAVING count(*) > 1 and count(some_value) > 0
) a
on tbl.label = a.label
And there is Sql Fiddle.
The HAVING parameter limits grouped items:
SELECT
Label
FROM dbo.TableName
WHERE NOT Some_Value IS NULL
GROUP BY Label
HAVING COUNT(*) > 2
SELECT t1.*
FROM yourTable t1
JOIN yourTable t2
ON t1.LABEL = t2.LABEL
AND t1.ID < t2.ID
WHERE t1.SOME_VALUE IS NOT NULL
OR t2.SOME_VALUE IS NOT NULL
This should work -
SELECT test.*
FROM (
SELECT label
FROM test
GROUP BY Label
HAVING COUNT(DISTINCT IFNULL(some_value, '~null~')) > 1
) AS tmp
INNER JOIN test
ON tmp.label = test.label;