From the table Table, I would like to build a query that gives me the table Query.
The first issue is to build Query.Note as a concatenation of Table.Note.
Te second issue, more difficult, is to concatenate Table.Project + ": " + Table.Note each time that Table.Note is not empty.
Some clues?
For my needs, it is enough to solve the first issue. The second one would be great to have.
I am open to VBA-solutions.
Thanks in advance!
This involves using the "GetList" Function that is found Microsoft Access condense multiple lines in a table.
Please note that Table and Query are reserved words, and that Note needed to be made plural as Notes.
SELECT
T2.Client
,GetList("Select Project & "": "" & Note From myTable as T1 where T1.Client = """ & T2.Client & """","","; ") AS Notes
FROM myTable as T2
GROUP BY T2.Client;
Related
I copied and pasted a new version of the data into my MS Access table and now I'm getting weird characters in my queries. Essentially if I say:
SELECT a, b from table1
everything is fine. If I instead do
SELECT a, b from table1 group by a, b
I get really weird characters as a result. At first I got upside down L's, but now I'm getting Chinese characters. It's weird because other queries in my database use the table and get the desired output. It seems like it's only when I do a group by that I have the problems. Any suggestions? I was ready to roll it out, but now I'm getting these errors!
This is a bug typically met if grouping on a memo field.
There may be several workarounds depending on your needs:
Select
a, Left(b, 255) As b
From
table1
Group By
a, Left(b, 255)
Select
a, Mid(b, 1) As b
From
table1
Group By
a, Mid(b, 1)
Select
a, First(b) As firstb
From
table1
Group By
a
Select
a, DLookUp("b","table1","Id = " & [table1]![Id] & "") AS b
From
table1
Group By
a, DLookUp("b","table1","Id = " & [table1]![Id] & "")
I have just had the same issue in various reports. The problem is indeed the Memo Field.
The solution that worked for me was more straight forward... I had to remove the "Group by" for the Memo field and the problem disapeared.
I realize this might not be an option in every situation, but if it is, this is the easiest solution as it requires no rewrite of the SQL or even any other change in the DB.
I found this solution here: Allen Brown - Grouping by Memo field yields garbage
Here is yet another option, which I just tried successfully. I was updating a query that someone else had created, and the author had included every field in the Group By clause, to return distinct records. I removed the entire Group By clause and inserted DISTINCT right after SELECT. No more Chinese. This may not be possible in some situations, but in this case it was a simple fix.
Also, I would not have thought of this if not for the insights offered above. Thanks everyone!
After five months of no problems, I had this issue today on a group query which included a "Count" field, so the "DISTINCT" technique wouldn't work. What I did was wrap my LongText field around the offending field. In this table, all of the fields are ShortText except for "Description". So the field groups by CSTR([Description]) - and now it works fine! By the way - this came shortly after a MS Office 365 update!
I am currently in the process of converting a large amount of ASP classic/VBscript pages from an old database (Unify Dataserver) to MySQL.
Say you have a query like this:
sql = "SELECT c.container_type, c_amount, c_sdate, c_edate, csrt " & _
"FROM containers c, container_chars cc"
objRS.Open sql, objConn, 3, 1
If I want to reference the column "c_edate", I can simply use this and it works fine:
x = objRS("c_edate")
However, when it comes to referencing a column like "c.container_type" (With a . used to differentiate it from another table, like so:
x = objRS("c.container_type")
It will say
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
I can fix it by using a number instead:
objRS(0)
This was never an issue until we switched to MySQL. In our old database, using the rs(table.column_name) format worked just fine. But in MySQL, once you add a (.) to the code, it can't find that item unless you switch it to a number.
As you can imagine, this is quite a pain as I go through the 700+ pages of this website manually counting the placement of each column in the corresponding select statement every time something from the query is referenced.
Does anyone know why this is happening or how to make the rs(table.column_name) format work with MySQL like it does with our old database?
In SQL Server, and apparently in MySQL too, the way to reference a field in the result set is to just use the name, without the prefix.
x = objRS("container_type")
The prefix is needed by the database to differentiate between identically-named columns, but once you send the results to a recordset, that recordset doesn't know or care where the columns came from.
The same goes for aliases:
SQL = "SELECT c.container_type AS ctype, [...]"
...
x = objRS("ctype")
Combining these two facts, if you do have identically-named columns in the result set, you must alias at least one of them. If you don't, it won't necessarily give an error, but you will not be able to reference the second column using the rs("name") syntax.
SQL = "SELECT c1.container_type, c2.container_type AS c_type2, ..."
...
x = objRS("container_type")
y = objRS("c_type2")
[Note that while you're at it, you probably should also modify your FROM clauses to use proper FROM table1 INNER JOIN table2 ON table1.fieldA = table2.fieldB type syntax. The FROM table1, table2 WHERE table1.fieldA = table2.fieldB syntax has been deprecated for many years now.]
I have a crosstab query which queries a bunch of locations and gets their measurement readings. I pivot on the measurement readings so I get a table which has all the measurements for a location/date combo on each line. This works fine for getting all the data. It also works fine for filtering on one value per field. i.e. WHERE LocationID = ? AND MeasureID = ? but what I really need is to have something like WHERE LocationID IN (?) AND MeasureID IN (?) where ? is an array (or whatever gets to job done. Is this possible?
On my forms I'm using a DAO.QueryDef object to build my recordsets. I'd like to avoid building the entire query string in VBA if possible, mostly because this particular query is pretty long and I'd rather it live in a view and not a code module. With that said I can build it all in VBA but it's just not the desired solution.
You can always use replace.
sSQL = "SELECT lots of sql WHERE LocationID IN (qqlocidqq)"
sSQLWithLoc = Replace (sSQL, "qqlocidqq", "1,2,3,4")
Dim qdf As QueryDef
'A query that exists
Set qdf= CurrentDB.QueryDefs("MyJunkQuery")
'Permanently change the sql of that query
qdf.SQL = sSQLWithLoc
Looking into this a little further, it may suit you to use Instr, like so:
SELECT Table1.LocationID
FROM Table1
WHERE InStr([#List],[LocationID])>0
Tested like so:
PARAMETERS Number_List Text(50);
TRANSFORM Count(Table1.AKey) AS CountOfAKey
SELECT Table1.AText
FROM Table1
WHERE InStr([Number_List],[ANumber])>0
GROUP BY Table1.AText
PIVOT Table1.ANumber;
Where Table1 consists of fields AKey, AText, and ANumber. Number_List is a comma separated list of numbers supplied by a parameter. Instr checks for the existence of ANumber from Table1 in the supplied parameter.
There is a problem with overlap 1,2,12, but a creative use of commas may suit:
WHERE InStr("," & [Number_List] & "," , "," & [ANumber] & ",")>0
Of course the delimiter does not have to be a comma, | is often useful.
I'm trying to avoid database access in loops within a project I am working on. Not being too good with SQL, I'm not sure of the best way to approach this.
I'm updating a stock level database in a sale procedure with multiple stock locations/pick locations.
Therefore, this is what I am doing.
Looping through Product IDs, then looping through the pick locations for each product and updating quantities as it goes, like:
For Each wProductId In calculatedProds.Keys '' loop through products requested passing values of pick locations
For i = 0 To locationCount ' split the location value from the string as per above
Dim thisLocation As Integer = locationID
Dim thisQty As Integer = qtyPicked
Dim sql As String = "UPDATE `stockLevels` SET `stockLevel`=`stockLevel` - '" & thisQty & "' WHERE `stockLocation`='" & thisLocation & "' AND `id`='" & wProductId & "'"
' DO DATA ACCESS WITH SQL ABOVE
Next
Next
Of course this works, but it is opening a new Database Connection for every stock location, for every item.
So how would I work this into a single Update Statement?
http://www.karlrixon.co.uk/writing/update-multiple-rows-with-different-values-and-a-single-sql-query/
That link gets me very close to what I am after, I think, but I am not 100% sure how to dynamically build that SQL statement and how to add two conditions to the CASE.
I need to build a SQL Statement something like:
UPDATE stockLevels
SET stockLevel= CASE id
WHEN '"& wProductId &"' AND stockLocation='"& thisLocation &"' THEN `stockLevel` - '" & thisQty & "'
WHEN '"& NEXTwProductId &"' AND stockLocation='"& NEXTthisLocation &"' THEN `stockLevel` - '" & NEXTthisQty & "'
END
But that's not correct where I am adding the second parameter to the CASE!
I am using MySQL and VB.NET, as usual, any help much appreciated.
Using a case statement in the set clause in your example isn't ideal for a number of reasons.
There is no where clause to help the database execute the query efficiently
The size of the query for large numbers of updates becomes excessive (consider updating 1000 rows like this)
You are manually implementing a join - the database can almost certainly do this more efficiently than you.
Debugging such a query is also difficult.
Instead, you should first measure the performance of the update one at a time approach to see if you actually need to make improvements.
If performance improvements are required, then I would suggest an approach where the updates are first bulk inserted into a temporary table. A suitable table would have the following columns:
wProductID, stockLocation, newStockLevel
The updates can be bulk inserted using the following MySQL syntax:
INSERT INTO temp_stock_updates
(wProductID, stockLocation, newStockLevel)
VALUES
(?,?,?), (?,?,?), (?,?,?), ...
And then a single update is run to update the main table. This query would look something like this:
UPDATE stockLevels s
JOIN temp_stock_updates u USING (wProductID, stockLocation)
SET
s.stockLevel = u.newStockLevel
Your CASE expression is simply incorrect syntactically.
There are two kinds of CASE expressions, almost identical to each other and yet slightly different in syntax.
One has the form of
CASE expr
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE result_else
END
The other looks like this:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE result_else
END
And you were essentially attempting to mix these two kinds of CASE.
You probably just need to use the second one (also called search CASE, if I am not much mistaken):
...
CASE
WHEN id = '"& wProductId &"' AND stockLocation='"& thisLocation &"' THEN ...
WHEN id = '"& NEXTwProductId &"' AND stockLocation='"& NEXTthisLocation &"' THEN ...
...
Note that if there's no match and the CASE has no ELSE part, the result will be NULL, so make sure you've covered all the cases, otherwise use an ELSE part like this:
ELSE `stocklevel`
I.e. the CASE will evaluate to the original value of the column being updated, rendering no update for it in the end.
I've been having this problem with MySQL for the past month. It's not a big problem, but it's pretty annoying.
query = "SELECT * FROM MESSAGE_TEMPLATES WHERE MESSAGE_SEND_DATE = '" & _date & "'"
This simple line works fine when I pass it to the dataReader in MySQL. However:
query = "SELECT * FROM MESSAGE_TEMPLATES" & _
" WHERE MESSAGE_SEND_DATE = '" & _date & "'"
This does not work, even though intellisense shows them both to have the same value. MySQL complains about having an error in the syntax. You can imagine this becomes more problematic for larger statements that take up several lines. What exactly am I missing here?
Can you print out each string to a console or a log and look for any random white space or involuntarily-included control characters?
It should work - can't be an MySQL thing as a strings a string. If it persists consider using XML syntax to allow you to do the multi-line thing:
Dim query = <sql><![CDATA[
SELECT * FROM MESSAGE_TEMPLATES
WHERE MESSAGE_SEND_DATE = :Date
]]></sql>.Value
Also note that you should be using parameterized queries as they are more secure easier to read and faster.