Querying 2 tables for all results - sql

I have a sql qustion. its suppose to be simple but i cant figure it out.
I have 2 tables:
**customer1 -**
id name
1 aaa
2 bbb
3 ccc
4 ddd
**customer2**-
id name
1 aaa
2 bbb
5 eee
6 fff
I need to get ALL the customers in both tables.
i need to query in a way that if a customer is on both tables I will see him twice in the results (e.g aaa, bbb), and all the rest only one.
Thanks a lot.

select id, name from customer1
union all
select id, name from customer2

Pay attention to UNION and UNION ALL , with the first one you will not get duplicate , with the second one you will get even duplicate as result of your query.
Let's talk about the performance :
A UNION is highly optimized and really fast, except in cases where one query finishes long before the other, and you have to wait to get the whole result set.
When you don't mind about duplicates , UNION ALL will be faster.

I would have put this as a comment to : Raghavendra Kedlaya post; but it wouldn't let me.
I like to know from whence the data came when using union all..
SELECT id, name, 'Customer1' as src
FROM customer1
UNION ALL
SELECT id, name, 'Customer2' as src
FROM customer2

Related

merging multiple rows into one based on id

i have the data in this format in an amazon redshift database:
id
answer
1
house
1
apple
1
moon
1
money
2
123
2
xyz
2
abc
and what i am looking for would be:
id
answer
1
house, apple, moon, money
2
123, xyz, abc
any idea? the thing is that i cannot hard code the answers as they will be variable, so preferably a solution that would simply scoop the answers for each id's row and put them together separated by a delimiter.
you can use aggregate function listagg:
select id , listagg(answer,',')
from table
group by id
You can use string_agg(concat(answer,''),',') with group by so it will be like that:
select id , string_agg(concat(answer,''),',') as answer
from table
group by id
tested here
Edit:
you don't need concatenate, you can just use string_agg(answer,',')

Returning all the friends connected with a user

I was looking to see where I can improve on my query to get all the friends of a user. I was originally thinking of inserting 2 rows per add in order to run
SELECT *
FROM friends
WHERE friends.requesterId={id};
Problem with this approach is the result of N^(N-1) since I'll have double the insertion. I was hoping to use a join or union but I might not understand it correctly. I have tried doing
SELECT *
FROM "friends"
WHERE "requesterId"=2
UNION
SELECT *
FROM "friends"
WHERE "receiverId"=2
This results in a problem of itself where one row becomes
id
requesterId
receiverId
status
7
2
4
"Accepted"
1
1
2
"Accepted"
6
2
3
"Accepted"
As we can see, if we were to depend on receiverId alone to list our friends, it'll result in us also saying we friended ourselves.
Any idea or advice on what I should consider?
Thank you.
Just don't include the column where you already know what the value will be (the 'self' value).
SELECT id, receiverId AS friendID, 1 AS is_my_request, status
FROM friends
WHERE requesterId = 2
UNION ALL
SELECT id, requesterId AS friendID, 0 AS is_my_request, status
FROM friends
WHERE receiverId = 2
Note, I also changed the column name, to just being the friendID, and added a column to say if "I" made the rquest or not.
I also changed UNION to UNION ALL. That's because UNION uses compute resources to check for and remove duplicates. In this case that would be asted resource.

Select value from one column based on three criteria and display each criteria in each column

How to select the column value based on three criteria. I want each criteria display in each column. Is it possible?
Database is Ms-Access
My Table is:
ID Name Key Keyer_Type
1 xxx 565 Beginner
8 yyy 456 Beginner
1 xxx 4589 Skill
9 zzz 89 Beginner
2 uuu 8789 Expert
8 yyy 5689 Skill
Using distinct and groupby clause I select the unique vaue in ID no and corresponding name.
I want to select the key based on beginner, skill, expert on separeate column.
My need my output as:
ID Name Beginner Skill Expert#
1 xxx 565 4589
8 yyy 456 5689
9 zzz 89
2 uuu 8789
How to display the Beginner, skill, expert as a separate column from Keying level column.
Not sure it is exactly what you need, but I think this is close to the answer you need:
TRANSFORM First(TableName.Key)
SELECT TableName.ID, TableName.Name
FROM TableName
GROUP BY TableName.ID, TableName.Name
PIVOT TableName.Keyer_Type;
Edit as per the comment:
This will return a grouping on the ID, and the first corresponding name.
TRANSFORM First(Table2.Key)
SELECT Table2.ID,First( Table2.Name)
FROM Table2
GROUP BY Table2.ID
PIVOT Table2.Keyer_Type;
But it is not clear to me what the meaning of Name is.

How can I combine multiple columns of the same data into a single column?

I have an issue here that is arising from poor data formatting (not on my behalf). I had a large CSV file downloaded from an external entity with nation wide data - it has about 5,000,000+ rows so that its too large of a file to open, let alone manually manipulate the data. I did get it uploaded to our SQL database, but getting the data into a usable format is difficult; each row has 10 different category codes, and can have multiple codes in each category. Unfortunately, they added new columns to handle this instead of adding a new row. Its tough to describe without an example:
ID A_Code1 A_Code2 A_Code3 B_Code1 B_Code2 B_Code3
1 123 765 654 qwe asd zxc
2 987 345 567 poi lkj mnb
and this is what I need:
ID A_Code B_Code
1 123 qwe
1 765 asd
1 654 zxc
2 987 poi
2 345 lkj
2 567 mnb
The way it is set up now makes querying nearly impossible as there are about 10 different types of on each row, and there are 10 columns for each code type. This means I have to query 100 different columns when I should only have to query 10.
If somebody knows a way to do this, it would be greatly appreciated. I have not been able to find anything like this so far, so I am getting desperate!
Thank you!
You need to unpivot the multiple columns of data into multiple rows, depending on your version of SQL Server there are several ways to get the result.
You can use CROSS APPLY and UNION ALL if using SQL Server 2005+:
select id, A_Code, B_Code
from yourtable
cross apply
(
select A_Code1, B_Code1 union all
select A_Code2, B_Code2 union all
select A_Code3, B_Code3
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
You can also use CROSS APPLY with VALUES if using SQL Server 2008+:
select id, A_Code, B_Code
from yourtable
cross apply
(
values
(A_Code1, B_Code1),
(A_Code2, B_Code2),
(A_Code3, B_Code3)
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
This allows you to convert the columns into rows in pairs - meaning A_Code1 and B_Code1 will be matched in the final result.
You could also use a UNION ALL:
select id, A_Code = A_Code1, B_Code = B_Code1
from yourtable
union all
select id, A_Code = A_Code2, B_Code = B_Code2
from yourtable
union all
select id, A_Code = A_Code3, B_Code = B_Code3
from yourtable ;
See SQL Fiddle with Demo

How to group by a column

Hi I know how to use the group by clause for sql. I am not sure how to explain this so Ill draw some charts. Here is my original data:
Name Location
----------------------
user1 1
user1 9
user1 3
user2 1
user2 10
user3 97
Here is the output I need
Name Location
----------------------
user1 1
9
3
user2 1
10
user3 97
Is this even possible?
The normal method for this is to handle it in the presentation layer, not the database layer.
Reasons:
The Name field is a property of that data row
If you leave the Name out, how do you know what Location goes with which name?
You are implicitly relying on the order of the data, which in SQL is a very bad practice (since there is no inherent ordering to the returned data)
Any solution will need to involve a cursor or a loop, which is not what SQL is optimized for - it likes working in SETS not on individual rows
Hope this helps
SELECT A.FINAL_NAME, A.LOCATION
FROM (SELECT DISTINCT DECODE((LAG(YT.NAME, 1) OVER(ORDER BY YT.NAME)),
YT.NAME,
NULL,
YT.NAME) AS FINAL_NAME,
YT.NAME,
YT.LOCATION
FROM YOUR_TABLE_7 YT) A
As Jirka correctly pointed out, I was using the Outer select, distinct and raw Name unnecessarily. My mistake was that as I used DISTINCT , I got the resulted sorted like
1 1
2 user2 1
3 user3 97
4 user1 1
5 3
6 9
7 10
I wanted to avoid output like this.
Hence I added the raw id and outer select
However , removing the DISTINCT solves the problem.
Hence only this much is enough
SELECT DECODE((LAG(YT.NAME, 1) OVER(ORDER BY YT.NAME)),
YT.NAME,
NULL,
YT.NAME) AS FINAL_NAME,
YT.LOCATION
FROM SO_BUFFER_TABLE_7 YT
Thanks Jirka
If you're using straight SQL*Plus to make your report (don't laugh, you can do some pretty cool stuff with it), you can do this with the BREAK command:
SQL> break on name
SQL> WITH q AS (
SELECT 'user1' NAME, 1 LOCATION FROM dual
UNION ALL
SELECT 'user1', 9 FROM dual
UNION ALL
SELECT 'user1', 3 FROM dual
UNION ALL
SELECT 'user2', 1 FROM dual
UNION ALL
SELECT 'user2', 10 FROM dual
UNION ALL
SELECT 'user3', 97 FROM dual
)
SELECT NAME,LOCATION
FROM q
ORDER BY name;
NAME LOCATION
----- ----------
user1 1
9
3
user2 1
10
user3 97
6 rows selected.
SQL>
I cannot but agree with the other commenters that this kind of problem does not look like it should ever be solved using SQL, but let us face it anyway.
SELECT
CASE main.name WHERE preceding_id IS NULL THEN main.name ELSE null END,
main.location
FROM mytable main LEFT JOIN mytable preceding
ON main.name = preceding.name AND MIN(preceding.id) < main.id
GROUP BY main.id, main.name, main.location, preceding.name
ORDER BY main.id
The GROUP BY clause is not responsible for the grouping job, at least not directly. In the first approximation, an outer join to the same table (LEFT JOIN below) can be used to determine on which row a particular value occurs for the first time. This is what we are after. This assumes that there are some unique id values that make it possible to arbitrarily order all the records. (The ORDER BY clause does NOT do this; it orders the output, not the input of the whole computation, but it is still necessary to make sure that the output is presented correctly, because the remaining SQL does not imply any particular order of processing.)
As you can see, there is still a GROUP BY clause in the SQL, but with a perhaps unexpected purpose. Its job is to "undo" a side effect of the LEFT JOIN, which is duplication of all main records that have many "preceding" ( = successfully joined) records.
This is quite normal with GROUP BY. The typical effect of a GROUP BY clause is a reduction of the number of records; and impossibility to query or test columns NOT listed in the GROUP BY clause, except through aggregate functions like COUNT, MIN, MAX, or SUM. This is because these columns really represent "groups of values" due to the GROUP BY, not just specific values.
If you are using SQL*Plus, use the BREAK function. In this case, break on NAME.
If you are using another reporting tool, you may be able to compare the "name" field to the previous record and suppress printing when they are equal.
If you use GROUP BY, output rows are sorted according to the GROUP BY columns as if you had an ORDER BY for the same columns. To avoid the overhead of sorting that GROUP BY produces, add ORDER BY NULL:
SELECT a, COUNT(b) FROM test_table GROUP BY a ORDER BY NULL;
Relying on implicit GROUP BY sorting in MySQL 5.6 is deprecated. To achieve a specific sort order of grouped results, it is preferable to use an explicit ORDER BY clause. GROUP BY sorting is a MySQL extension that may change in a future release; for example, to make it possible for the optimizer to order groupings in whatever manner it deems most efficient and to avoid the sorting overhead.
For full information - http://academy.comingweek.com/sql-groupby-clause/
SQL GROUP BY STATEMENT
SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups.
Syntax:
1. SELECT column_nm, aggregate_function(column_nm) FROM table_nm WHERE column_nm operator value GROUP BY column_nm;
Example :
To understand the GROUP BY clauserefer the sample database.Below table showing fields from “order” table:
1. |EMPORD_ID|employee1ID|customerID|shippers_ID|
Below table showing fields from “shipper” table:
1. | shippers_ID| shippers_Name |
Below table showing fields from “table_emp1” table:
1. | employee1ID| first1_nm | last1_nm |
Example :
To find the number of orders sent by each shipper.
1. SELECT shipper.shippers_Name, COUNT (orders.EMPORD_ID) AS No_of_orders FROM orders LEFT JOIN shipper ON orders.shippers_ID = shipper.shippers_ID GROUP BY shippers_Name;
1. | shippers_Name | No_of_orders |
Example :
To use GROUP BY statement on more than one column.
1. SELECT shipper.shippers_Name, table_emp1.last1_nm, COUNT (orders.EMPORD_ID) AS No_of_orders FROM ((orders INNER JOIN shipper ON orders.shippers_ID=shipper.shippers_ID) INNER JOIN table_emp1 ON orders.employee1ID = table_emp1.employee1ID)
2. GROUP BY shippers_Name,last1_nm;
| shippers_Name | last1_nm |No_of_orders |
for more clarification refer my link
http://academy.comingweek.com/sql-groupby-clause/