ADO SQL update table with result of group by query - sql

I am trying to update records in an .mdb table with the number of records containing the same value.
The SQL below does not work but I think gives an indication of what I am trying to achieve.
UPDATE table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT FROM table1 GROUP BY PH_BSP) AS B
ON A.PH_BSP=B.PH_BSP
SET A.PH_SORT = B.PHCOUNT;
Any ideas?

If you are doing this in Access, you need to use a domain aggregate function:
UPDATE table1
SET PH_SORT = DCount("PH_BSP","Table1","PH_BSP='" & PH_BSP & "'")
The above assumes that PH_BSP is a text field, drop the single quotes if it is numeric.

Untested, but setting out the statement thusly this should solve your issue
UPDATE A
SET A.PH_SORT = B.PHCOUNT
From table1 AS A
INNER JOIN (SELECT PH_BSP , Count(PH_BSP) AS PHCOUNT FROM table1 GROUP BY PH_BSP) AS B
ON A.PH_BSP=B.PH_BSP
Edit:
Your problem might be from your sub query, I would try putting that part into a separate Access Query and see how it goes. From memory I used to have a lot of trouble with Access and subqueries, square brackets would also sometimes help, but unreliable from memory.

Have you tried something alike?
update table1 as a
set a.ph_sort = (
select COUNT(b.ph_bsp) as phcount
from table1 b
where b.ph_bsp = a.ph_bsp)
I'm assuming SQL Server here.
But this or something alike should do it, I guess.

Related

UPDATE query joining table and subselect in ACCESS

I am trying to update a field of my table1 using the following code but I receive the following error message:3073 operation must use an updateable query.
UPDATE table1 a
INNER JOIN (SELECT COUNT(somevalue) AS Total,ID
FROM ReadsPNPA GROUP BY ID) b
ON a.ID=b.ID
SET a.Total = b.Total
Any ideas? the subselect query works on its own.
Use a DCount expression instead of a subquery to do your UPDATE ...
UPDATE table1 a
SET a.Total = DCount('somevalue', 'ReadsPNPA', '[ID]=' & a.ID);
Access UPDATE queries are fussy. Techniques like subqueries or GROUP BY cause Access to treat the query as "not updateable". See Why is my query read-only? for more information.
You can use domain aggregate functions (such as DCount, DSum, DAvg, etc.) so that Access will treat the query as updateable.

Update query based on two conditions

I have the following table based on this query:
SELECT
repName.repID, repName.Rep_Name, repName.Job_Code, GenItems.Item_Name,
repName.Entered
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
ORDER BY
repName.Rep_Name
I want to add an update routine to it. I want to update the entered field if the user entry matches the rep.ID and the Item Name. and finally return the Max value for the Entered field. Can I add this to this query or is it better to write another.
I just started working with sql, so if my questions seems basic, please forgive me. I am self taught and stumbling greatly.
Thank you
I don't understand fully your question.
You are showing us a SELECT statement. It can only be used to return a table-like result. If you want to upate a table you must use an UPDATE query. For the SQL-Server (and SQL CE) the query looks like this:
UPDATE repName
SET repName.Entered = x
FROM
GenItems
INNER JOIN repName
ON GenItems.Job_Code = repName.Job_Code
WHERE
repName.repID = x AND GenItems.Item_Name = 'y'
The difficulty is that tables have to be joined in the UPDATE statement. This not supported in Oracle for instance, where you have to do it with sub-selects.

sql query result returns asterisk "*" as column value

I'm trying to update a temporary table with multiple values from another table without using a join.
However, the query doesn't give any error but rather returns an asterisk as the value of the column. I have googled and asked some folks around the office but no one seems to have encountered this before or can offer explanation of why this could be happening.
update ##tempCLUnique set Total =
(
select COUNT(distinct u.unique_subs)
from tbl_Cluster_Cumm_Unique_Subs u
where u.cluster = ##tempCLUnique.cluster
)
Seems simple enough
Result Screen Grabhttp://i.stack.imgur.com/qE0ER.png
Use this
update ##tempCLUnique set Total = U.unique_subs
FROM ##tempCLUnique
INNER JOIN
(
select COUNT(distinct unique_subs)unique_subs
from tbl_Cluster_Cumm_Unique_Subs
)U
ON
u.cluster = ##tempCLUnique.cluster
Change the join according to your use.
Ashutosh

are there loops in t-sql ? is using them a good idea?

Pardon me if I'm asking this, but I'm not a SQL Server nor SQL developer.
i have a CSV that i import into a table let's call it T that i create on the fly in SQL Server 2005.
what i would like to do is to run some queries against other tables based on the data imported into the table T i created.
example :
select *
from TableX
where customerID = [this should contain the customerID from the table T]
and then if i find it, i need to update the same table T, if not i move along... until the last record in that csv file. Any idea will be appreciated.
No loop necesary for what you want, it seems that you only need IN:
SELECT *
FROM TableX
WHERE CustomerID IN (SELECT CustomerID FROM TableT)
If you need to update TableX with some mark if exists on TableT, it should be:
UPDATE TableX
SET Mark = 1
WHERE CustomerID IN (SELECT CustomerID FROM TableT)
If you need to update TableX with some value from TableT, it should be something like this:
UPDATE X
SET X.Column = T.Column
FROM TableX X
INNER JOIN TableT T
ON X.CustomerID = T.CustomerID
Looping is a red flag in SQL and is not usually needed. SQL is based on set theory. Understand JOINs and you will rarely need to iterate.
So, in your case, no, looping is neither necessary nor a good idea.

Multiple Query Writing in Single Query

I have two tables in Database , I need to select a field from one table and update it in another table with a condition where id is same .. Is it Possible to write in single query ???
This should work for you:
update storage
set storage.email = (select register.email
from register
where register.id = storage.id)
Yeah it is, you could do this for example:
UPDATE Origin SET DesiredColumn = NewValue
FROM Origin
JOIN NewTable ON Origin.Id = NewTable.Id
And guess the column names were like DesiredColumn in the updating table and NewValue in the table that holds the new value.
Yes, this is possible, although the syntax depends on the type of SQL you are using.
Here is an example for T-SQL (for Microsoft SQL Server)
UPDATE
S
SET
Email = R.Email
FROM
dbo.Register R
INNER JOIN dbo.Storage S
ON S.RegisterID = R.RegisterID