group by clause query - sql-server

i have a table with values like this,
i want to group by cusotmer name where as sum of the local amount should exceed 50000 else i need to delete those records which do not satisfy?
how to achieve it in sql server 2005?
TRN 259 3 9/9/2010 6622 68667(Rs) ABHIJIT KATARE
TRN 260 3 9/9/2010 6622 14635(Rs) ABHIJIT KATARE
TRN 235 3 9/9/2010 6586 68128 AJAY PARASRAMPURIA
TRN 236 3 9/9/2010 6586 14490 AJAY PARASRAMPURIA
TRN 257 3 9/9/2010 6621 68667 ANAND DESAI
TRN 258 3 9/9/2010 6621 14635 ANAND DESAI
TRN 287 3 9/9/2010 6817 119095 ANAND KATAKAM
TRN 242 3 9/9/2010 6594 95689 ANILKUMAR MUTHUNPARA
TRN 211 3 9/9/2010 6507 52239 ARBIND KUMAR GUPTA
TRN 212 3 9/9/2010 6538 63183 ASHOK KELKAR
TRN 185 3 9/9/2010 6431 140610 BANSAL Y.K
TRN 186 3 9/9/2010 6431 46845 BANSAL Y.K
TRN 248 3 9/9/2010 6600 72565 BENNO HANS LUKE

Try this for the select:
SELECT * FROM Table
GROUP BY CustomerName
HAVING SUM(LocalAmount) > 50000
Try this for delete (modified version of #RedFilter's):
delete from MyTable
where CustomerName in (
select CustomerName
from Table
group by CustomerName
having sum(LocalAmount) <= 50000
)

select * from MyTable group by CustomerName having sum(LocalAmount) > 50000

to see the good rows use:
SELECT
cusotmerName ,SUM(localAmount)
FROM YourTable
GROUP BY cusotmerName
HAVING SUM(localAmount)>50000
to delete the bad rows use:
DELETE YourTable
WHERE
CustomerName IN (SELECT
cusotmerName
FROM YourTable
GROUP BY cusotmerName
HAVING SUM(localAmount)<50000
)

This will show you the customers that have less than 50000:
select CustomerName, sum(LocalAmount)
from MyTable
group by CustomerName
having sum(LocalAmount) <= 50000
Note that you are better off grouping by CustomerID if there is one, as there could be duplicate names. Then, you can delete by doing this:
delete from MyTable
where CustomerID in (
select CustomerID
from MyTable
group by CustomerID
having sum(LocalAmount) <= 50000
)

this syntax looks a bit weird but it works!
;with cte as
(
select sum(LocalAmount) over (partition by cname) s
from yourtable
)
delete from cte where s <= 50000

Related

SQL server select statement to select the ids of a duplicated entries of another column

Consider the table 'Table1' as below
main_id main_item_id
-------- ---------
1 101
1 102
2 105
2 105
3 105
3 106
4 101
4 101
4 102
I need to fetch main_id 2 and 4 as it has duplicate main_item_id among 1 million other records
Thanks in advance.
This will select all unique main_id's which have 2 or more identical main_item_id's:
SELECT DISTINCT T.main_id
FROM YourTable T
GROUP BY T.main_id
, T.Main_item_id
HAVING COUNT(1) > 1
Use group by clause to check the duplication
SELECT main_id, main_item_id
FROM table
GROUP BY main_id, main_item_id
HAVING count(*) > 1

SQL Server: Join with ambiguous name columns

I have two tables in SQL Server (I removed many rows, but it should not affect the result):
[dbo].[Order]:
CaseId Gender
-----------------
698 Female
694 Male
676 Female
659 Male
[dbo].[Plimplan]:
OrderID CaseID InfoID
--------------------------------------------
33425999 698 BBBBBE
33425984 694 AAAAAS
33425984 694 DSSSAS
33425609 694 BBBBBA
33425270 676 AAAAAA
33424973 676 AAAAAD
33424716 676 SASSAS
33424704 676 AAAAAA
33424500 676 BBBBBE
33424340 659 AAAAAA
33424281 659 BBBBBD
and I want to obtain the following one:
[dbo].[Plimplan]:
OrderID CaseID InfoID Gender
----------------------------------------------------
33425984 694 AAAAAS Male
33425270 676 AAAAAA Female
33424340 659 AAAAAA Male
Let's forget the join. I can remove undesired rows from [dbo].[Order] with this:
SELECT TOP (100) MAX (OrderID) AS [OrderID]
,MAX (CaseID) AS [CaseID]
,MAX (InfoID) AS [InfoID]
/*,[Gender]*/
FROM [dbo].[Plimplan] as Plimpl
/* INNER JOIN [dbo].[Order] as Ord
ON Plimpl.CaseID = Ord.CaseID */
WHERE [InfoID] NOT LIKE 'BBBBB%'
and CaseID < 700
and CaseID > 600
GROUP BY (CaseID)
order by CaseID desc
But if I remove the comments to complete the join, I get this error:
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'CaseID'.
Use aliases. The error you are getting is due to both tables in the join having a CaseID column. SQL Server is telling you that it can't figure out which one you are trying to select.
SELECT TOP 100
MAX (Plimpl.OrderID) AS [OrderID]
MAX (Plimpl.CaseID) AS [CaseID]
MAX (Plimpl.InfoID) AS [InfoID]
FROM [dbo].[Plimplan] AS Plimpl
INNER JOIN [dbo].[Order] AS Ord
ON Plimpl.CaseID = Ord.CaseID
WHERE
Plimpl.[InfoID] NOT LIKE 'BBBBB%' AND
Plimpl.CaseID < 700 AND
Plimpl.CaseID > 600
GROUP BY
Plimpl.CaseID
ORDER BY
Plimpl.CaseID DESC;
You had at one point apparently been selecting the Gender column from the Order table, but it makes no sense to do this with your use of GROUP BY.
You're getting the error when you uncomment the JOIN statement right?
Use Aliases
SELECT TOP (100) MAX (OrderID) AS [OrderID]
,MAX (CaseID) AS [CaseID]
,MAX (InfoID) AS [InfoID]
/*,[Gender]*/
FROM [dbo].[Plimplan] as Plimpl
INNER JOIN [dbo].[Order] as Ord
ON Plimpl.CaseID = Ord.CaseID
WHERE [InfoID] NOT LIKE 'BBBBB%'
and Plimpl.CaseID < 700
and Plimpl.CaseID > 600
GROUP BY Plimpl.CaseID
order by Plimpl.CaseID desc

How to create identity column in SQL Server using with statement

I am looking for some help in SQL, I am using following query
with t AS
(
select
EmpID, mgrid, HierarchyLevel, Description
from
empdatatest
)
select *
from t
order by empid
I want a way so that table T has an identity column
Data output should be like
ID EmpID mgrid HierarchyLevel Description
------------------------------------------
1 201 7 1 Partner
2 202 201 2 Senior Manager
3 221 202 3 Manager
4 343 221 4 employee
5 534 221 4 employee
6 552 221 4 employee
Use ROW_NUMBER():
;With t As
(
Select Row_Number() Over (Order By EmpId) As ID,
EmpID,
mgrid,
HierarchyLevel,
Description
From empdatatest
)
Select *
From t
Order By empid;

Find rows with duplicate entries in SQL Server

I need to find rows with duplicate order numbers in a table in SQL Server.
For example SELECT * shows:
No
-----
5001
5002
5003
5003
5003
5004
5005
5006
5006
I want to get
No
------
5003
5003
5003
5006
5006
Is it possible to write a query to do that?
Although this question was answered thousand times on SO, use GROUP BY + HAVING:
SELECT No
FROM dbo.tablename
Group By No
Having Count(*) > 1
SELECT S.No
FROM [dbo].[Shows] S
INNER JOIN (
SELECT [No]
FROM [dbo].[Shows]
GROUP BY [No]
HAVING COUNT(*) > 1
) J
ON S.No = J.No
You can use COUNT() OVER() to get count of rows per No and, in an outer query, filter out all non-duplicate rows:
SELECT [No]
FROM (
SELECT [No], COUNT(*) OVER (PARTITION BY [No]) AS cnt
FROM mytable ) t
WHERE cnt > 1
Demo here

SQL Server 2008 how to select top [column value] and random record?

I'm using SQL Server 2008, I want select random row record, and the total number of record is depend on another table's column value, how to do this?
My SQL statement is something like this, but wrong..
select top b.number a.name, a.link_id
from A a
left join B b on b.link_id = a.link_id
order by newid()
Here are my tables and the expected result.
Table A:
name link_id
james 100
albert 100
susan 100
simon 101
tom 101
fion 101
Table B:
link_id number
100 2
101 1
Expected result:
when run 1st time, result may be:
name link_id
james 100
susan 100
fion 101
2nd time result may be:
albert 100
susan 100
simon 101
3rd time could be:
james 100
albert 100
fion 101
Explaination
Refer to table B, link_id: 100, number: 2
meaning that Table A should select out 2 random record for link_id = 100
and need to select 1 random record for link_id=101
You can use the ROW_NUMBER() function:
SELECT A.name, A.link_id
FROM(
SELECT name,link_id, ROW_NUMBER()OVER(PARTITION BY link_id ORDER BY NEWID()) rn
FROM dbo.tblA
) AS A
JOIN dbo.tblB AS B
ON A.link_id = B.link_id
WHERE A.rn <= B.number;
Here is a SqlFiddle to show this in action: http://sqlfiddle.com/#!3/92eac/2
Try this:
SELECT a.*
FROM b
CROSS APPLY
(
SELECT TOP (b.number) a.*
FROM a
WHERE a.link_id = b.link_id
ORDER BY
NEWID()
) a
Also see: SQLFiddle

Resources