I have a table data with have 100.000 rows+ and its content like below;
Service Owner
ABC JOHN
ABC MARK
ABC MARK
ABC STEVE
ABC STEVE
The output what i want is like below. Only getting unique values for services;
Service Owner
ABC JOHN
ABC MARK
ABC STEVE
How can i select the query?
An aggregate query should do the trick:
SELECT MIN(service), owner
FROM mytable
GROUP BY owner
Selecting the distinct service and owner will give you the unique combinations of service and owner.
SELECT DISTINCT service,owner FROM TABLE
Here's a SQLFiddle.
Related
I have a table that contains data like below:
Name
ID
Dept
Joe
1001
Accounting
Joe
1001
Marketing
Mary
1003
Administration
Mary
1009
Accounting
Each row is uniquely identified with a combo of Name and ID. I want the resulting table to combine rows that have same Name and ID and put their dept's together separated by a comma in alpha order. So the result would be:
Name
ID
Dept
Joe
1001
Accounting, Marketing
Mary
1003
Administration
Mary
1009
Accounting
I am not sure how to approach this. So far I have this, which doesn't really do what I need:
SELECT Name, ID, COUNT(*)
FROM employees
GROUP BY Name, ID
I know COUNT(*) is irrelevant here, but I am not sure what to do. Any help is appreciated! By the way, I am using PostgreSQL and I am new to the language.
Apparently there is an aggregate function for string concatenation with PostgreSQL. Find documentation here. Try the following:
SELECT Name, ID, string_agg(Dept, ', ' ORDER BY Dept ASC) AS Departments
FROM employees
GROUP BY Name, ID
I have 2 tables.
One is Admin Table with all valid combinations and other is a Transaction table with the transactions.
I am trying to write a query which will give me the result of invalid combinations from transaction table.
The query should return the invalid transactions.
As you can see in the example below ABCD - TUV and IJKL - EFG are not valid combinations.
Admin Table
Column A Column B
ABCD XYZ
ABCD EFG
EFGH XYZ
IJKL TUV
IJKL XYZ
Alternatively you can use NOT EXISTS e.g.
SELECT * FROM TRANSACTIONS T WHERE NOT EXISTS (SELECT 1
FROM ADMIN
WHERE COMBINATION = T.COMBINATION)
Wether to use EXISTS or NOT IN depends largely on the data you're querying.
you can find more info on the topic here
https://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:953229842074
There is no example in your question, but you should use NOT IN.
For example :
SELECT * FROM TRANSACTIONS WHERE combination NOT IN (SELECT combination FROM ADMIN)
I would like to create a table that takes the distinct combinations of 2 columns and only returns those that are greater than one.
What I have so far creates a table of all companies and contracts associated with them.
Company Contract
------- --------
ABC 0000111
ABC 0000113
DEF 0000124
GHI 0000207
GHI 0000389
GHI 0000567
It also creates a second table that finds distinct combinations of those columan and creates a list of how many times they appear.
Company Countr
------- --------
GHI 3
ABC 2
I am trying to make a third table that returns just those rows that have companies matched with more than one contract.
Company Contract
------- --------
ABC 0000111
ABC 0000113
GHI 0000207
GHI 0000389
GHI 0000567
Modified source, if it helps
SELECT DISTINCT inventory.company, inventory.contracts
FROM inventory
WHERE inventory.company not in ('Company Name')
(
SELECT
DISTINCT inventory.company,
count(DISTINCT(concat(inventory.company, inventory.contracts))) AS Countr
FROM inventory
WHERE
inventory.company not in ('Company Name')
GROUP BY company
HAVING count(DISTINCT(concat(inventory.company, inventory.contracts))) > 1
)
ORDER BY Countr DESC
Something like this should work:
SELECT * FROM inventory WHERE company in
(SELECT company FROM inventory GROUP BY company HAVING count(distinct contract)>1);
The sub-query just creates a list of companys that have more than one contract associated with them. Then selects all data inventory that matches those companys. The DISTINCT modifier in the Count in the HAVING clause makes sure that there are multiple distinct contracts
Here's a link to the functional schema and query
I am not sure if it is necessary to create more than one table. Considering the information you gave I would do it as follows:
Create a table with company and contract information and call it TABLE
Company Contract
------- --------
ABC 0000111
ABC 0000113
DEF 0000124
GHI 0000207
GHI 0000389
GHI 0000567
Then I would create a view on that table as follows and call it VIEW1; the columns are called COMPANY and CONTRACT_COUNT, for example:
select company, count(contract) from TABLE group by company;
Then I would create another view, VIEW2, with different query:
select distinct TABLE.company, TABLE.contract from TABLE, VIEW1 where TABLE.COMPANY=VIEW1.COMPANY and CONTRACT_COUNT > 1
I have 2 tables storing information. For example:
Table 1 contains persons:
ID NAME CITY
1 BOB 1
2 JANE 1
3 FRED 2
The CITY is a id to a different table:
ID NAME
1 Amsterdam
2 London
The problem is that i want to insert data that i receive in the format:
ID NAME CITY
1 PETER Amsterdam
2 KEES London
3 FRED London
Given that the list of Cities is complete (i never receive a city that is not in my list) how can i insert the (new/received from outside)persons into the table with the right ID for the city?
Should i replace them before I try to insert them, or is there a performance friendly (i might have to insert thousands of lines at one) way to make the SQL do this for me?
The SQL server i'm using is Microsoft SQL Server 2012
First, load the data to be inserted into a table.
Then, you can just use a join:
insert into persons(id, name, city)
select st.id, st.name, c.d
from #StagingTable st left join
cities c
on st.city = c.name;
Note: The persons.id should probably be an identity column so it wouldn't be necessary to insert it.
insert into persons (ID,NAME,CITY) //you dont need to include ID if it is auto increment
values
(1,'BOB',(select Name from city where ID=1)) //another select query is getting Name from city table
if you want to add 1000 rows at a time that'd be great if you use stored procedure like this link
I have two fields, tblIT.Person_Name and tblEng.Full_Name. I want to create a new column that contains all of the names from the first table and adds them to the second column and places it all into one main column
Person_name
-------------
John Smith |
-------------
Gary Porter |
Full_name
-------------
Gary Porter |
-------------
Nancy Becker|
I am looking for this:
People
-------------
Gary Porter |
-------------
Nancy Becker|
-------------
John Smith |
It is not concatenating, which a lot of people have been telling me, it is simply joining the two tables.
Create a UNION of the 2 tables. This will weed out duplicate values.
SELECT Person_name AS People FROM tblIT
UNION
SELECT Full_Name FROM tblEng
You would use UNION ALL if you wanted all the rows from both tables, including the duplicates. But it looks like you don't want duplicates.
The result set will not be editable.
If you want to store those in a column in another table, you can insert the results from the UNION query. The UNION keyword may cause a syntax error with a simple INSERT statement, so include the UNION as a subquery in the INSERT.
INSERT INTO YourTable (People)
SELECT sub.People
FROM
(
SELECT Person_name AS People FROM tblIT
UNION
SELECT Full_Name FROM tblEng
) AS sub
use UNION (without the ALL because it will allow duplicate) to merge all the results.
SELECT col1 FROM Person_name
UNION
SELECT col1 FROM Full_name
It's combining the results, not joining the results :)