simple SUM in T-sql - tsql

This should be really simple. I am using SSMS 2008, trying to get a sum of just one column. Problem is that I currently group on this one column and also use a HAVING statement. How do I get sum of total # of records > 1? This is my T-SQL logic currently:
select count(*) as consumer_count from #consumer_initiations
group by consumer
having count(1) > 1
But this data looks like:
consumer_count
----------------
2
2
4
3
...

Wrap it?
SELECT SUM(consumer_count)
FROM (
select count(*) as consumer_count from #consumer_initiations
group by consumer
having count(1) > 1
) AS whatever

With a nested query:
select sum(consumer_count)
FROM (
select count(*) as consumer_count from #consumer_initiations
group by consumer
having count(1) > 1
) as child

select sum(t.consumer_count)
from (select count(*) as consumer_count
from #consumer_initiations
group by consumer
having count(1) > 1) t

Try:
select sum(t.consumer_count) from
(
select count(*) as consumer_count from #consumer_initiations
group by consumer
having count(1) > 1
) t
This will give you the sum of records that your original query returns. These type of queries are called nested queries.

Besides the wrapping in another query, you could use this:
SELECT COUNT(*) AS consumer_count
FROM #consumer_initiations AS a
WHERE EXISTS
( SELECT *
FROM #consumer_initiations AS b
WHERE b.consumer = a.consumer
AND b.PK <> a.PK -- the Primary Key of the table
)

Related

Selecting row(s) that have distinct count (one) of certain column

I have following dataset:
org system_id punch_start_tb1 punch_start_tb2
CG 100242 2022-08-16T00:08:00Z 2022-08-16T03:08:00Z
LA 250595 2022-08-16T00:00:00Z 2022-08-16T03:00:00Z
LB 300133 2022-08-15T04:00:00Z 2022-08-16T04:00:00Z
LB 300133 2022-08-16T04:00:00Z 2022-08-15T04:00:00Z
MO 400037 2022-08-15T14:00:00Z 2022-08-15T23:00:00Z
MO 400037 2022-08-15T23:00:00Z 2022-08-15T14:00:00Z
I am trying to filter out data so that it only populates the outcome when Count of "system_id" = 1.
So, the expected outcome would be only following two rows:
org system_id punch_start_tb1 punch_start_tb2
CG 100242 2022-08-16T00:08:00Z 2022-08-16T03:08:00Z
LA 250595 2022-08-16T00:00:00Z 2022-08-16T03:00:00Z
I tried with Group by and Having clause, but I did not have a success.
You can try below
SELECT * FROM
(
SELECT org,system_id,punch_start_tbl,punch_start_tb2
,ROW_NUMBER()OVER(PARTITION BY system_id ORDER BY system_id)RN
FROM <TableName>
)X
WHERE RN = 1
CTE returns org with only one record then join with main table on org column.
;WITH CTE AS (
select org
from <table_name>
group by org
Having count(1) = 1
)
select t.*
from cte
inner join <table_name> t on cte.org = t.org
You can try this (use min because we have only one row):
select MIN(org), system_id, MIN(punch_start_tb1), MIN(punch_start_tb2)
from <table_name>
group by system_id
Having count(1) = 1
or use answer #Meyssam Toluie with group by by system_id

Appear in the Group By Clause or use in an aggregate function

Calculate the first actual bought item and populate the first_actual_item column in tr2_invoice.
SELECT cust_id, total_amount, items, MIN(time_in)
FROM tr_invoice WHERE total_amount <> 0
GROUP BY cust_id;
ERROR: column "tr_invoice.total_amount" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT cust_id, total_amount, items, MIN...
I used AVG(), MIN(), MAX(), or ARRAY_AGG() as aggregations for total_amount and items, it would output differently from what I queried on MySQL. Any better solution to solve this?
the selected fields must appear in the GROUP BY clause.
1.
SELECT cust_id, total_amount, items, MIN(time_in) over( PARTITION by cust_id) as min_time_in
FROM tr_invoice WHERE total_amount <> 0 ;
2.
SELECT
b.cust_id ,
b.total_amount,
b.items ,
a.min_time_in
from
(
SELECT
cust_id,
MIN(time_in) as min_time_in
FROM
tr_invoice
WHERE
total_amount <> 0
GROUP BY
cust_id
)a
join
tr_invoice b
ON
a.cust_id=b.cust_id;
Please refer link

Exclude Duplicate or Exisisting Values in PostgreSQL

I have report like this:
I tried to remove the duplicate values in documentno as paymentno column using :
DELETE FROM c_payment pa USING (
SELECT MIN(ctid) as ctid, pa.documentno
FROM c_payment pa
GROUP BY pa.documentno
HAVING COUNT(*) > 1
) b
WHERE pa.documentno = b.documentno
AND pa.ctid <> b.ctid) documentno
but it's still not working, any other advice on what method should I use?
You should remove the last closing brace together with the word documentno.
By using sequence number logic will work for my case
ROW_NUMBER() OVER( PARTITION BY pym.c_payment_id ORDER BY pym.c_bpartner_id)
as seq_no, coalesce (ci.documentno, '-') as inv_no ,
instead of DELETE with or without subquery
DELETE FROM c_payment pa USING (
SELECT MIN(ctid) as ctid, pa.documentno
FROM c_payment pa
GROUP BY pa.documentno
HAVING COUNT(*) > 1
) b
WHERE pa.documentno = b.documentno
AND pa.ctid <> b.ctid) documentno

How to Select the Maximum value occurring 2 times?

Suppose I have a table of values looking like this:
Sample_Number |
-------------------
1 |
1 |
2 |
3 |
3 |
4 |
5 |
How can I write a SELECT statement to return the maximum sample number that occurs exactly 2 times? In the sample data the value I am looking for would be 3.
I imagine there could be a number of answers to this - I am especially interested in a solution with no inner selects and that uses the Having clause (if this is possible).
You can use this query:
SELECT TOP 1 Sample_Number As MaxSampleNumberThatOccursTwice
FROM dbo.TableName
GROUP BY Sample_Number
HAVING COUNT(*) = 2
ORDER BY Sample_Number DESC
I'm sure there's an easier way to do this, but you can do it by pulling all of the Sample_Numbers with exactly two entries, and pulling the MAX() of those values:
;With Cte As
(
Select Sample_Number
From Test
Group By Sample_Number
Having Count(Sample_Number) = 2
)
Select Max(Sample_Number)
From Cte
;with cte
as
(select sample_number
from #temp
group by Sample_Number
having
count(Analysis_ID)=2
)
select max(sample_number) from cte
I would use subselect:
SELECT MAX (sample_number)
FROM (SELECT sample_number
FROM TAB1
GROUP BY sample_number
HAVING COUNT(sample_number) =2
)
Group by the Sample_Number and get the count of the group
and only select if the count is 2
select Sample_Number, count(*) count from someTable
group by Sample_Number
having count=2

T-SQL how to count the number of duplicate rows then print the outcome?

I have a table ProductNumberDuplicates_backups, which has two columns named ProductID and ProductNumber. There are some duplicate ProductNumbers. How can I count the distinct number of products, then print out the outcome like "() products was backup." ? Because this is inside a stored procedure, I have to use a variable #numrecord as the distinct number of rows. I put my codes like this:
set #numrecord= select distinct ProductNumber
from ProductNumberDuplicates_backups where COUNT(*) > 1
group by ProductID
having Count(ProductNumber)>1
Print cast(#numrecord as varchar)+' product(s) were backed up.'
obviously the error was after the = sign as the select can not follow it. I've search for similar cases but they are just select statements. Please help. Many thanks!
Try
select #numrecord= count(distinct ProductNumber)
from ProductNumberDuplicates_backups
Print cast(#numrecord as varchar)+' product(s) were backed up.'
begin tran
create table ProductNumberDuplicates_backups (
ProductNumber int
)
insert ProductNumberDuplicates_backups(ProductNumber)
select 1
union all
select 2
union all
select 1
union all
select 3
union all
select 2
select * from ProductNumberDuplicates_backups
declare #numRecord int
select #numRecord = count(ProductNumber) from
(select ProductNumber, ROW_NUMBER()
over (partition by ProductNumber order by ProductNumber) RowNumber
from ProductNumberDuplicates_backups) p
where p.RowNumber > 1
print cast(#numRecord as varchar) + ' product(s) were backed up.'
rollback