I have a sql table Product:
id name value type
1 a abs#123 1
2 b abs#123 2
3 c abs#123 1
How can I update the value column for the products type 1 with the value before # , meaning that abs#123 will be abs, so I have to split the value column by #?
Use Left and charindex function. Try this.
update Product
set value=left(value,charindex('#',value)-1)
where type=1
or use Substring
update Product
set value=substring(value,1,charindex('#',value)-1)
where type=1
Related
I've created a simple table - table1.
thre is only one talbe (table1)
the table1 has two fields: [table1].[id] and [table1].[method]
the RowSourceType of [table1].[method] is - 'Value list'
the Row Source of [table1].[method] is is ' 1;"A";35;"B";2;"C";3;"D" ' (so two columns).
RowSourceType - Value list
Row Source - 1;"A";35;"B";2;"C";3;"D"
I've populated table1 with rows:
id
method
1
35
2
2
3
1
I'm loking for a query to receive result:
[table1].[id]
[table1].[ method]
1
B
2
C
3
A
(I'd like to avoid to add lookup table)
Thank You in advance.
rgds
You can use Switch:
Select
id,
Switch([method]=1,"A",[method]=35,"B",[method]=2,"C",[method]=3,"D") As MethodCode
From
table1
As #Gustav suggested and perhaps you didn't understand, first step is to create a lookup table, second step is to use that as your RowSource, third step is to build your query
Benefits of this approach is that you don't need to change your Rowsource every time you make a change to the Lookup List
1. Create Lookup Table to match your rowsource
LookupID
LookupValue
1
A
35
B
2
C
3
D
2-a. Change Rowsource of your input field
In Properties | Data
Set Rowsource to
SELECT LookupID, LookupValue FROM LookupTable
Set RowsourceType to
Table/Query
2-b Still in Properties | Format, set up columns and Hide the ID field
ColumnCount =2
ColumnWidths = 0;3cm
3. Build your final query
SELECT Table1.ID, LookupTable.LookupValue
FROM Table1 INNER JOIN LookupTable ON Table1.metod = LookupTable.LookupID;
Results of Query
ID
LookupValue
1
B
2
C
3
A
id value
1 a
2 b
3 c
How do i add second value 'z' to id=1 (separated by comma)?
id value
1 a,z
2 b
3 c
and how to remove the 'z' now if i have that final table?
You can use update:
update t
set value = concat(value, ',z')
where id = 1;
To answer your secondary question, yes.
If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.
The best and simplest way to do this is the following query according to me :
update table1 set value = concat(value,'z') where id = 1
where : Table1 is the name of your table.
Data
id cust_name
1 walmart_ca
2 ikea_mo
2 ikea_ca
2 ikea_in
1 walmart_in
when i do
select id,cust_name from test where id=2
Query returns below output
id cust_name
2 ikea_mo
2 ikea_ca
2 ikea_in
How can i get or store the result as single column value as shown below
id cust_name
2 {ikea_mo,ikea_ca,ikea_in}
you should use string_agg function and here is an example for it
select string_agg(field_1,',') from db.schema.table;
you should mention the separator in your case its , so I am doing string_agg(field_1,',')
How do I not get all the ID's grouped, but instead listed from first to last; with all their respective values in the columns next to them?
Instead of grouping it, it should show ID 1 and its value, ID 2 and its value. EVEN if the values for the ID is the same. I tried removing the GROUP_CONCAT, but then it's only showing one ID per customfield_value?
SELECT GROUP_CONCAT(virtuemart_product_id), customfield_value, COUNT(*) c
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
GROUP BY customfield_value
HAVING c > 1
It's working currently, but grouping the ID's and spacing them with a comma. Should just display as in a normal table/list format.
Currently it shows like this(as you can see, it's ALL the same ICOS number, but different ID's. I ONLY need to display the values WHERE the ICOS NUMBER is "duplicate"):
ID ICOS Count
1,2,3 775896 3
It should be displaying like this:
ID ICOS Count
1 775896 1
2 775896 1
3 775896 1
All rows where the customfield_value is not unique:
-- Assuming MySQL
SELECT virtuemart_product_id, customfield_value
, COUNT(*) c -- maybe not needed
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
AND customfield_value IN
( SELECT customfield_value
FROM jos_virtuemart_product_customfields
WHERE virtuemart_custom_id = 6
GROUP BY customfield_value
HAVING COUNT(*) > 1 -- more than one row exists
)
GROUP BY virtuemart_product_id, customfield_value -- maybe not needed
If the virtuemart_product_id is unique you don't need the outer count/group by as it will always be 1.
I have a set of Data in MS Access
Number Owner
1 Heelo
1 Hi
1 There
2 What
2 Up
This needs to be transferrid into
Number Owner1 Owner2 Owner3 Owner4
1 Heelo Hi There -
2 What Up - -
Any idea on how to go on with this?
The crux in this case is we don't have a third column from where we can pivot the data.
You could add a third column with a sequence of numbers:
SELECT Number, (select count(*)
from YourTable as s
where s.number = t.number) as sequence, owner
from YourTable as t
then apply this solution to the results: SQL to transpose row pairs to columns in MS ACCESS database