Have the following table which i am trying to concat two rows from the 'value' column into one
mysql> select * from table;
+-----+---------+----------+------------------------+
| id | rsvp_id | field_id | value |
+-----+---------+----------+------------------------+
| 181 | 37 | 1 | First |
| 184 | 37 | 4 | Last |
| 187 | 37 | 10 | |
| 190 | 37 | 13 | spicegirls |
| 193 | 37 | 7 | mark#test2.com |
| 196 | 40 | 1 | Brian |
| 199 | 40 | 1 | Smith |
| 202 | 40 | 7 | Brian#test .com |
| 205 | 40 | 10 | BBQ |
+-----+---------+----------+------------------------+
9 rows in set (0.00 sec)
Ideally i'd like to get the following result
rsvp_id | value
======== ========
37 First Last
40 Brian Smith
The query only grabs the rows with field_id=1 then concats the value column and creates a new row with rsvp_id and the concat value.
Also, the field_id column right now and the 1 is an example, i'll have to figure out how to make it work so instead of 1 it takes the condition from a different table.
Basically the above are values for first name and last name. field_id is a foreign_key to a different table.
I've tried searching online and messing with it myself but i wasn't able to merge the two rows into one row.
Thank You.
You have to use grouping and then use GROUP_CONCAT.
Something like this (untested) might work:
SELECT rsvp_id, GROUP_CONCAT(value SEPARATOR ' ')
FROM t
WHERE field_id = 1
GROUP BY rsvp_id
See MySQL docs for details, also to learn about ordering of values etc:
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_group-concat
Related
In MS Access database, I'm working with a table that has rows. In some cases not all columns of the rows are filled. I want to create an update query to update the values of the empty fields with data from other rows where the column is not empty.
googled the question but no satisfying answer has been found. Can someone show me how the query should be build?
Current table looks like
| Invoicenumber | Customer | Date |
|---------------|----------|---------|
| 5 | 12 | 12-6-19 |
| 5 | | 12-6-19 |
| 5 | | 12-6-19 |
| 5 | | 12-6-19 |
| 6 | 18 | 15-6-19 |
| 6 | | 15-6-19 |
| 6 | | 15-6-19 |
| 7 | 20 | 20-6-19 |
| 7 | | 20-6-19 |
I need the table to look like this after updating:
| Invoicenumber | Customer | Date |
|---------------|----------|---------|
| 5 | 12 | 12-6-19 |
| 5 | 12 | 12-6-19 |
| 5 | 12 | 12-6-19 |
| 5 | 12 | 12-6-19 |
| 6 | 18 | 15-6-19 |
| 6 | 18 | 15-6-19 |
| 6 | 18 | 15-6-19 |
| 7 | 20 | 20-6-19 |
| 7 | 20 | 20-6-19 |
You can do it with just SQL by joining the table to itself:
UPDATE
Invoices
INNER JOIN Invoices AS Inv2
ON Invoices.InvoiceNumber = Inv2.InvoiceNumber
SET
Invoices.Customer = Inv2.Customer
WHERE
(Invoices.[Customer] Is Null)
AND (Inv2.Customer IS NOT NULL)
You can place a Switch statement into the update to choose the value to update based on the state of the CUSTOMER field.
Update TestTable
Set CUSTOMER = Switch(CUSTOMER is Null,OTHER_FIELD,CUSTOMER = '',OTHER_FIELD,CUSTOMER<>'',CUSTOMER)
This statement will update the CUSTOMER field to the OTHER_FIELD where CUSTOMER is blank or CUSTOMER is Null. If CUSTOMER has a value, it sets it to that value (essentially leaving it as the same.)
The 'SWITCH' statement is Access's version of a 'Switch' or 'Select Case' in code, where the first parameter is the condition to check, the parameter after it is the value to take if the previous condition returns true. So...
Switch(1=1,'YES',2=1,'NO', case 3, return 3, case 4, return 4, etc., etc.,)
would return the 'YES' because 1 is equal to 1.
There would be a more eloquent way to do this with code, but in an Access query, I don't know any other way.
Try using domain aggregate function to pull the Customer:
UPDATE table1 SET table1.Customer = DMax("Customer","table1","Invoicenumber=" & [Invoicenumber])
WHERE (((table1.Customer) Is Null));
Question answered by solution mentioned by #John Mo. Used his code to update table with data available within the table.
My Table structure is like this
-------------------------------------
| from | to | price | pid | type|
| 0 | 100 | 50 | 2 | new |
| 101 | 200 | 60 | 2 | new |
| 201 | 300 | 70 | 2 | new |
| 301 | 700 | 80 | 2 | new |
| 301 | 700 | 70 | 2 | old |
Now i am getting a value from that i have to get the price.
Let say my value is 106 then i should get the price 60.
My query is
SELECT * FROM tbl_goals WHERE from <= '106' AND to >='106' AND pid='2' AND type='new'
With this query i am getting all rows with type new
But in result i should get this row
| 101 | 200 | 60 | 2 | new |
Where i am doing wrong?
I would use BETWEEN here for the sake of readability:
SELECT *
FROM tbl_goals
WHERE 106 BETWEEN `from` AND `to` AND
pid = 2 AND
type = 'new'
Notes:
Don't put single quotes around numbers in your query when you want to treat them as numbers
Don't names your columns from or any other MySQL keyword, because then you might have to escape them everywhere in backticks to make the query work as intended
I have a working SQL query which returns the following results:
What I'm looking to be able to do is for MySQL to calculate the sum of the weight column and present combinations of rows from the above table in which the sum(Weight) <= 300. An example of the expected results using the table above would be:
My questions about this are: is this possible from with MySQL? Would I need to execute multiple SQL queries and how would I be able to produce the results illustrated above? Is it possible to achieve the first table and the combinations from one query?
Disclaimer: I'm not sure how exactly you envision returning 3 result sets from one query, and why there are only three -- (1,4) and (2,3) would also be valid combinations. So, I'll assume it was just a general example, and you want the complete result in some form.
Let's say you have this table (I've added one row to make it more generic, you example would only produce 2-element combinations):
MariaDB [test]> SELECT * FROM t1;
+------+--------+
| id | weight |
+------+--------+
| 1 | 100 |
| 2 | 120 |
| 3 | 200 |
| 4 | 96 |
| 5 | 50 |
+------+--------+
5 rows in set (0.00 sec)
With MariaDB 10.2, you can use a recursive CTE to achieve your goal, e.g.
WITH RECURSIVE comb(id,ids,weights,sumweight) AS (
SELECT
id,
CAST(t1.id AS CHAR) AS ids,
CAST(weight AS CHAR) AS weights,
weight AS sumweight
FROM t1
WHERE weight <= 300
UNION
SELECT
t1.id AS id,
CONCAT(comb.ids,',',t1.id) AS ids,
CONCAT(comb.weights,',',weight) AS weights,
t1.weight + comb.sumweight AS sumweight
FROM t1 JOIN comb ON (comb.id < t1.id)
HAVING sumweight <= 300
) SELECT ids, weights, sumweight FROM comb;
You'll get this:
+-------+------------+-----------+
| ids | weights | sumweight |
+-------+------------+-----------+
| 1 | 100 | 100 |
| 2 | 120 | 120 |
| 3 | 200 | 200 |
| 4 | 96 | 96 |
| 5 | 50 | 50 |
| 1,2 | 100,120 | 220 |
| 1,3 | 100,200 | 300 |
| 1,4 | 100,96 | 196 |
| 1,5 | 100,50 | 150 |
| 2,4 | 120,96 | 216 |
| 2,5 | 120,50 | 170 |
| 3,4 | 200,96 | 296 |
| 3,5 | 200,50 | 250 |
| 4,5 | 96,50 | 146 |
| 1,2,5 | 100,120,50 | 270 |
| 1,4,5 | 100,96,50 | 246 |
| 2,4,5 | 120,96,50 | 266 |
+-------+------------+-----------+
17 rows in set (0.00 sec)
The query above is not perfect, it is just to give an idea of the possible solution. The result seems correct, and you can improve and polish representation according to your needs.
For your second question, "Is it possible to achieve the first table and the combinations from one query?", you didn't say how you got the first table, so it's hard to give a precise example, but in any case it surely should be possible. The most obvious way is to take whatever query you used to get that result set, wrap it into a view, and then use this view instead of t1 table in the above example.
I am trying to find a way to output a calculation row (or two) of an SQL search query, so I may see the raw results along with a calculation associated to them, either above or under the listing of raw results. For instance, I have the following data:
mysql> select * from data [where condition];
+----+--------+-----+--------+
| id | height | age | weight |
+----+--------+-----+--------+
| 1 | 65.2 | 45 | 45.23 |
| 2 | 63.1 | 47 | 0 |
| 3 | 59.2 | 37 | 38.1 |
| 4 | 59.8 | 39 | 36.4 |
| 5 | 63.4 | 37 | 38.1 |
| 6 | 72.1 | 34 | 2 |
| 7 | 100 | 50 | 20 |
+----+--------+-----+--------+
And what I want is to be able to perform any query to get all or a subset of this data, but have the resulting table give something like the following (with the summary/calculation output separate from the raw data, such as either above or below it):
mysql> query???
+--------+--------+------+--------+
| id | height | age | weight |
+--------+--------+------+--------+
| 1 | 65.2 | 45 | 45.23 |
| 2 | 63.1 | 47 | 0 |
| 3 | 59.2 | 37 | 38.1 |
| 4 | 59.8 | 39 | 36.4 |
| 5 | 63.4 | 37 | 38.1 |
| 6 | 72.1 | 34 | 2 |
| 7 | 100 | 50 | 20 |
+--------+--------+------+--------+
| STDDEV | 13.26 | 5.57 | 17.15 |
| COUNT | 7 | 7 | 7 |
| etc. | etc. | etc. | etc. |
+--------+--------+------+--------+
I've found some approaches such as this (http://www.sqlservercurry.com/2011/06/sql-server-row-summary-column-summary.html) that somewhat do it, but because the calculation acts on all rows it doesn't work well for some calculations (for instance, using stddev results in "0" for everything except the calculation row).
I can create a separate result of calculations such as the following, but it would be nice to have them somehow combined, such as shown above. In addition, the following only outputs one row of calculations, and it would be nice to have several rows of pertinent calculations.
select stddev(height), stddev(age), stddev(weight) from data [where condition];
The point here is to perform any search query and get an auto-generated preview of basic descriptive information from the results (deviations, counts, mean, etc.). Hopefully this can be done directly in SQL, without needing to use another language/API.
Combine your results with union. If you need the results in a particular order, then create a column containing the precedence and order by that column.
select id, height, age, weight, 0 sortorder
from data [where condition]
union all
select 'stdev count', stddev(height), stddev(age), stddev(weight), 1
from data [where condition]
order by sortorder
Objective: Find all rows where (1) the number of messages for a number is 1, and (2) the length of the message is less than 5 characters in length. I can do each separately, but having difficulty when I combine the two conditions in one SQL query.
Sample Database Table:
+-----+----------+----------+
| id | number | message |
+-----+----------+----------+
| 1 | 100 | Test |
| 2 | 100 | Testing |
| 3 | 100 | Testing |
| 4 | 200 | Test |
| 5 | 201 | Test |
| 6 | 201 | Test |
| 7 | 250 | Testing |
| 8 | 251 | Test |
| 9 | 300 | Testing |
| 10 | 300 | Testing |
+-----+----------+----------+
Should just return rows 200 and 251. Tried the following, but no luck:
SELECT * FROM `reports` WHERE LENGTH(message) < 5 GROUP BY number HAVING count(*) = '1'
Returns rows but rows contains counts > 1.
Ok, I made the wrong change a few hrs ago - this time I have the data just like yours. Ah, and NOW I see what you're having a problem with. You don't understand the order MySQL interprets your SELECT; it's first doing the WHERE to limit the results THEN it does the GROUP BY. Working as designed.