How to fill in the gaps in a query of totals with zeroes? - sql

I have a table with data like this
picks
20
20
20
18
17
12
12
9
9
This is the table but I need to get result like this.
Picks Count
20 3
19 0
18 1
17 1
16 0
...up to
1 12
How can we write query to get zero totals for data which doesn't exist in the table?
Arun

Use a subquery to generate all the numbers and then outer join it to your table.
with nos as ( select level as pick_id
from dual
connect by level <= 20 )
select nos.pick_id
, count(*)
from nos
left outer join picks
on nos.pick_id = picks.id
group by nos.pick_id
order by nos.pick_id desc ;

Related

Counting SUM(VALUE) from previous cell

I have the following table:
A
Sum(Tickets)
01-2022
5
02-2022
2
03-2022
8
04-2022
1
05-2022
3
06-2022
3
07-2022
4
08-2022
1
09-2022
5
10-2022
5
11-2022
3
I would like to create the following extra column 'TotalSum(Tickets)' but I am stuck....
Anyone who can help out?
A
Sum(Tickets)
TotalSum(Tickets)
01-2022
5
5
02-2022
2
7
03-2022
8
15
04-2022
1
16
05-2022
3
19
06-2022
3
22
07-2022
4
26
08-2022
1
27
09-2022
5
32
10-2022
5
37
11-2022
3
40
You may use SUM() as a window function here:
SELECT A, SumTickets, SUM(SumTickets) OVER (ORDER BY A) AS TotalSumTickets
FROM yourTable
ORDER BY A;
But this assumes that you actually have a bona-fide column SumTickets which contains the sums. Assuming you really showed us the intermediate result of some aggregation query, you should use:
SELECT A, SUM(Tickets) AS SumTickets,
SUM(SUM(Tickets)) OVER (ORDER BY A) AS TotalSumTickets
FROM yourTable
GROUP BY A
ORDER BY A;
left join the same table where date is not bigger, then sum that for every date:
select
table1.date,
sum(t.tickets)
from
table1
left join table1 t
on t.date<= table1.date
group by
table1.date;

SQL Server: inner join running total

I'm doing some work on a SQL Server 2008 database (before partition) and trying to get my head running totals using inner joins. I've followed a few online tutorials but I can't get the desired results.
This is the underlying data...
Compdate count
------------------
2 198
3 29
4 22
5 27
6 31
9 18
10 16
11 22
12 26
etc...
and I need to add a running total column.
This is the query I've created so far...
select
t1.Compdate,
t1.count,
Sum(t2.count) as 'Total'
from
DB_KpiTr_Remo_CumComp_TV t1
inner join
(select count, compdate
from DB_KpiTr_Remo_CumComp_TV
where Month like 'l%') t2 on t1.Count >= t2.Count
and t1.Compdate = t2.Compdate
where
t1.Month like 'l%'
group by
t1.Compdate, t1.count
order by
t1.Compdate
select Compdate, count
from DB_KpiTr_Remo_CumComp_TV t1
But all I'm getting back is the exact same numbers in my total column...
Compdate count Total
-------------------------
2 198 198
3 29 29
4 22 22
etc...
I've tried several combinations of joins and I get several combinations of results but not the one I'm looking for - what am I missing here?
You can use apply:
select u.*, u2.running_count
from underlying u outer apply
(select sum(u2.count) as running_count
from underlying u2
where u2.compdate <= u.compdate
) u2

how to Get only the rows which's D column hold nearest lowest number to the C column?

------------------------------------------
ID Name C D
------------------------------------------
1 AK-47 10 5
2 RPG 10 20
3 Mp5 20 15
4 Sniper 20 18
5 Tank 90 80
6 Space12 90 20
7 Rifle 90 110
8 Knife 90 85
Consider 1,2 ; 3,4 ; 5,6,7,8 are as separate groups
So i need to get the row group wise that which's D column holds the nearest lower number to the C column
So the Expected Result is :
------------------------------------------
ID Name C D
------------------------------------------
1 AK-47 10 5
4 Sniper 20 18
8 Knife 90 85
How can I achieve this ?
select t1.*
from your_table t1
join
(
select c, min(abs(c-d)) as near
from your_table
group by c
) t2 on t1.c = t2.c and abs(t1.c-t1.d) = t2.near
Here is the syntax for another way of doing this. This uses a cte and will only hit the base table once.
with MySortedData as
(
select ID, Name, C, D, ROW_NUMBER() over(PARTITION BY C order by ABS(C - D)) as RowNum
from Something
)
select *
from MySortedData
where RowNum = 1

SQL To Select Records based on different values of a single column

Table
Que_id | question | isPicture | cat_det_id
1 Where are U? 1 27
2 Hello 0 22
3 Hey 1 31
4 What is Dis? 1 27
.. . . ........ .. ....
... ........... . ...
Given the table in the picture as sample, I want select different number records based on different values of cat_det_id.
For Instance to select 5 records that has cat_det_id of 27, 10 Records that has cat_det_id of 31 and 7 records that has cat_det_id of 22
and these records will be presented as a records set ordered by this same cat_det_id in ascending order.
Thank you.
EDIT: For some reason i assumed you were using Mysql, but you don't say that your question neither on tags, if you are using other RDMS you'll have to cahnge the LIMIT part since it is specific to mysql, but the union and order by will still work.
You could do it with union
(SELECT * FROM YOURTABLE WHERE cat_det_id = 27 LIMIT 5)
UNION
(SELECT * FROM YOURTABLE WHERE cat_det_id = 31 LIMIT 10)
UNION
(SELECT * FROM YOURTABLE WHERE cat_det_id = 22 LIMIT 7)
ORDER BY cat_det_id ASC

SUM aggregate function and Subquery -SQL

I'm using MS SQL Server and I have 2 tables.
Supply_list
sl_id(pk) supply_id(fk)* transaction_id Qty
1 14 872670099 3
2 15 872670100 5
3 16 872670101 1
4 16 872670105 4 <
supply_id is a foreign key to the supply_id in amenity table
Supply
supply_id(pk) no_of_units
----------------------------
13 2
14 3
15 6
16 10
The output should be supply_id then the no. of units available which is equal to No_of_units subtracted by the Qty.
output
id units available
-------------------------
13 2
14 0 --> [1]
15 1
16 5 --> [2]
[1] Since based on the supply_list table supply_id 14 has 3 for its Qty
[2] There are two records that contains supply_id 16 so we have to add their qty which are 4 and 1 so we have 5. And that 5 would be subtracted from the no_of_units of supply_id 16 and we will get 5 for units available.
You left outer JOIN the list table to the parent table
Subtract the SUM of the list Qty values from the parent no_of_units value
Use ISNULL in case there are no list rows
Something like
SELECT
S.supply_id,
S.no_of_units - ISNULL(SUM(SL.Qty), 0) AS [units available]
FROM
supply S
LEFT JOIN
supply_list SL ON S.supply_id = SL.supply_id
GROUP BY
S.supply_id, S.no_of_units
This makes the aggregate more obvious but is the same query
SELECT
S.supply_id,
S.no_of_units - ISNULL(SL.SumListQty, 0) AS [units available]
FROM
supply S
LEFT JOIN
(
SELECT supply_id, SUM(Qty) AS SumListQty
FROM supply_list
GROUP BY supply_id
) SL ON S.supply_id = SL.supply_id