Count 'Number of Transactions' - tsql

I'm trying to get a count of 'number of transactions' that occurred'.
The data could look like this.
Cust # Trans# TransType LineItem
42 5000 1 1
42 6000 1 1
42 6000 1 2
42 6000 2 1
42 6000 2 2
42 6000 2 3
There can be multiple transaction types for any given transaction number. In this example, my desired returned 'number of transactions' count is '3', as Trans# 5000 only had one different TransType and 6000 had two. If I do a distinct count of Trans# I get '2' and if I do just a count, I get '6'.
I've tried working with:
COUNT(DISTINCT CASE Trans# WHEN ???? THEN 1 ELSE null END) AS [Num of Transactions],
But I know that I'm not quite on the right track. If anyone could point me in the right direction, it'd be much appreciated.

Try this :-
with cte as
(
Select Cust,Trans,row_number() over (partition by trans,TransType order by cust) rn
from Sample
)
Select count(*) as TransCount from cte
where rn=1
SQL FIDDLE DEMO

You can use the following to get the count of distinct transtype for each customer and transaction:
select cust,
trans,
count(distinct transtype) cnt
from yourtable
group by cust, trans;
Then if you want a total of that count, you can apply sum() to the query:
select sum(cnt) Total
from
(
select cust,
trans,
count(distinct transtype) cnt
from yourtable
group by cust, trans
) src
See SQL Fiddle with Demo of both queries.

Related

I want to select 2 data from database which durations less than 150

I have a problem with my SQL command. I want to select 2 movies which 2 movies sum of durations less than 150 I wrote this SQL command:
Select
movie_title,Sum(movie_time) as sum_movie
From
movie_movie
Group By
movie_title
Having
Sum(movie_time)<100
Order By
sum_movie DESC
You can get two movies with minimum movie_time values ​​with order by movie_time ASC limit 2 in CTE, and then use that in the condition.
with two_min_movie as (
select *
from movie_movie
order by movie_time ASC limit 2
)
select *
from two_min_movie
where (select sum(movie_time) from two_min_movie) < 150
Demo in DBfiddle

How to normalize group by count results?

How can the results of a "group by" count be normalized by the count's sum?
For example, given:
User Rating (1-5)
----------------------
1 3
1 4
1 2
3 5
4 3
3 2
2 3
The result will be:
User Count Percentage
---------------------------
1 3 .42 (=3/7)
2 1 .14 (=1/7)
3 2 .28 (...)
4 1 .14
So for each user the number of ratings they provided is given as the percentage of the total ratings provided by everyone.
SELECT DISTINCT ON (user) user, count(*) OVER (PARTITION BY user) AS cnt,
count(*) OVER (PARTITION BY user) / count(*) OVER () AS percentage;
The count(*) OVER (PARTITION BY user) is a so-called window function. Window functions let you perform some operation over a "window" created by some "partition" which is here made over the user id. In plain and simple English: the partitioned count(*) is calculated for each distinct user value, so in effect it counts the number of rows for each user value.
Without using a windowing function or variables, you will need to cross join a grouped subquery on a second "maxed" subquery then select again to return a subset you can work with.
SELECT
B.UserID,
B.UserCount,
A.CountAll
FROM
(
SELECT
CountAll=SUM(UserCount)
FROM
(
SELECT
UserCount=COUNT(*)
FROM
MyTable
GROUP BY
UserID
) AS A
)AS C
CROSS JOIN(
SELECT
UserID,
UserCount=COUNT(*)
FROM
MyTable
GROUP BY
UserID
)AS B

Postgresql running sum of previous groups?

Given the following data:
sequence | amount
1 100000
1 20000
2 10000
2 10000
I'd like to write a sql query that gives me the sum of the current sequence, plus the sum of the previous sequence. Like so:
sequence | current | previous
1 120000 0
2 20000 120000
I know the solution likely involves windowing functions but I'm not too sure how to implement it without subqueries.
SQL Fiddle
select
seq,
amount,
lag(amount::int, 1, 0) over(order by seq) as previous
from (
select seq, sum(amount) as amount
from sa
group by seq
) s
order by seq
If your sequence is "sequencial" without holes you can simply do:
SELECT t1.sequence,
SUM(t1.amount),
(SELECT SUM(t2.amount) from mytable t2 WHERE t2.sequence = t1.sequence - 1)
FROM mytable t1
GROUP BY t1.sequence
ORDER BY t1.sequence
Otherwise, instead of t2.sequence = t1.sequence - 1 you could do:
SELECT t1.sequence,
SUM(t1.amount),
(SELECT SUM(t2.amount)
from mytable t2
WHERE t2.sequence = (SELECT MAX(t3.sequence)
FROM mytable t3
WHERE t3.sequence < t1.sequence))
FROM mytable t1
GROUP BY t1.sequence
ORDER BY t1.sequence;
You can see both approaches in this fiddle

Summing Multiple Records by maxdate

I have a table with the following data
Bldg Suit SQFT Date
1 1 1,000 9/24/2012
1 1 1,500 12/31/2011
1 2 800 8/31/2012
1 2 500 10/1/2005
I want to write a query that will sum the max date for each suit record, so the desired result would be 1,800, and must be in one cell/row. This will ultimately be part of subquery, I am just not getting what I expect with the queries I have writtren so far.
Thanks in advance.
You can use the following (See SQL Fiddle with Demo):
select sum(t1.sqft) Total
from yourtable t1
inner join
(
select max(dt) mxdt, suit, bldg
from yourtable
group by suit, bldg
) t2
on t1.dt = t2.mxdt
and t1.bldg = t2.bldg
and t1.suit = t2.suit
; With Data As
(
Select Bldg, Suit, SQFT, Row_Number() Over (Partition By Bldg, Suit Order By Date DESC) As RowID
From YourTableNameHere
)
Select Bldg, Sum(SQFT) As TotalSQFT
From Data
Where RowId = 1
Group By Bldg

Perform arithmetic in select statement

Let's suppose I have balance 2000, and want to select balance as
balance=balance-Cr+Dr
So my balance column will give values as below.
balance DR Cr
40000 0 60000
100000 60000 0
0 0 100000
How is this possible in SQL query?
Please check similar question like me
enter link description here
Here is a recursive CTE that calculates the balance using the balance from the previous row. You need something that defines the order of the rows. I use the ID column in the sample table.
-- Test table
declare #T table
(
ID int identity primary key,
DR int,
Cr int
)
-- Sample data
insert into #T (DR, Cr)
select 0, 60000 union all
select 60000, 0 union all
select 0, 100000
-- In value
declare #StartBalance int
set #StartBalance = 100000
-- Recursive cte calculating balance as a running sum
;with cte as
(
select
T.ID,
#StartBalance - T.Cr + T.DR as Balance,
T.DR,
T.Cr
from #T as T
where T.ID = 1
union all
select
T.ID,
C.Balance - T.Cr + T.DR as Balance,
T.DR,
T.Cr
from cte as C
inner join #T as T
on C.ID+1 = T.ID
)
select Balance, DR, Cr
from cte
option (maxrecursion 0)
Result:
Balance DR Cr
----------- ----------- -----------
40000 0 60000
100000 60000 0
0 0 100000
This should work:
SELECT (T.BALANCE-T.CR+T.DR) as "Balance", T.DR, T.CR
FROM <table-name> T
If you use Oracle, there is a function called LAG to reach the previous row data: http://www.adp-gmbh.ch/ora/sql/analytical/lag.html
If you read this link I think you will see that this is exactly what you need. But only if you use Oracle..