Sqlserver group by pending days period - sql-server

I have table structure as below,
State Application_count Pending_Days
_________________________________________
TN 10 0
TN 20 1
TN 60 2
TN 10 3
MH 40 1
MH 50 3
MH 20 5
MH 30 8
I want to sum Application_count based on State and Pending_Days period.
I have to group Pending_days 0 to 1 Days, 1 to 3 days, morethan 3 days
Expected Output:
State Application_count
_________________________
TN 30
TN 70
MH 40
MH 50
MH 50

Give an additional column, group_num using a CASE expression based on the condition of Pending_Days column.
Then find the sum group by group_num and state columns.
Query
select t.[state], sum(t.[Application_Count]) as [Application_Count] from(
select [group_num] = (
case when [Pending_Days] between 0 and 1 then 1
when [Pending_Days] between 2 and 3 then 2
when [Pending_Days] > 3 then 3 else 4 end
), *
from [your_table_name]
)t
group by t.[group_num], t.[state];
Find demo here
Note:
For Pending_Days condition, 0 to 1 Days I have taken 0 and 1
for 1 to 3 days I have taken 2 and 3 , for morethan 3 days I have taken the value greater than 3.

Related

Repeat a sum as a column in SQL

I have a table
WO# Seg# Part# QTY TotSale FlatSale
1 1 1 5 35 159
1 1 2 2 100 159
1 2 3 3 50 50
I need to calculate the FlatSale/SUM(TotSale) for each Seq# & WO# but do not group Seg# into one row.
I need this
WO# Seg# Part# QTY TotSale FlatSale Calc
1 1 1 5 35 159 1.177
1 1 2 2 100 159 1.177
1 2 3 3 50 50 1
With my code I am able to only do the division on each individual line like this:
select *, FlatSale/TotSale as Calc from table
WO# Seg# Part# QTY TotSale FlatSale Calc
1 1 1 5 35 159 4.54
1 1 2 2 100 159 1.59
1 2 3 3 50 50 1
I wouldn't mind leaving my Calc column and adding another column if that's the easiest way to do it.
Maybe something like this:
SELECT
[WO#],[Seg#],[Part#],[QTY],[TotSale],[FlatSale]
((FlatSale*100)/(SUM([TotSale]) OVER(PARTITION BY [Seg#] ORDER BY [Seg#])))/100 AS Calc
FROM SomeTable
You can use a lateral join. Here's something quick (not tested as I don't have access to mssql at the moment):
select *, x.CalcPrice as Calc
from table t
outer apply (
select t.FlatSale/SUM(ix.TotSale) CalcPrice
from table ix where ix.[WO#] = t.[WO#]
and ix.[Seg#] = t.[Seg#]) x
Something like that.

Calculating the running total

I want to calculate the running total using a Stored Procedure. The base Table is ~10.000 rows and is as follows:
nWordNr nBitNr tmTotals
------------------------
5 14 86404
5 14 146
2 3 438
10 2 3319
5 12 225
2 3 58
.... .... .....
.... .... .....
I want this to be GROUPED BY nWordNr, NBitNr and have the total tmTotals. To do this is started of with the following:
SELECT TOP 10
[nWordNr] as W,
[nBitNr] as B,
SUM([tmTotals]) as total,
COUNT(*) as Amount
FROM Messages_History
GROUP BY nWordNr, nBitNr
ORDER BY total desc
This results in:
W B total Amount
-----------------------
2 3 3578775 745
3 3 3557975 395
5 4 2305229 72
5 3 2183050 33
5 12 2022401 825
5 14 1673295 652
48 12 1658862 302
4 3 1606454 215
48 13 1541729 192
5 9 1463256 761
Now I want to calculate the running total on the column total like this:
W B total Amount running
-------------------------------
2 3 3578775 745 3578775
3 3 3557975 395 7136750
5 4 2305229 72 9441979
5 3 2183050 33 11625029
5 12 2022401 825 etc.
5 14 1673295 652 etc.
48 12 1658862 302 etc.
4 3 1606454 215 etc.
48 13 1541729 192 etc.
5 9 1463256 761 etc.
so what I found was:
COUNT([tmTotals]) over (ORDER BY [nWordNr], [nBitNr]) as Running
But here I get the error that is discussed in this question: Column invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause and I just can't figure out how to solve it in this case
it should be SUM ( SUM ( tmTotals) ) OVER ( ... )
SELECT TOP 10
[nWordNr] as W,
[nBitNr] as B,
SUM([tmTotals]) as total,
COUNT(*) as Amount,
SUM(SUM([tmTotals])) OVER (ORDER BY [nWordNr], [nBitNr]) as Running
FROM Messages_History
GROUP BY nWordNr, nBitNr
ORDER BY total desc
EDIT :
Looking at your expected result, the Running should be
SUM(SUM([tmTotals])) OVER (ORDER BY SUM([tmTotals]) DESC) as Running
if the above is a bit difficult to grasp, then you can use a CTE or derived table and perform the running total on the outer query
; with CTE as
(
SELECT
[nWordNr] as W,
[nBitNr] as B,
SUM([tmTotals]) as total,
COUNT(*) as Amount
FROM Messages_History
GROUP BY nWordNr, nBitNr
)
SELECT TOP 10 *,
SUM(total) OVER (ORDER BY total desc) as Running
FROM CTE
ORDER BY total desc

SQL select joined 2 tables same as one column

SQL table Authority is:
AuthorNo Price PrePay(bit)
----------------------------
1 250$ 1
2 120$ 0
3 300$ 0
4 112$ 1
5 25$ 0
Table Order is:
AuthorNo OrderNo
-----------------
1 33
1 34
2 33
2 38
3 41
3 82
4 55
4 21
5 21
5 66
I want the result is:
Select from Authority.AuthorNo where AuthorNo same in Order.OrderNo and at least one of the AuthorNo.Prepay is 1
AuthorNo
--------
1
2
4
5
How to select this?
If you need to find the authors that have orders that were also ordered by authors with PrePay?
Then you could use an EXISTS for this.
SELECT auth.AuthorNo
FROM Authority auth
JOIN [Order] ord ON ord.AuthorNo = auth.AuthorNo
WHERE EXISTS
(
SELECT 1
FROM [Order] ord_pp
JOIN Authority auth_pp
ON auth_pp.AuthorNo = ord_pp.AuthorNo
AND auth_pp.Prepay = 1
WHERE ord_pp.OrderNo = ord.OrderNo
)
GROUP BY auth.AuthorNo;
A test here
Result:
AuthorNo
--------
1
2
4
5
I am guessing you just want to see the AuthorNo in the result? Try this
Select distinct a.AuthorNo
From Authority a
join Order b on a.AuthorNo=b.AuthorNo
where a.Prepay=1

Get Unmatched records from the 3 table

I have 3 tables. The first table 'Status_Mapping' has following columns
Status_original Status_Site
accepted Call Verified
duplicate Duplicate Leads
dq DQ
'Lead_transaction' has the columns:
Lead_transaction_id Rate Status
11 0.01 accepted
12 0.02 accepted
13 0.01 newstatus
'Lead_Instance' table:
Lead_Instance_id Lead_transaction_id product_id affiliate_id
1 11 6 10
2 12 7 11
3 13 6 10
What I want to do is get the count(lead_instance_id) and sum(rate) for status which are not present in status_mapping table and should display status as "other", with product_id = 6 and affiliate_id = 10 My End result should be like
Total Sum Status
1 0.01 Other
you can start with this query:
select count(distinct a.Lead_Instance_id), sum(b.Rate)
from
Lead_Instance as a
inner join
Lead_transaction as b
on (a.Lead_transaction_id = b.Lead_transaction_id)
where
b.Status not in (select distinct Status_original from Status_Mapping)
and a.product_id = 6
and a.affiliate_id = 10

How to extract ids of the rows with minimum value in sql

I have the following table
RecordID Group Date Value
----------------------------------------
1 Test 1 1/01/2012 10
2 Test 1 1/01/2012 10
3 Test 1 1/01/2012 20
4 Test 2 1/01/2012 20
5 Test 1 2/01/2012 10
6 Test 2 2/01/2012 30
7 Test 1 2/01/2012 20
8 Test 1 2/01/2012 20
9 Test 2 2/01/2012 20
10 Test 1 3/01/2012 10
11 Test 2 3/01/2012 10
12 Test 2 3/01/2012 20
13 Test 3 3/01/2012 10
14 Test 1 4/01/2012 10
I need to get all RecordIds, where for the same date and same group it has the lowest value and disregard all records for the same date and group that have greater value. So my query needs to group by "date" and "group" and find records with lowest value, that is result should be:
RecordIds: 1, 2, 4, 5, 9, 10, 11, 13, 14
You can use rank in a sub query.
select T.RecordID,
T.[Group],
T.Date,
T.Value
from
(
select RecordID,
[Group],
Date,
Value,
rank() over(partition by [Group], Date order by Value) as rn
from YourTable
) as T
where T.rn = 1
order by T.RecordID
SQL Fiddle

Resources