Select single row from database table with one field different and all other being same - sql

I have a database table with 8 fields say Table(a,b,c,d,e,f,g,h).For some rows one of the field is different(say 4 different a values) while all other field values(b-h) in the schema are same.I have to make a table selecting just one rows from such rows with different a's but same b-h.That is I can select any one of the different a's and keep b-h same which they are and display it in table as 1 single row instead of 4.

SELECT MIN(a) a,b,c,d,e,f,g,h
FROM mytable
GROUP BY b,c,d,e,f,g,h

Related

Select a single column across an entire database

I have database (couple hundred tables) that all contains a specific column called LastReplicationDate. This column name is always the same in each table, and is always the same value within each table.
Is it possible to write a query that gets the distinct value of this column assigned to each table in my database without having to select each table via a union query?

Copy specific columns from one table to another table, and include the source tablename

I have this newly created table in SQL Server with 3 columns ID, Name, Source.
Basically this table will be populated with data from other different tables, each specifically taking in their record IDs and record Names. I believe this can be easily achieved with an INSERT INTO SELECT statement.
I would like to find out on how to populate the Source column. This column is supposed to indicate which table the data came from. For example, Source in table A has 3 records, which I then copied the ID and Name columns from this table, and put it into my destination table.
At the same time, the 3 new records will have their Source column set, indicating it came from Table A. Then I will proceed to do the same for other tables.
You can use the constant string as follows:
INSERT INTO your_table
SELECT id, name, 'TableA' as source
FROM tableA

SQL query to update one for one

I have table A with one to many relationship to table B. Whenever I do an update I create new record in table A with a new batch of records related created in table B. I had a bug in my code so when new items where being created the order of them was being reversed. I need to write a SQL query to update items in table B related to second item in table A to be the same as the ones related to first item in table A. I hope it makes any sense, will try to illustrate:
A1 -> B - 1234
A2 -> B - 4321
I want to update second set of values from table B to be the same as the ones related to A1 (1234)
You should not think of records order in database table. The only way to specify order of rows is ORDER BY clause in SELECT statement.
Make row_order or idx column in table B and put there required value for each item.
In some cases you will also get different order of B items for the first/source A record when selecting them without specifying order in ORDER BY clause.
Relational database table has no notion about neither row order nor column order.

Numbers help creating aggregates

I have a table with rows of multiple types. How do I create another table from it so that the rows in this table reflect the sum of all rows of each type from the previous table.
For e.g. I have a Table1 with 5 rows of type A, 6 rows of Type B. I want to create a table (Table2) that contains one row of type A (which is sum of all the 5 rows from Table1) and one row of type B (again this is the sum of all 6 rows from Table1).

Get fields from one column to another in Access

Below i have a table where i need to get fields from one column to three columns.
This is how i would like the data to end up
Column1
Music
Column2
com.sec.android.app.music
Column3
com.sec.android.app.music.MusicActionTabActivity
Give the table a numeric autonumber id
Remove the rows with no data with a select where blank spaces or null
Find records with no point in the content with a select
Use the previous query as a source and use the id to find the id + 1 to find the next record and do the same with + 2 to find the second row
Build a table to hold the new structure and use the query as a source to insert the new created data in the new table with the 3 columns structure.
This is an example using sql server
Test table design
Data in table
Query
Look at the query from the inside. The first query inside clean the null records. Then the second query find the records with out point. This records are the reference to find the related two records. Then the id of the records with out point are used to make a query in the select adding 1 for the next record and then other query adding 2 to find the other record. Now you only need to create a table to insert this data, using this query as the source.