How to Sum Entire Column with SQL - sql

I'm trying to Sum an entire column in SQL. The code below sums each row, but instead I just want a total row. My guess is it has to be done with GROUP BY, but the only way the query works is if I use Share_Type or Balance, neither of which sums the column. I also tried adding the CASE statement to the Group by (by leaving off 'AS MoneyMaxBalance" but I get an error message.
SELECT
CASE
WHEN SHARE_TYPE = 57 THEN SUM(BALANCE) ELSE 0
END AS MoneyMaxBalance
FROM SHARE
INNER JOIN ACCOUNT ON SHARE.MEMBER_NBR = ACCOUNT.MEMBER_NBR AND
SHARE.SHARE_NBR = ACCOUNT.ACCOUNT_NBR
INNER JOIN PRODUCT ON ACCOUNT.PRODUCT_CODE = PRODUCT.PRODUCT_CODE
GROUP BY SHARE_TYPE

If you just want one row, don't use group by:
SELECT SUM(BALANCE) AS MoneyMaxBalance
FROM SHARE s INNER JOIN
ACCOUNT a
ON s.MEMBER_NBR = a.MEMBER_NBR AND
s.SHARE_NBR = a.ACCOUNT_NBR INNER JOIN
PRODUCT p
ON a.PRODUCT_CODE = p.PRODUCT_CODE
WHERE SHARE_TYPE = 57;

SELECT SUM(CASE WHEN SHARE_TYPE = 57 THEN BALANCE ELSE 0 END) AS MoneyMaxBalance
FROM SHARE s
INNER JOIN ACCOUNT a ON s.MEMBER_NBR = a.MEMBER_NBR AND s.SHARE_NBR = a.ACCOUNT_NBR
INNER JOIN PRODUCT p ON a.PRODUCT_CODE = p.PRODUCT_CODE
Hope this Query Works fine for your case:

Related

I have a problem with left join, sum(), group by

I have 4 tables in the database
dbo.Product_Tbl
dbo.Buy_Details_Tbl
R_BuyDetails_Tbl
Sale_Details_Tbl
Re_Sale_Details_Tbl
They all have a one-to-many relationship with the first table. I am trying to get the sum of the quantities sold and bought, the sum total amount sold and bought, the sum quantities returned bought and sold, and the sum total amount returned bought and sold in the tables above
The problem is that this sentence gives a wrong sum. Sometimes you double the sum to two or three times.
This is the sentence that I'm trying to find the true solution but it is wrong sum.
SELECT dbo.Product_Tbl.Prd_ID, dbo.Product_Tbl.Prd_Code, dbo.Product_Tbl.Prd_Name, dbo.Cat_Tbl.CatName,
dbo.Unit_Tbl.UnitName, sum(isnull(dbo.Buy_Details_Tbl.Qty,0))as 'sumBuyQty', sum(isnull(dbo.Buy_Details_Tbl.TotalAmount,0))as 'sumBuyTotalAmount',
sum(isnull(dbo.R_BuyDetails_Tbl.Qty,0)) AS 'sumR_BuyQty',sum(isnull(dbo.R_BuyDetails_Tbl.TotalAmount,0))as 'sumR_BuyTotalAmount',
sum (isnull(dbo.Sale_Details_Tbl.Qty,0)) AS 'sumSaleQty',sum(isnull( dbo.Sale_Details_Tbl.TotalAmount,0)) AS'sumSaleTotalAmount',
sum(isnull( dbo.Re_Sale_Details_Tbl.Qty,0)) As'sumRe_SaleQty',sum( isnull(dbo.Re_Sale_Details_Tbl.TotalAmount,0)) AS 'sumRe_SaleTotalAmount'
FROM dbo.Product_Tbl left JOIN dbo.Buy_Details_Tbl ON
dbo.Product_Tbl.Prd_ID = dbo.Buy_Details_Tbl.Prd_ID left JOIN
dbo.R_BuyDetails_Tbl ON
dbo.Product_Tbl.Prd_ID = dbo.R_BuyDetails_Tbl.Prd_ID left JOIN
dbo.Re_Sale_Details_Tbl ON
dbo.Product_Tbl.Prd_ID = dbo.Re_Sale_Details_Tbl.Prd_ID left JOIN
dbo.Sale_Details_Tbl ON
dbo.Product_Tbl.Prd_ID = dbo.Sale_Details_Tbl.Prd_ID left JOIN
dbo.Cat_Tbl ON
dbo.Product_Tbl.Cat_ID = dbo.Cat_Tbl.Cat_ID left JOIN
dbo.Unit_Tbl ON
dbo.Product_Tbl.Unit_ID = dbo.Unit_Tbl.Unit_ID group by dbo.Product_Tbl.Prd_ID, dbo.Product_Tbl.Prd_Code, dbo.Product_Tbl.Prd_Name, dbo.Cat_Tbl.CatName , dbo.Unit_Tbl.UnitName
Rewriting your script using subqueries would look something like this:
SELECT
p.Prd_ID,
p.Prd_Code,
p.Prd_Name,
c.CatName,
u.UnitName,
(SELECT SUM(bd.Qty) FROM dbo.Buy_Details_Tbl bd WHERE bd.Prd_ID = p.Prd_ID) AS sumBuyQty,
(SELECT SUM(bd.TotalAmount) FROM dbo.Buy_Details_Tbl bd WHERE bd.Prd_ID = p.Prd_ID) AS sumBuyTotalAmount,
(SELECT SUM(rbd.Qty) FROM dbo.R_BuyDetails_Tbl rbd WHERE rbd.Prd_ID = p.Prd_ID) AS sumR_BuyQty,
(SELECT SUM(rbd.TotalAmount) FROM dbo.R_BuyDetails_Tbl rbd WHERE rbd.Prd_ID = p.Prd_ID) AS sumR_BuyTotalAmount,
(SELECT SUM(sd.Qty) FROM dbo.Sale_Details_Tbl sd WHERE sd.Prd_ID = p.Prd_ID) AS sumSaleQty,
(SELECT SUM(sd.TotalAmount) FROM dbo.Sale_Details_Tbl sd WHERE sd.Prd_ID = p.Prd_ID) AS sumSaleTotalAmount,
(SELECT SUM(rsd.Qty) FROM dbo.Re_Sale_Details_Tbl rsd WHERE rsd.Prd_ID = p.Prd_ID) AS sumRe_SaleQty,
(SELECT SUM(rsd.TotalAmount) FROM dbo.Re_Sale_Details_Tbl rsd WHERE rsd.Prd_ID = p.Prd_ID) AS sumRe_SaleTotalAmount
FROM dbo.Product_Tbl p
INNER JOIN dbo.Cat_Tbl c ON p.Cat_ID = c.Cat_ID
INNER JOIN dbo.Unit_Tbl u ON p.Unit_ID = u.Unit_ID
This query eliminates the multiple joins to the details tables.
The isnull function shouldn't be necessary since aggregrate functions such as SUM won't count nulls.

Oracle SQL - how to NOT SHOW athlete name that apears only once

created a view called winners, it contains the columns: athlete_name,year,medal_won
its basicly athletes that won olympic medal and the year,
it look like that,
data base is in live sql: https://livesql.oracle.com/apex/f?p=590:1000:0
select distinct year,athlete_name,medal
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
order by athlete_name
as you can see some name show only once and some names are showing more than once, i want to get rid off all lines of those who show ONLY ONCE, please help me.
thank you!
if i have understand your problem, must group your data,
select year,athlete_name,medal, count(*) "number of Medals"
from olym.olym_medals
join olym.olym_athlete_games on olym_athlete_games.id = olym_medals.athlete_game_id
join olym.olym_nations on olym_nations.id = olym_athlete_games.nation_id
join olym.olym_games on olym_games.id = Olym_athlete_games.game_id
join olym.olym_athletes on olym_athletes.id = olym_athlete_games.athlete_id
group by year,athlete_name,medal;
If I followed you correctly, you can use window functions:
select *
from (
select og.year, oa.athlete_name, om.medal, count(*) over(partition by oa.id) cnt
from olym.olym_medals om
join olym.olym_athlete_games oag on oag.id = om.athlete_game_id
join olym.olym_nations ona on ona.id = oag.nation_id
join olym.olym_games og on og.id = oag.game_id
join olym.olym_athletes oa on oa.id = oag.athlete_id
) t
where cnt > 1
order by athlete_name
Notes:
I am unsure why you were using distinct in the first place, so I removed it (I suspect it is actually not needed)
I added table aliases to shorten the query, and prefixed the columns in the select clause with the table they belong to (you might want to review that) - these are best practices when dealing with multi-table queries
Use GROUP BY and HAVING COUNT(*) > 1:
SELECT year,
athlete_name,
medal
FROM olym.olym_medals
INNER JOIN olym.olym_athlete_games
ON olym_athlete_games.id = olym_medals.athlete_game_id
INNER JOIN olym.olym_nations
ON olym_nations.id = olym_athlete_games.nation_id
INNER JOIN olym.olym_games
ON olym_games.id = Olym_athlete_games.game_id
INNER JOIN olym.olym_athletes
ON olym_athletes.id = olym_athlete_games.athlete_id
GROUP BY
year,
athlete_name,
medal
HAVING COUNT(*) > 1
ORDER BY athlete_name

SQL Query with counts only returning equivalent counts

I have a query that consists of 1 table and 2 sub queries. The table being a listing of all customers, 1 sub query is a listing all of the quotes given over a period of time for customers and the other sub query is a listing of all of the orders booked for a customer over the same period of time. What I am trying to do is return a result set that is a customer, the number of quotes given, and the number of orders booked over a given period of time. However what I am returning is only a listening of customers over the period of time that have an equivalent quote and order count. I feel like I am missing something obvious within the context of the query but I am unable to figure it out. Any help would be appreciated. Thank you.
Result Set should look like this
Customer-------Quotes-------Orders Placed
aaa----------------4----------------4
bbb----------------9----------------18
ccc----------------18----------------9
select
[Customer2].[Name] as [Customer2_Name],
(count( Quotes.UD03_Key3 )) as [Calculated_CustomerQuotes],
(count( Customer_Bookings.OrderHed_OrderNum )) as [Calculated_CustomerBookings]
from Erp.Customer as Customer2
left join (select
[UD03].[Key3] as [UD03_Key3],
[UD03].[Key4] as [UD03_Key4],
[UD03].[Key1] as [UD03_Key1],
[UD03].[Date02] as [UD03_Date02]
from Ice.UD03 as UD03
inner join Ice.UD02 as UD02 on
UD03.Company = UD02.Company
And
CAST(CAST(UD03.Number09 AS INT) AS VARCHAR(30)) = UD02.Key1
left outer join Erp.Customer as Customer on
UD03.Company = Customer.Company
And
UD03.Key1 = Customer.Name
left outer join Erp.SalesTer as SalesTer on
Customer.Company = SalesTer.Company
And
Customer.TerritoryID = SalesTer.TerritoryID
left outer join Erp.CustGrup as CustGrup on
Customer.Company = CustGrup.Company
And
Customer.GroupCode = CustGrup.GroupCode
where (UD03.Key3 <> '0')) as Quotes on
Customer2.Name = Quotes.UD03_Key1
left join (select
[Customer1].[Name] as [Customer1_Name],
[OrderHed].[OrderNum] as [OrderHed_OrderNum],
[OrderDtl].[OrderLine] as [OrderDtl_OrderLine],
[OrderHed].[OrderDate] as [OrderHed_OrderDate]
from Erp.OrderHed as OrderHed
inner join Erp.Customer as Customer1 on
OrderHed.Company = Customer1.Company
And
OrderHed.BTCustNum = Customer1.CustNum
inner join Erp.OrderDtl as OrderDtl on
OrderHed.Company = OrderDtl.Company
And
OrderHed.OrderNum = OrderDtl.OrderNum) as Customer_Bookings on
Customer2.Name = Customer_Bookings.Customer1_Name
where Quotes.UD03_Date02 >= '5/15/2018' and Quotes.UD03_Date02 <= '5/15/2018' and Customer_Bookings.OrderHed_OrderDate >='5/15/2018' and Customer_Bookings.OrderHed_OrderDate <= '5/15/2018'
group by [Customer2].[Name]
You have several problems going on here. The first problem is your code is so poorly formatted it is user hostile to look at. Then you have left joins being logically treated an inner joins because of the where clause. You also have date literal strings in language specific format. This should always be the ANSI format YYYYMMDD. But in your case your two predicates are contradicting each other. You have where UD03_Date02 is simultaneously greater than and less than the same date. Thankfully you have =. But if your column is a datetime you have prevented any rows from being returned again (the first being your where clause). You have this same incorrect date logic and join in the second subquery as well.
Here is what your query might look like with some formatting so you can see what is going on. Please note I fixed the logical join issue. You still have the date problems because I don't know what you are trying to accomplish there.
select
[Customer2].[Name] as [Customer2_Name],
count(Quotes.UD03_Key3) as [Calculated_CustomerQuotes],
count(Customer_Bookings.OrderHed_OrderNum) as [Calculated_CustomerBookings]
from Erp.Customer as Customer2
left join
(
select
[UD03].[Key3] as [UD03_Key3],
[UD03].[Key4] as [UD03_Key4],
[UD03].[Key1] as [UD03_Key1],
[UD03].[Date02] as [UD03_Date02]
from Ice.UD03 as UD03
inner join Ice.UD02 as UD02 on UD03.Company = UD02.Company
And CAST(CAST(UD03.Number09 AS INT) AS VARCHAR(30)) = UD02.Key1
left outer join Erp.Customer as Customer on UD03.Company = Customer.Company
And UD03.Key1 = Customer.Name
left outer join Erp.SalesTer as SalesTer on Customer.Company = SalesTer.Company
And Customer.TerritoryID = SalesTer.TerritoryID
left outer join Erp.CustGrup as CustGrup on Customer.Company = CustGrup.Company
And Customer.GroupCode = CustGrup.GroupCode
where UD03.Key3 <> '0'
) as Quotes on Customer2.Name = Quotes.UD03_Key1
and Quotes.UD03_Date02 >= '20180515'
and Quotes.UD03_Date02 <= '20180515'
left join
(
select
[Customer1].[Name] as [Customer1_Name],
[OrderHed].[OrderNum] as [OrderHed_OrderNum],
[OrderDtl].[OrderLine] as [OrderDtl_OrderLine],
[OrderHed].[OrderDate] as [OrderHed_OrderDate]
from Erp.OrderHed as OrderHed
inner join Erp.Customer as Customer1 on OrderHed.Company = Customer1.Company
And OrderHed.BTCustNum = Customer1.CustNum
inner join Erp.OrderDtl as OrderDtl on OrderHed.Company = OrderDtl.Company
And OrderHed.OrderNum = OrderDtl.OrderNum
) as Customer_Bookings on Customer2.Name = Customer_Bookings.Customer1_Name
and Customer_Bookings.OrderHed_OrderDate >= '20180515'
and Customer_Bookings.OrderHed_OrderDate <= '20180515'
group by [Customer2].[Name]
COUNT() will just give you the number of records. You'd expect this two result columns to be equal. Try structuring it like this:
SUM(CASE WHEN Quote.UD03_Key1 IS NOT NULL THEN 1 ELSE 0 END) AS QuoteCount,
SUM(CASE WHEN Customer_Bookings.Customer1_Name IS NOT NULL THEN 1 ELSE 0 END) AS custBookingCount

postgresql count distinct on differect condition

I'm stuck on an exercise where I need to count the total amount of unique visits to planets, but if the planet is the moon (maan), then it should be counted twice. Also the client number should be 121
select case
when objectnaam = 'Maan' then count(objectnaam)
else count(distinct objectnaam)
end as aantal_bezoeken
from klanten inner join deelnames on klanten.klantnr = deelnames.klantnr
inner join reizen on deelnames.reisnr = reizen.reisnr
inner join bezoeken on reizen.reisnr = bezoeken.reisnr
where klanten.klantnr = 121
group by objectnaam
And it gives me this result
aantal_bezoeken
1
4
1
1
but the result should be
aantal_bezoeken
7
I just need to add all these values together but I don't know how to,
or maybe there's a better more simple solution. It should be without subqueries
Try this:
select sum(aantal_bezoeken) as aantal_bezoeken from
(select case
when objectnaam = 'Maan' then count(objectnaam)
else count(distinct objectnaam)
end as aantal_bezoeken
from klanten inner join deelnames on klanten.klantnr = deelnames.klantnr
inner join reizen on deelnames.reisnr = reizen.reisnr
inner join bezoeken on reizen.reisnr = bezoeken.reisnr
where klanten.klantnr = 121
group by objectnaam) as a

SQL IN statement not checking properly?

I am working with a column/field from a table that has multiple "codes" or "ids" associated with it.
The table afacctbal has a column/field called afacbbalid, where the possibilities can be COST, PRN, or COLL.
What I am having trouble with is pulling the rows where COST is equal to 0 on the account.
I have tried a sub-queried SQL statement in the where clause, but returns a general error. Right now I am trying an IN statement but that doesn't seem to work either.
Here is what I've got:
select araccount.aracid as AccountID, arentity.arenst State, arentity.ARENNAME ClientName,
afaccount.afaccurbal CurrentBal, afacctbal.afacbcurbal PrincipalBal,
afacctbal.AFACBbalid
from araccount
inner join arrelationship on araccount.aracid = arrelationship.arrelacid
inner join arentity on arentity.arenid = arrelationship.ARRELENID
inner join afaccount on afaccount.afacacctid = araccount.ARACID
inner join afacctbal on afaccount.AFACKEY = afacctbal.AFACBACCTID
where afacctbal.afacbbalid in("COST",0)
and afaccount.AFACRATEID = "MN100"
and arentity.ARENST = "MN"
and araccount.araclstdte > "2013-04-01"
order by afaccount.afaccurbal
The trouble lies within my where clause
Here:
where afacctbal.afacbbalid in("COST",0)
How would I check for COST and check to see if its equal to 0?
AFACCTBAL TABLLE
Used Values:
afacbbalid -- balance id
afacbcurbal -- balance amount per balance id.
afaccount Table
Used Values:
afaccurbal -- current balance
You are using "COST" which is string and then 0 which is an integer. So, you need to cast both in the datatype which column afacctbal.afacbbalid has.
I think below query will work for your requirement -
select araccount.aracid as AccountID, arentity.arenst State, arentity.ARENNAME ClientName,
afaccount.afaccurbal CurrentBal, afacctbal.afacbcurbal PrincipalBal,
afacctbal.AFACBbalid
from araccount
inner join arrelationship on araccount.aracid = arrelationship.arrelacid
inner join arentity on arentity.arenid = arrelationship.ARRELENID
inner join afaccount on afaccount.afacacctid = araccount.ARACID
inner join afacctbal on afaccount.AFACKEY = afacctbal.AFACBACCTID
where ((afacctbal.afacbbalid ='COST'
and afaccurbal.currentBal = 0) or
(afacctbal.afacbbalid ='PRN'
and afaccurbal.currentBal > 0))
and afaccount.AFACRATEID = "MN100"
and arentity.ARENST = "MN"
and araccount.araclstdte > "2013-04-01"
order by afaccount.afaccurbal