Sum is not getting after grouping also in SQL Server - sql

I have a query like this.
select
TC.F_Exhibition_Code, TC.F_Exhibition,
c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson,
c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate,
TC.F_Paymentmethod, TC.F_Currency,
TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt,
sum(TC.F_Totalamt) as sum
from
T_CreditInvoice TC
left join
T_Customer c on c.F_Customer_Code = tc.F_Customer_Code
where
TC.F_CreditInvoiceNo = 'INV100098'
group by
TC.F_Exhibition_Code, TC.F_Exhibition, c.F_Customer_Code, c.F_Customer_Name,
c.F_Address, c.F_ContactPerson, c.F_Phone, c.F_Fax,
Tc.F_CreditInvoiceNo, tc.F_CreditInvoiceDate, TC.F_Paymentmethod,
TC.F_Currency, TC.F_Description, TC.F_Price,
TC.F_quanity, TC.F_ReceivedAmt, TC.F_Totalamt
I trying to get sum of my F_Totllamt column, but I am not getting it.
What is wrong with my query?

You can use window function and write as:
sum(TC.F_Totalamt) OVER (PARTITION BY TC.F_CreditInvoiceNo ORDER BY (SELECT 1))as sum
in existing query.. hope it helps!!
Demo

You're grouping on the variable you want to get the sum of. Hence you get the sum of every single "TC.F_Totalamt" because you're also grouping over that same variable.
Just remove "TC.F_Totalamt" from the "group by" statement and it will work.

Related

How to avoid using aggregation function in another aggregation function in ClickHouse

I want to get the average number of days between creation user profile ads_clients_data.create_date and the first day of its posts min(ads_data.time).
I wrote the following SQL statement:
select
avg(dateDiff(dd, ads_clients_data.create_date, ads_data.time),
min(ads_data.time))
from
ads_data
inner join
ads_clients_data on ads_clients_data.client_union_id = ads_data.client_union_id;
But it is impossible to use min in avg function. It is the first time I worked with ClickHouse and would be very thankful if someone can help me.
If I follow you correctly, you can use two levels of aggregation:
select avg(datediff(dd, c.create_date, d.min_time)
from (
select client_union_id, min(ads_data.time) min_time
from ads_data
group by client_union_id
) d
inner join ads_clients_data c on c.client_union_id = d.client_union_id;

How to use SUM in this situation?

I have the following tables below and their schema:
INV
id, product code, name, ucost, tcost, desc, type, qoh
1,123,CPASS 700,1.00,5.00,CPASS 700 Lorem, COM,5
2,456,Shelf 5,2.00,6.00,Shelf 5 KJ, BR,3
GRP
id,type,desc
1,COM,COMPASS
2,BR,SHELF
Currently I have a query like this:
SELECT INV.*,GRP.DESCR AS CATEGORY
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
There is no problems with that query.
Right now,I want to know the SUM of the TCOST of every INV record where their QOH is 0.
In this situation, does that I mean all I have to do is to write a separate query like the one below:
SELECT SUM(TCOST)
FROM INV
WHERE QOH = 0
Does it make any sense for me to try and combine those two queries as one ?
First understand that SUM is the aggregate function hence either you can run the Query like
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as total
This will return Sum of TCOST in INV Table for mentioned condition.
Another approach is finding the Sum based on the some column (e.g. Type)
you could write query like
SELECT Type , SUM(TCOST) FROM INV WHERE QOH=0 GROUP BY type ;
Its not clear on what criteria you want to sum . But I think above two approaches would provide you fare idea .
Mmm, you could maybe use a correlated query, though i'm not sure it's the best approach since I'm not sure I understand what your attempting to do:
SELECT INV.*,
GRP.DESCR AS CATEGORY ,
(SELECT SUM(TCOST) FROM INV WHERE QOH=0) as your_sum
FROM INV LEFT JOIN GRP ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0
If you want only one value for the sum(), then your query is fine. If you want a new column with the sum, then use window functions:
SELECT INV.*, GRP.DESCR AS CATEGORY,
SUM(INV.TCOST) OVER () as sum_at_zero
FROM INV LEFT JOIN
GRP
ON INV.TYPE = GRP.TYPE
WHERE INV.QOH = 0;
It does not make sense to combine the queries by adding a row to the first one, because the columns are very different. A SQL result set requires that all rows have the same columns.

"Your query does not include the specified expression..."

I have tried endless things to get this to work and it seems to break over and over again and not work. I'm trying to GROUP BY product after I have calculated the field quantity returned/quantity ordered, but I get the error
your query does not include the specified expression 'quantity_returned/quantity_ordered' as part of an aggregate function.
I do not want to GROUP BY quantity_returned, quantity_ordered, and product, I only want to GROUP BY product.
Here's what my SQL looks like currently...
SELECT
quantity_returned/quantity_ordered AS percentage_returned,
quantity_returned,
quantity_ordered,
returns_fact.product
FROM
Customer_dimension
INNER JOIN
(
Product_dimension
INNER JOIN
(
Day_dimension
INNER JOIN
returns_fact
ON Day_dimension.day_key = returns_fact.day_key
)
ON Product_dimension.product_key = returns_fact.product_key
)
ON Customer_dimension.customer_key = returns_fact.customer_key
GROUP BY returns_fact.product;
When you use a group by you need to actually include everything in your select that isn't a aggregate function.
I have no idea how your tables are set up, but I am throwing a blind dart. If you provide fields in each of the 4 tables someone will be better able to help.
SELECT returns_fact.product, count(quantity_returned), count(quantity_ordered), count(quantity_returned)/count(quantity_ordered) as percentage returned

SQL COUNT FORM JOIN TABLES

I have the following sql command:
SELECT "USERNAME"."TOPICS".VALUE,
"USERNAME"."TOPICS".QID,
"USERNAME"."QUESTION".QRATING
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
It works really well, but I want to count how many element returns. So I tried to use:
SELECT COUNT("USERNAME"."TOPICS".QID)
FROM "USERNAME"."TOPICS" JOIN "USERNAME"."QUESTION"
ON "USERNAME"."TOPICS".QID = "USERNAME"."QUESTION".QID
AND "USERNAME"."TOPICS".VALUE = 'kia'
ORDER BY QRATING DESC
But I get the error :
Column reference 'USERNAME.TOPICS.VALUE' is invalid. When the SELECT
list contains at least one aggregate then all entries must be valid
aggregate expressions.
What is the problem?
Hmmm. The ORDER BY should be getting the error, not the SELECT. However, your query would be much easier to understand using table aliases:
SELECT COUNT(t.QID)
FROM "USERNAME"."TOPICS" t JOIN
"USERNAME"."QUESTION" q
ON t.QID = q.QID AND t.VALUE = 'kia';
If the first query works, I see no reason why this would not (and your original without the ORDER BY should also work).

Where Multiple Values SQL SERVER

For this code I need the sum of the serv1 - serv8 where the student is receiving "Speech Language Therapy". I can not for the life of me figure out how to get those. If I say where serv1 equals 'SLT' then it will show up as null for serv 2. If I do it for serv2 it will show up as another service like DD for serv1.
My sum isn't working I would like to sum all of the services up by school but I'll take what anyone is willing to help me with. Any help is appreciated
SELECT SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name AS [[[School Name]]]]]]],
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SUM (SPE_Student_Profile_VW.mins1),
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
FROM DBO.SPE_Student_Profile_VW SPE_Student_Profile_VW
WHERE ( SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47')
GROUP BY SPE_Student_Profile_VW.school_code,
SPE_Student_Profile_VW.organization_name,
SPE_Student_Profile_VW.sis_number,
SPE_Student_Profile_VW.primary_disability_code,
SPE_Student_Profile_VW.secondary_disability_code,
SPE_Student_Profile_VW.tertiary_disability_code,
SPE_Student_Profile_VW.serv1,
SPE_Student_Profile_VW.mins1,
SPE_Student_Profile_VW.serv2,
SPE_Student_Profile_VW.mins2,
SPE_Student_Profile_VW.serv3,
SPE_Student_Profile_VW.mins3,
SPE_Student_Profile_VW.serv4,
SPE_Student_Profile_VW.mins4,
SPE_Student_Profile_VW.serv5,
SPE_Student_Profile_VW.mins5,
SPE_Student_Profile_VW.serv6,
SPE_Student_Profile_VW.mins6,
SPE_Student_Profile_VW.serv7,
SPE_Student_Profile_VW.mins7,
SPE_Student_Profile_VW.serv8,
SPE_Student_Profile_VW.mins8
the column you are trying to sum should not be in the group by clause, all the others should.
What I mean is the SELECT clause has this:
SUM (SPE_Student_Profile_VW.mins1),
and the GROUP BY has this part which should not be there:
SPE_Student_Profile_VW.mins1,
because it is the column he is trying to sum and you can't group by a column that you are aggregating with a function and that is why SUM isn't working
not sure of what is required but from what I get, you can probably get what you need if you add another
OR
in your
WHERE
Something like
WHERE (SPE_Student_Profile_VW.serv1 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
(SPE_Student_Profile_VW.serv2 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47') OR
SPE_Student_Profile_VW.serv3 = 'Speech Language Therapy'
AND SPE_Student_Profile_VW.school_code = '47'
and so on.
Thanks.
In Group by clause use all columns which you are used in select statement.Except the column you are calculating sum in select statement.
Thanks.