I have a table with the following
Table1
col1 col2
------------
1 A
2 B
3 C
4 D
Table2
col1 col2
------------
5 E
6 F
7 G
8 H
And I want the result like this
col1 col2
------------
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
How to do it in MS SQL Server?
Please help me..
I search the Join, Inner Join and other but they are just making 4 colums.
How to do in the way that i want..
Please help me
You need to UNION the two tables together. Try this:
SELECT col1, col2 FROM Table1
UNION
SELECT col1, col2 FROM Table2
If you want to include any rows that are duplicate, use UNION ALL instead of UNION.
Try This:
Select * From Table1
Union ALL
Select * From Table2
Related
I have two tables.
Table1 has following data
Id1 Name Comments
--------------------
1 abc hgdhg
2 xyz mnoph
3 ysdfr jkljk
4 asdf iiuoo
5 pqrs liuoo
Table2 has following data
Id2 Id1 count date
-------------------------------
1 1 18 11/16/2005
2 1 1 11/15/2005
3 1 4 11/25/2005
4 2 4 11/22/2005
5 3 8 11/05/2005
6 3 3 11/30/2005
7 4 2 11/29/2005
8 3 0 11/04/2005
9 2 5 11/02/2005
10 3 9 11/22/2005
11 2 15 11/10/2005
12 5 12 11/19/2005
I want to return output as name, comments, sum of all count since 11/10/2005
I am trying the following query(with out date where condition)
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
group by
ID1
But it is throwing error
Name is invalid in the select list because it is not contained in either an aggregate function or the Group by clause.
Can anyone help me with query (with the date where condition)? What's wrong with this?
Thanks in advance
You have to add any columns not contained in the aggregate function, and use where to filter the results:
select Name,
Comments,
sum(count)
from Table1 T1 join Table2 T2 on T1.Id1 = T2.Id1
where T2.[date] >= '11/10/2005'
group by Name, Comments
you can use below query
SELECT T1.Name ,
T1.Comments ,
SUM(T2.[count]) AS [count]
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id1 = T2.Id1
WHERE CAST(T2.[date] AS DATE) >= CAST('11/10/2005' AS DATE)
GROUP BY T1.Name ,
T1.Comments
Every column in a select statement without an aggregate function needs to be in the group by sentence too to prevent aggregate errors, about limiting the date, use where clause to define the condition, like shows ahead.
select
Name, Comments, sum(count)
from
Table1 T1
join
Table2 T2 on T1.Id1 = T2.Id1
where
date >= '2005-11-10 00:00:00'
group by
Name, Comments
i have column and rows in my table as below
col0 col1 col2 col3 col4
----------------------------
1 A 1 100 AA
2 B 2 200 BB
3 B 1 100 AA
4 A 2 200 BB
i want the final result is
col0 col1 col2 col3 col4
----------------------------
1 A 1 100 AA
2 B 2 200 BB
OR
col0 col1 col2 col3 col4
----------------------------
3 B 1 100 AA
4 A 2 200 BB
i want to delete first and second rows OR third and fourth rows but based on col1, as you can see, their's not same with each other rows except with the col0, because the col0 is primary key. how should i do with sql server express 2012?
Here is an option for deleting the first pair of duplicate records. You can assign a row number based on a partition of the four data columns. Do this in a CTE, and then delete all records from that CTE where the row number is 1.
WITH cte AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY col2, col3, col4 ORDER BY col0) rn
FROM yourTable
)
DELETE
FROM cte
WHERE rn = 1
Follow the link below for a demo showing that the logic of my CTE is correct.
Demo
You can use this
DELETE FROM yourTable
WHERE col0 NOT IN ( SELECT MIN(col0) FROM yourTable GROUP BY col1)
table 1 table2
k2 id k1 id
3 1 3 1
3 1 3 1
3 1 3 1
5 2 5 2
6 2 6 2
7 2 7 2
7 2 7 2
7 2 7 2
5 2
output i need is:
id sum k1 sum k1
1 9 9
2 37 32
Here is the query i used:
select table1.id,sum(table1.k1),table2.id,sum(table2.k1) from table1,table2
where table1.id= table2.id
but output i have not right
A simple combination of UNION and GROUP BY will give you desired result. Here you go..
SELECT id,max(SumK1) AS SumK1,max(SumK2) AS SumK2 FROM
(
SELECT ID, sum(k1) AS SumK1, NULL AS SumK2 FROM table1 GROUP BY id
UNION
SELECT ID, NULL AS SumK1, sum(k2) AS SumK2 FROM table2 GROUP BY id
) T
GROUP BY id
This should do the trick:
SELECT
COALESCE(table1_result.id1, table2_result.id2) AS id
, table1_result.sum_k1 AS sum_k1
, table2_result.sum_k2 AS sum_k2
FROM
( SELECT id AS id1, SUM(k1) AS sum_k1 FROM table1 GROUP BY id ) AS table1_result
FULL OUTER JOIN ( SELECT id AS id2, SUM(k2) AS sum_k2 FROM table2 GROUP BY id ) AS table2_result
ON table1_result.id1 = table2_result.id2
The first sub-query in the FROM clause:
SELECT id AS id1, SUM(k1) AS sum_k1 FROM table1 GROUP BY id
will give you a result of
id1 sum_k1
1 9
2 37
And likewise, the second sub-query will give the sum for table 2.
id2 sum_k2
1 9
2 32
The outer query matches the id values from both sub-queries and displays the respective sums from table 1 and table 2 alongside one another.
id sum_k1 sum_k2
1 9 9
2 37 32
I believe table1 has id and k1 column. So, you can do this:
select coalesce(t1.id, t2.id) id,
sum(t1.k1) sum_t1_k1,
sum(t2.k1) sum_t2_k1
from table1 t1 full outer join table2 t2
t1.id = t2.id
group by coalesce(t1.id, t2.id);
I want to write a sql query to get column of the next row to be of column of a row.the test example is as follow:
table:
ID startno
1 1
2 5
3 9
I want to get sql query to get result as follow:
ID startno endno
1 1 5
2 5 9
3 9 null
You can do it this way:
WITH CTE AS
(SELECT *,ROW_NUMBER()OVER(ORDER BY ID) AS RN
FROM TableName)
SELECT T1.ID,T1.startno,T2.startno as endno
FROM CTE T1 LEFT JOIN
CTE T2 ON T1.RN=(T2.RN-1)
You can use ON T1.ID=(T2.ID-1) as well. But if the ID field is not continuous or missing any ID, Join won't work as we exptected. That is why I have used ROW_NUMBER to get a continuous series of numbers to join the tables with.
Result:
ID startno endno
1 1 5
2 5 9
3 9 (null)
Sample result in SQL Fiddle
This is another way of doing it:
SELECT id, StartNo,
(SELECT TOP(1) StartNo
FROM Table1 t2 WHERE t2.id > t1.id ORDER BY t2.id) EndNo
FROM Table1 t1
I have three tables, tableA (id, A), tableB (id,B) and tableC (id,C). id is unique and primary key. Now I want to run a query on these three tables to find out sum of values of A,B and C for every id. (i.e. if id 1 is present in tableA but not in tableB then value B should be considered as 0 for id 1).example: tableA:
id A
1 5
2 6
3 2
5 7
tableB:
id B
2 5
3 8
4 1
tableC:
id C
5 2
the output should be:
id Sum
1 (5 + 0 + 0 =)5
2 (6 + 5 + 0 =)11
3 (2 + 8 + 0 =)10
4 (0 + 1 + 0 =)1
5 (7 + 0 + 2 =)9
First get a distinct list ( UNION ) of the IDs so that you include all, then LEFT JOIN to add the values together.
Something like
SELECT IDs.ID,
IFNULL(tableA.A,0) + IFNULL(tableB.B,0) + IFNULL(tableC.C,0) SumVal
FROM (
SELECT ID
FROM tableA
UNION
SELECT ID
FROM tableB
UNION
SELECT ID
FROM tableC
) IDs LEFT JOIN
tableA ON IDs.ID = tableA.ID LEFT JOIN
tableB ON IDs.ID = tableB.ID LEFT JOIN
tableC ON IDs.ID = tableC.ID
Something like this should work:
select id, sum(val) from
( select id, "A" as val from "tableA"
union all
select id, "B" as val from "tableB"
union all
select id, "C" as val from "tableC" ) as joined
group by id
order by id
I could not test it with MySql but this works my databases (HSQLDB, Oracle):
select ID, sum(X) from
(SELECT ID, A as X FROM tableA
UNION
SELECT ID, B as X FROM tableB
UNION
SELECT ID, C as X FROM tableC)
group by ID
Not sure about the exact MySQL syntax, but this works in SQL Server:
SELECT ID, SUM(ColToSum) As SumValue FROM
(
SELECT ID, A As ColToSum FROM TableA
UNION ALL
SELECT ID, B As ColToSum FROM TableB
UNION ALL
SELECT ID, C As ColToSum FROM TableC
) Combined
GROUP BY ID
Remember to use "UNION ALL", not just "UNION" which strips out duplicate rows as it combines (see http://dev.mysql.com/doc/refman/5.0/en/union.html)