I have a table which have a duplicate records for only one field that is
Field A | Field B
a | djs
a | nsm
a | cns
Where field A contains duplicates
If the field contains duplicates then it shld be appended with 1,2 etc..
Is there a way of getting output as
Field A | Field B
a | djs
a1 | nsm
a2 | cns
Thank you
Related
I have two tables, with different columns that I would like to compare. There is an issue in our system with serial numbers, and I want to make sure that all of the serial numbers (CMMTTEXT - in comma delimitted form) in Table B are being transferred to Table A (SERLTNUM - where each individual serial number has its own line)
Basically, what I would like to try and do is take the SOPNUMBER's from the last 3 months (which I would get from Table C), then get all rows from Table B and Table A with the last 3 months SOPNUMBER's and then somehow to make sure all serial numbers in CMMTTEXT in Table B are in Table A as SERLTNUM.
I know how to get all of the data, but I'm not sure what I can do in order to compare the two columns in SQL when they have different data formats. I am trying to think if there is someway I can just use substr() to search CMMTTXT but don't know how I could then display rows where there was no match found.
The LNITMSEQ table is an ID that corresponds to different line items in an order.
Table A
+-----------+----------+----------+---------------+
| SOPNUMBER | LNITMSEQ | SERLTNUM | ITEMNMBR |
+-----------+----------+----------+---------------+
| I327478 | 16384 | ABC123 | someItem |
+-----------+----------+----------+---------------+
| I327478 | 32768 | DEF123 | someOtherItem |
+-----------+----------+----------+---------------+
Table B
+-----------+----------+-----------------------------+
| SOPNUMBER | LNITMSEQ | CMMTTEXT |
+-----------+----------+-----------------------------+
| I327478 | 16384 | ABC123,ABC124,ABC125,ABC126 |
+-----------+----------+-----------------------------+
| I327478 | 32768 | DEF123,DEF124,DEF125,DEF126 |
+-----------+----------+-----------------------------+
Table C
+-----------+-----------+
| SOPNUMBER | DATE |
+-----------+-----------+
| I327478 | 5/20/2017 |
+-----------+-----------+
| I327479 | 5/21/2017 |
+-----------+-----------+
I have commented above, but a clearer answer can be found here for what you need:
SQL split values to multiple rows
You can use FIND_IN_SET function like as follows
SELECT * FROM TableA INNER JOIN TAableB
ON FIND_IN_SET(TableA.SERLTNUM, TableB.CMMTTEXT) > 0
FIND_IN_SET function returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. for more detail see the manual
I have a poorly created table I want to update. It is set up as
ID
Name
Value
Because a given Name can have more than one value right now the Value field is varchar and populated with comma delimited values:
12,15,92
I would like to create an update or create table query that will make those into separate records so a table with
ID | Name | Value
1 | Bob | 5,6,9
2 | Alice| 5,9
3 | Ted | 1
ends up as
1 | Bob | 5
2 | Bob | 6
3 | Bob | 9
4 | Alice | 5
5 | Alice | 9
6 | Ted | 1
In searching online it appear this is a pretty common issue and I found one of several functions for splitting delimited fields into records here:
http://kedar.nitty-witty.com/blog/mysql-stored-procedure-split-delimited-string-into-rows
This is an old question but there is a mysql function for this, it name is group_concat, you can see the function in the offical documentation, also you can see an example here
I have 2 tables. a master table and a master_staging table. I want to know which rows in the master_staging table also appear in the master table. bellow is the structure of master_staging
--------------+------------------------+-------
id | integer |
firstname | character varying(255) |
lastname | character varying(255) |
email | text |
address | character varying(255) |
country | character varying(255) |
phones | json |
twitters | json |
linkedin | character varying(255) |
urls | json |
source | character varying(255) |
notes | json |
conflict_id | integer | **************************
businessname | character varying(255) |
warnings | json | **************************
has_warning | boolean | **************************
deleted | boolean | **************************
The structure of the master table is the exact same except it does not contain the columns with '**************************' in the third column of the above table.
One solution is to loop over all rows in the master_staging table and query the master table for rows which have the same firstname, lastname, email and from there 'manually' check if all other fields are identical.
I am hoping there is a more elegant solution which will allow me to make one run one sql statement which returns the ids of all rows in master_staging which have duplicates in master. The main problem that is stumping me is I'm not sure how to do deep json equality checks?
You can JOIN two tables on whatever fields make a record unique. For example,
Select M.id
From master M, master_staging MS
Where M.email= MS.email
and M.firstName = MS.firstName
and M.lastName = MS.lastName
So, I am working in Visual Studio 2013. And I have a table like this:
id | fk | data
----------------------
1 | 1 | something1
2 | 1 | something2
3 | 1 | something3
. | . | ...
. | . | ...
6 | 2 | ...
The fk is a foreign key to another table. I want to select all the rows which have the same fk. There is an undefined number of the same fk entries (there could be 5 rows with value 1 and 3 rows with value 2, etc.) But, I want to be able to switch said fk in my program so that when i put the DB in a Form in Visual Studio, clicking a button next would get me the next value of the fk. For example, I would like the first result to come out like this:
id | fk | data
----------------------
1 | 1 | something1
2 | 1 | something2
3 | 1 | something3
And when I click next to get me to the next entry, the result would be:
id | fk | data
----------------------
6 | 2 | ....
7 | 2 | ....
Is there a way in SQL to combine just the results with the same fk value? All of the solutions I have found so far gave me all of the fk values (all entries). I haven't found a way to get just all the entries with a single, unique value in the fk column.
You're looking for a basic SELECT statement with a WHERE clause:
SELECT
id, fk, data
FROM
your_table_name
WHERE
fk = 1
I can't help you with the VisualStudio stuff though, but you'll just have to repeat the same query incrementing the fk value in the WHERE clause
I'm not very good at joining tables in mysql and I'm still learning,
So I wanted to ask, when joining two tables....
I have 2 tables
So for the first table I want to join the 2 of its columns (id & path) on the second table.
But on the second table there's no column name id and path, there is a column name pathid & value. The field of the pathid column is the same as the id.
it looks like this.
first table
| id | path |
---------------------
| 1 | country/usa |
| 2 | country/jpn |
| 3 | country/kor |
second table
| pathid | value |
-------------------
| 3 | 500 |
| 1 | 10000 |
| 2 | 2000 |
So on the first table, it indicates that for usa the id is 1, japan is 2, korea is 3.
And on the table it says that for pathid no. 3 ( which is the id for korea) the value is 500 and so on with the others.
I want it to look like this. So then the path will be joined on the second table on its corresponding value. How can I do this on mysql? Thank You
Desired Result
| id | path | value |
------------------------------
| 1 | country/usa | 10000 |
| 2 | country/jpn | 2000 |
| 3 | country/kor | 500 |
You can join on the columns irrespective of the column name as long as the data type match.
SELECT id, path, value
FROM firstTable, secondTable
WHERE id = pathid
If you have same column names on both tables then you need to qualify the name using alias. Say the column names for id were same on both tables then whenever you use id you should mention which table you are referring to. other wise it will complain about the ambiguity.
SELECT s.id, path, value
FROM firstTable f, secondTable s
WHERE f.id = s.pathid
Note that I ommited s. on other columns in select, it will work as long as the second table doesn't have columns with same name.