appending 2 columns into one list in sql - sql

I have 2 tables and each table has some 3 columns. i want to get one column such that one column from each table are apended one after the other
eg:- suppose one column in a table contains hai, how, are, you.
and another column in another column contains i, am, fine.
i want a query which gives hai, how, are, you,i,am,fine. in just one column
can anybody give a query for this in sql...

If I understand your schema correctly you have this
Table1: Column1
hai,
how,
are,
you.
Table2: Column2
i,
am,
fine.
Do This:
Insert Into Table1 (Column1)
Select Column2 From Table2
You will get this:
Table1: Column1
hai,
how,
are,
you.
i,
am,
fine.
If you have 3 Columns
Then just do this:
Insert Into Table1 (Column1, Column2, Column3) //the (Column1, Column2, Column3) is not neccessary if those are the only columns in your Table1
Select Column1, Column2, Column3 From Table2 //the Select Column1, Column2, Column3 could become Select * if those are the only columns of your Table2
EDIT: Do this if you don't want to modify any tables.
Select Column1, Column2, Column3
From Table1
UNION ALL
Select Column1, Column2, Column3
From Table2

Your question isn't very clear. One interpretation of it is that you want to UNION the two:
select column
from table1
union
select column
from table2;
If you really want all rows from both tables (and not the distinct values), UNION ALL will be faster than UNION.
If you want the rows in a certain order be sure to specify an ORDER BY clause.

Related

SQL Query - Ensuring my code is correct/Assistance with multiple queries in one Query (Subquerying)

I have a query today regarding SQL.
Basically here is what I am trying to do (this will also be useful for a couple other tables I have in this DB)
Table 1 = Members
Table 2 = Payments
Essentially trying to insert record into 1 table however have the query also copy over a memberID field if it's present for the individual.
INSERT INTO Payments (FirstName, LastName, PaymentMade)
VALUES ('', '', ''); AND UPDATE Payments
SET Payments.MemberID = Members.MemberID
FROM Members INNER JOIN Members ON Payments.MemberID = Members.MemberID;
Question is: Have I performed this correctly or have I missed a critical step here?
Many thanks! :)
I'm guessing you want to insert data from Table1 into Table2.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
Use a Where condition for filtering out unwanted records.
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

Copy from one table with auto increment column

I want to copy the data from one table1 into table2. table1 does not have one of the columns that exists in table2 but it needs to be unique for my purpose.
My code is this so far:
set #uidfield=1000;
insert into table2 (column1, colulmn2, column3)
select column1, column2, (#uidfield := #uidfield+1)
from table1
This will copy about 6000 records, I don't have a good way to test my SQL statement, and I want to make sure its somewhat right before I send it to my SR analyst.
Thanks!
Are you looking for this?
insert into table2 (column1, colulmn2, column3)
select column1, column2,
1000 + row_number() over (order by (select null))
from table1;
This would be the SQL Server equivalent of your syntax, if I interpret your syntax as MySQL syntax.

Extracting data from SQL Server with no duplicate of one particular column

Similar questions have been asked before and I tried all those solutions. Unfortunately none of them worked for me. Here is my requirement. There is a table with column1 (which is PK), column2, column3, column4, column5. Other than the first column (PK), all columns may have duplicate values in different rows. However, I want to pull a list of all rows with just one condition, column2 must not repeat. If there are multiple rows with duplicate values in column2, I just want any of the rows (say the first one, which can be done using min(column1)) and disregards the rows that has same values in column2.
I tried group by, didn't work because group by requires me to put all columns in group by and that results in a combination of all columns being unique.
EDIT
Thank you everybody for putting me on the right track. I tried many things and finally I think I found the right answer. Please comment if you see any issues with it.
select * from myTable where column1 in (select MIN(column1) from myTable group by column2)
To get the complete row with the minimum column1 per value of column2, you can use analytic functions to assign an order of the rows ordered by column1 partitioning by column2.
Then you can just pick the rows with row number 1 (the first row per partition) and you'll get the complete existing row with the smallest value of column1;
WITH cte AS (
SELECT column1, column2, column3, column4, column5,
ROW_NUMBER() OVER (PARTITION BY column2 ORDER BY column1) rn
FROM test
)
SELECT column1, column2, column3, column4, column5
FROM cte
WHERE rn=1;
An SQLfiddle to test with.
Just apply MIN() to all the other columns you want to retrieve, and use group by on the one column you want to be unique.
SELECT MIN(column1), column2, MIN(column3), MIN(column4), MIN(column5)
FROM your_table
GROUP BY column2
Update
As mentioned in the comments, the above solution does not return a complete row, but the minimum values across all columns in the group. To retrieve a complete row, based on the lowest value in column1:
SELECT column1, column2, column3, column4, column5
FROM your_table t1
WHERE t1.id = (
SELECT TOP 1 t2.id
FROM your_table t2
WHERE t2.column2 = t1.column2
ORDER BY t2.column1
)

using two different order by statements in one query

Consider a column1 in a table has values 'A' or 'B' or null. if the Column1 has value 'A', a select query needs to be executed ordered by column2, else the select query needs to be executed ordered by column3.
Can you please help me to acheive this requirement via single query.
Just a guess, have to be checked!
select *, decode (column1, 'A', column2, column3) as field_for_order
from your_table
order by field_for_order

SQL Query to retrieve results from two equally designed tables

How can I query the results of two equally designed tables?
if table1 contains 1 column with data:
abc
def
hjj
and table2 contains 1 column with data:
uyy
iuu
pol
then i want my query to return
abc
def
hjj
uyy
iuu
pol
but I want to make sure that if I try to do the same task with multiple columns that the associations remain.
SELECT
Column1, Column2, Column3 FROM Table1
UNION
SELECT
Column1, Column2, Column5 AS Column3 FROM Table2
ORDER BY
Column1
Notice how I do an order by at the end and that Column5 in Table2 is the equivalent of Column3 in Table1. The Order By is of course optional, but allows you to control the order of items from both tables once they are combined.
Use a UNION
SELECT *
FROM TABLE_A
UNION
SELECT *
FROM TABLE_B
UNION will give you all distinct results, as where UNION ALL will give you results combined from the sets.
SELECT col FROM t1 UNION SELECT col FROM t2
Union reference.
sev, since union is the solution to what you described and you say that didn't work, perhaps you can provide the code you wrote that didn't work as clearly we are missing part of the picture. Are you positive the second table has the records you want? How do you know for sure?