I have a table made like this:
Col1 Col2 Col3
____________________________
A AA AAA
A AA AAA
A AA BBB
B BB BBB
B AA CCC
What do i need is to count how many rows have the same combination of Col1 and Col2, like this:
Count Col1 Col2
_________________________
3 A AA
1 B BB
1 B AA
Just group by the columns and count the results:
SELECT COUNT(*) AS [COUNT], COL1, COL2
FROM YOUR_TABLE
GROUP BY COL1, COL2
Try this:
SELECT COUNT(*) as Count, COL1, COL2
FROM TableName
GROUP BY COL1, COL2
Result:
COUNT COL1 COL2
3 A AA
1 B AA
1 B BB
See result in SQL Fiddle.
COUNT returns the number of items in a group. Read more here.
Related
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)
I just have one question in SQL server.
I have table table1 containing the below rows.
col1 col2
----------
A X
A X
A Y
A Y
A Z
B X
B Z
B Z
C V
I want to add a new column col3 to the table as incremental of grouping of col1 and col2 , as shown below :
col1 col2 col3
-----------------
A X 1
A X 1
A Y 2
A Y 2
A Z 3
B X 1
B Z 2
B Z 2
C V 1
You can do this with a DENSE_RANK():
Select col1, col2,
Dense_Rank() Over (Partition By col1 Order By col2) As Col3
From Table1
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
I have a following table
Col1 Col2 Col3
A1 1 null
A1 2 null
B1 5 null
B2 6 null
M1 1 M
M2 2 M
M3 3 M
J1 1 J
J2 2 J
I want to sum Col2 based on Col1. The query will be like following,
select Col1, sum (Col2)
group by Col1
However, if Col3 has the same letter, I want sum up Col2 for all Col1. So the result table should be like
Col1 Col2
A1 3
B1 5
B2 6
M1 6
M2 6
M3 6
J1 3
J2 3
How do I change my query to get above table?
Edit after comment / update to question. I didn't know a clever way, seems like some others have one though.
select * from (
select Col1, SUM(Col2) Col2
from Table
where Col3 is null
group by Col1
union
select mainset.Col1, tmp.Col2
from Table mainset
join
(
select Col3, SUM(Col2) Col2
from Table
where Col3 is not null
group by Col3
) tmp on tmp.Col3 = mainset.Col3
where mainset.Col3 is not null
) fullset
order by fullset.Col1
You could do something like this (I named the table #a):
;WITH col3Sum AS
(
SELECT Col3, SUM(Col2) SUM3
FROM #a
WHERE col3 IS NOT NULL
GROUP BY col3
),
col1Sum AS
(
SELECT Col1, SUM(Col2) sum1
FROM #a
GROUP BY Col1
)
SELECT c1.Col1, ISNULL(c3.SUM3, c1.sum1) AS Col2
FROM col1Sum c1
LEFT JOIN col3Sum c3
ON c1.Col1 LIKE c3.Col3+'%'
ORDER BY c1.Col1
Imagine the below table as A
col1 col2 col3 rank
1 2 n 5
1 2 n 6
2 3 a 3
While inserting below records from table B to table A, the rank column valus should be keep incrementing if the same records inserts.
Records in table B which gonna insert into A are
col1 col2 col3
1 2 n
2 3 a
The desired output in table A after the above records are inserted is,
col1 col2 col3 rank
1 2 n 5
1 2 n 6
1 2 n 7
2 3 a 3
2 3 a 4
Please help me how to acheive this.Thanks.
If the record in B are unique, then you could use a query like this
--------------EDIT-------------------
If B can have multiple records, you can use row_number() funcion with partition
insert into TestA
select b.*,
(select max([rank]) from TestA where col1 = b.col1 and col2 = b.col2 and col3 = b.col3)
+ row_number()over (partition by col1, col2, col3 order by col1, col2,col3 asc) as N
from TestB b
--------------END EDIT-------------------
Note: i renamed the table: TestA and TestB
insert into TestA
select b.*, (select max([rank])+1 from TestAwhere col1 = b.col1 and col2 = b.col2 and col3 = b.col3)
from TestB b
or with a JOIN like this
insert into testa
select b.*, mr+1 from TestB b
join
(select col1, col2, col3, max([rank]) as mr
from TestA A
group by col1, col2, col3) as M
on
b.col1 = M.col1 and b.col2 = M.col2 and b.col3 =M.col3
I would use an INSTEAD OF INSERT trigger for this. Like this:
CREATE TRIGGER rankInsertTrigger
ON A
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO A(col1, col2, col3, rank)
SELECT i.col1, i.col2, i.col3,
MAX(SELECT a.rank
FROM A AS a
WHERE a.col1 = i.col1
AND a.col2 = i.col2
AND a.col3 = i.col3) + 1
FROM inserted i
END
Every time you now insert values into table A, this trigger runs and replaces the original insert with an insert that sets the rank you want.
For example, when you do INSERT INTO A(col1, col2, col3) VALUES (1, 2, n), what actually runs is the insert statement in the trigger (which takes the original values for col1, col2, col3 but overwrites rank).