How to get count based on Values in SQL - sql

I've something to execute I need the count based on values. Here is my Table
"ORD_NUM","ORD_AMOUNT","ORD_DATE","CUST_CODE","AGENT_CODE","ORD_DESCRIPTION"
"200118"|"500"|"07/20/2008"|"C00023"|"A006"|"SOD"
"200120"|"500"|"07/20/2008"|"C00009"|"A002"|"SOD"
"200129"|"1000"|"07/20/2008"|"C00024"|"A006"|"SOD"
"200127"|"1000"|"08/20/2008"|"C00015"|"A003"|"SOD"
"200128"|"500"|"08/20/2008"|"C00009"|"A002"|"SOD"
"200128"|"500"|"09/20/2008"|"C00009"|"A002"|"SOD"
"200128"|"1000"|"09/20/2008"|"C00009"|"A002"|"SOD"
"200128"|"1000"|"10/20/2008"|"C00009"|"A002"|"SOD"
In the order amount we only have either 1000 or 500. We need to return the data count of 500 and 1000 for each date. EX:
Date |1000Count|500Count
07/20/2008| 1 | 2
08/20/2008| 1 | 1
09/20/2008| 1 | 1
10/20/2008| 1 | 1

Thanks! to SlavaRozhnev
select
d,
count(case when amount = 500 then 1 end) count500,
count(case when amount = 1000 then 1 end) count1000
from test
group by d;
Gives Result :
+============+==========+===========+
| d | count500 | count1000 |
+============+==========+===========+
| 2023-01-01 | 4 | 2 |
| 2023-01-02 | 1 | 1 |
| 2023-01-03 | 0 | 1 |
| 2023-01-04 | 1 | 0 |
+------------+----------+-----------+

Related

SQL Grouping by year gives incorrect results

I am trying to summerize sales date, by month, sales region and type. The problem is, the results change when I try to group by year.
My simplified query is as follows:
SELECT
DAB700.DATUM,DAB000.X_REGION,DAB700.BELEG_ART, // the date, sales region, order type
// calculate the number of orders per month
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 1 THEN DAB700.BELEG_NR END) as jan,
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 2 THEN DAB700.BELEG_NR END) as feb,
COUNT (DISTINCT CASE WHEN MONTH(DAB700.DATUM) = 3 THEN DAB700.BELEG_NR END) as mar
FROM "DAB700.ADT" DAB700
left join "DAB050.ADT" DAB050 on DAB700.BELEG_NR = DAB050.ANUMMER // join to table 050, to pull in order info
left join "DF030000.DBF" DAB000 on DAB050.KDNR = DAB000.KDNR // join table 000 to table 050, to pull in customer info
left join "DAB055.ADT" DAB055 on DAB050.ANUMMER = left (DAB055.APNUMMER,6)// join table 055 to table 050, to pull in product info
WHERE (DAB700.BELEG_ART = 10 OR DAB700.BELEG_ART = 20) AND (DAB700.DATUM>={d '2021-01-01'}) AND (DAB700.DATUM<={d '2021-01-11'}) AND DAB055.ARTNR <> '999999' AND DAB055.ARTNR <> '999996' AND DAB055.TERMIN <> 'KW.22.22' AND DAB055.TERMIN <> 'KW.99.99' AND DAB050.AUF_ART = 0
group by DAB700.DATUM,DAB000.X_REGION,DAB700.BELEG_ART
This returns the following data, which is correct (manually checked):
| DATUM | X_REGION | BELEG_ART | jan | feb | mar |
|------------|----------|-----------|-----|-----|-----|
| 04.01.2021 | 1 | 10 | 3 | 0 | 0 |
| 04.01.2021 | 3 | 10 | 2 | 0 | 0 |
| 04.01.2021 | 4 | 10 | 1 | 0 | 0 |
| 04.01.2021 | 4 | 20 | 1 | 0 | 0 |
| 04.01.2021 | 6 | 20 | 2 | 0 | 0 |
| 05.01.2021 | 1 | 10 | 1 | 0 | 0 |
and so on....
The total number of records for Jan is 117 (correct).
Now I now want to summerize the data in one row (for example, data grouped by region and type)..
so I change my code so that I have:
SELECT
YEAR(DAB700.DATUM),
and
group by YEAR(DAB700.DATUM)
the rest of the code stays the same.
Now my results are:
| EXPR | X_REGION | BELEG_ART | jan | feb | mar |
|------|----------|-----------|-----|-----|-----|
| 2021 | 1 | 10 | 16 | 0 | 0 |
| 2021 | 1 | 20 | 16 | 0 | 0 |
| 2021 | 2 | 10 | 19 | 0 | 0 |
| 2021 | 2 | 20 | 22 | 0 | 0 |
| 2021 | 3 | 10 | 12 | 0 | 0 |
| 2021 | 3 | 20 | 6 | 0 | 0 |
Visually it is correct. But, the total count for January is now 116. A difference of 1. What am I doing wrong?
How can I keep the results from the first code - but have it presented as per the 2nd set?
You count distinct BELEG_NR. This is what makes the difference. Let's look at an example. Let's say your table contains four rows:
DATUM
X_REGION
BELEG_ART
BELEG_NR
04.01.2021
1
10
100
04.01.2021
1
10
200
05.01.2021
1
10
100
05.01.2021
1
10
300
That gives you per day, region and belegart:
DATUM
X_REGION
BELEG_ART
DISTINCT COUNT BELEG_NR
04.01.2021
1
10
2
05.01.2021
1
10
2
and per year, region and belegart
YEAR
X_REGION
BELEG_ART
DISTINCT COUNT BELEG_NR
2021
1
10
3
The BELEG_NR 100 never appears more than once per day, so every instance gets counted. But it appears twice for the year, so it gets counted once instead of twice.

SQL "Group" and "Count" categories

Edit. This is a follow up from another question. To simplify the question. Assume a table
date | id | type
01/01 | 1 | F
02/01 | 1 | F
02/01 | 1 | F
03/01 | 1 | S
03/01 | 1 | S
04/01 | 1 | F
04/01 | 1 | S
05/01 | 1 | S
I am looking for a way to summarise the above table by combination of transaction types per day. If a person (id) has only one transaction per day it counts as a Single type. If they have more than one it counts as a Multiple one. I've done that with my original query and it works. The output from the above table would be:
date | Single | Multiple
01/01 | 1 | 0
02/01 | 0 | 1
03/01 | 0 | 1
04/01 | 0 | 1
05/01 | 1 | 0
I got that far and it works. What's I'm struggling with (ie. don't have a clue of how to start) is how set up a query to show all possible combinations of Type (SS, FF, FS) instead of just counting the multiple transactions. The desired output would be like:
date | Single | # FF | # FS | # SS
01/01 | 1 | 0 | 0 | 0
02/01 | 0 | 1 | 0 | 0
03/01 | 0 | 0 | 0 | 1
04/01 | 0 | 0 | 1 | 0
05/01 | 1 | 0 | 0 | 0
Any constructive hints or ideas will be much appreciated.
this is assuming that you have max 2 types per date.
You can use the CASE WHEN statement with MIN() and MAX() to check for combination of FF, FS or SS
select [date],
case when count(*) = 1 then 1 else 0 end as Single,
case when count(*) >= 2
and min([type]) = 'F'
and max([type]) = 'F'
then 1
else 0
end as [# FF],
case when count(*) >= 2
and min([type]) = 'F'
and max([type]) = 'S'
then 1
else 0
end as [# FS],
case when count(*) >= 2
and min([type]) = 'S'
and max([type]) = 'S'
then 1
else 0
end as [# SS]
from yourtable
group by [date]
EDIT :
for more then 3 types, just change the count(*) = 2 to count(*) >= 2 as long as the type are either F or S

SQL sum total each column in last row

I wish SQL for SUM each column(IPO and UOR) in TOTAL in second last. And GRAND TOTAL(Sum IPO + UOR) in the last one. Thank you so much
No Code IPO UOR
----------------------
1 D173 1 0
2 D176 3 0
3 D184 1 1
4 D185B 1 0
5 D187 1 2
6 F042 3 0
7 ML004 12 3
8 TTPMC 2 0
9 Z00204 1 0
------------------
TOTAL (NOS) 25 6
-------------------------
GRAND TOTAL (NOS) 31
Here is my code, :
SELECT
SUM(CASE WHEN IPOType = 'IPO' THEN 1 ELSE 0 END) as IPO,
SUM(CASE WHEN IPOType = 'UOR' THEN 1 ELSE 0 END) as UOR
FROM IPO2018
GROUP BY OriProjNo
it can show like this
No Code IPO UOR
----------------------
1 D173 1 0
2 D176 3 0
3 D184 1 1
4 D185B 1 0
5 D187 1 2
6 F042 3 0
7 ML004 12 3
8 TTPMC 2 0
9 Z00204 1 0
------------------
Generally speaking, you want to leave totals and sub-totals to whatever tool you are presenting your data in, as they will be able to handle the formatting with significantly more ease. In addition, your desired output does not have the same number of columns (Grand Total row only has one numeric) so even if you did shoehorn this in to the same dataset, the column headings wouldn't make sense.
That said, you can return group totals via the with rollup statement. This will provide an additional row with the aggregate totals for the group. Where there is more than one group in your data, you will get a sub-total row for each group and a total row for the entire dataset:
declare #t table(c nvarchar(10),t nvarchar(3));
insert into #t values ('D173','IPO'),('D176','IPO'),('D176','IPO'),('D176','IPO'),('D184','IPO'),('D184','UOR'),('D185B','IPO'),('D187','IPO'),('D187','UOR'),('D187','UOR'),('F042','IPO'),('F042','IPO'),('F042','IPO'),('TTPMC','IPO'),('TTPMC','IPO'),('Z00204','IPO'),('ML004','UOR'),('ML004','UOR'),('ML004','UOR'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO'),('ML004','IPO');
select row_number() over (order by grouping(c),c) as n
,case when grouping(c) = 1 then 'TOTAL (NOS)' else c end as c
,sum(case when t = 'IPO' then 1 else 0 end) as IPO
,sum(case when t = 'UOR' then 1 else 0 end) as UOR
from #t
group by c
with rollup
order by grouping(c)
,c;
Output:
+----+-------------+-----+-----+
| n | c | IPO | UOR |
+----+-------------+-----+-----+
| 1 | D173 | 1 | 0 |
| 2 | D176 | 3 | 0 |
| 3 | D184 | 1 | 1 |
| 4 | D185B | 1 | 0 |
| 5 | D187 | 1 | 2 |
| 6 | F042 | 3 | 0 |
| 7 | ML004 | 12 | 3 |
| 8 | TTPMC | 2 | 0 |
| 9 | Z00204 | 1 | 0 |
| 10 | TOTAL (NOS) | 25 | 6 |
+----+-------------+-----+-----+

SQL charge reversal data

I'm extracting financial information, but ran into charge reversal information. Basically if someone was charged for a service there would be a column with that charge. If the charge was later reversed there would be another row with the exact same data, but with a charge reversal flag on it. I want to only get charges that are were not reversed at all.
Below is an example of what i mean and need. As you can see the RVSLInd column has a 1 if the charge is a reversal. The 0 represents an initial charge
I couldn't do: select * from from table where rvslInd = 0. because this would get rid of the reversal row only.
RvslInd|ExtPriceAmt
-------| ----------|
0 | 155.70 |
0 | 1.50 |
0 | 239.00 |
0 | 1111.00 |
1 | -1111.00 |
0 | 217.00 |
0 | 1491.00 |
1 | -1491.00 |
0 | 388.00 |
0 | 72.00 |
This is what I want to be able to get back:
RvslInd|ExtPriceAmt
-------| ----------|
0 | 155.70 |
0 | 1.50 |
0 | 239.00 |
0 | 217.00 |
0 | 388.00 |
0 | 72.00 |
this would be my new table with a customer column added:
CustomerID|RvslInd|ExtPriceAmt
----------|-------| ----------|
1 | 0 | 155.70 |
1 | 0 | 1.50 |
1 | 0 | 239.00 |
2 | 0 | 217.00 |
2 | 0 | 388.00 |
2 | 0 | 72.00 |
Given your data, you cannot reliably do what you want. For the data you have shown, you could do:
select ExtPriceAmt
from t
where RvslInd = 0 and
not exists (select 1 from t t2 where t2.ExtPriceAmt = - t.ExtPriceAmt and t2.RvslInd = 1);
The problem is when the price is repeated. That gets in the way.
That said, all is not hopeless. You can get a list of the prices along with the number of non-reversed times:
select ExtPriceAmt,
sum(case when RvslInd = 0 then 1 when RvslInd = 1 then -1 end) as non_reversed_count
from t
group by ExtPriceAmt
having sum(case when RvslInd = 0 then 1 when RvslInd = 1 then -1 end) > 0;

SQL column sum and difference

my table, I want to create three columns in single-column, reprinted now.
id | date | type | total
------ | ------ | ------ | -----
1 | 01.10.2016| Paypal | 50
2 | 03.10.2016| credit | 40
3 | 05.10.2016| Cash | 50
4 | 06.10.2016| payment| 100
5 | 07.10.2016| Cash | 20
6 | 15.10.2016| Skrill | 10
7 | 18.10.2016| payment| 20
8 | 19.10.2016| Paypal | 10
9 | 19.10.2016| payment| 20
10 | 22.10.2016| Cash | 40
11 | 23.10.2016| Skrill | 10
my table, I want to create three columns in single-column, reprinted now.
SELECT id,date,type,total
(select (
sum(case when type="Paypal" then total else 0 end)+
sum(case when type="credit" then total else 0 end))+
sum(case when type="Cash" then total else 0 end) ) as receiv,
(Select(
sum(case when type="payment" then total else 0 end)) AS payment,
(Select sum(receiv -payment) FROM totals t2
WHERE (t2.date <= t1.date) and (t2.id <= t1.id) order by t1.date) AS remainder
FROM totals t1
group by date, type
order by id,date
--
The following query for the sql code?
Type = "Paypal, credit, Cash" sums "receiv" sums and Type = "payment" sums will be added to the "remainder" column.
id | date | type | receiv| payment| remainder
------ | ------ | ------ | ------| ------ | ------
1 | 01.10.2016| Paypal | 50 | 0 | 50
2 | 03.10.2016| credit | 40 | 0 | 90
3 | 05.10.2016| Cash | 50 | 0 | 140
4 | 06.10.2016| payment| 0 | 100 | 40
5 | 07.10.2016| Cash | 20 | 0 | 60
6 | 15.10.2016| Skrill | 10 | 0 | 70
7 | 18.10.2016| payment| 0 | 20 | 50
8 | 19.10.2016| Paypal | 10 | 0 | 60
9 | 19.10.2016| payment| 0 | 20 | 40
10 | 22.10.2016| Cash | 40 | 0 | 80
11 | 23.10.2016| Skrill | 10 | 0 | 90
Running total is easier in other databases which have analytical functions. In MySQL, you can do this with a correlated sub-query.
select id,dt,type,
case when type <> 'payment' then total else 0 end receiv,
case when type = 'payment' then total else 0 end payment,
case when type <> 'payment' then total else 0 end
- case when type = 'payment' then total else 0 end
+ coalesce((select sum(case when type <> 'payment' then total else 0 end)
- sum(case when type = 'payment' then total else 0 end)
from yourtable where id < y.id),0)
from yourtable y
Sample Demo