SQL Server 2008 Issue With GROUP BY - sql

SELECT
ETRN_MFTransactionGroup.FolioNumber,
ETRN_MFTransactionGroup.PrimaryApplicantContactID,
ETRN_MFTransaction.PK_TransactionGroupID AS Expr1,
ETRN_MFTransaction.PK_SchemeProductID,
ETRN_MFTransaction.Units,
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID,
PA_E_CUSTOMER.dbo.ECUS_Contact.UserID,
PA_E_CUSTOMER.dbo.ECUS_Contact.FName,
PA_E_CUSTOMER.dbo.ECUS_Contact.MName,
PA_E_CUSTOMER.dbo.ECUS_Contact.LName,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID AS Expr2,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.RegistrarCode,
ETRN_MFTransactionGroup.PK_TransactionGroupID
FROM
ETRN_MFTransactionGroup
INNER JOIN
ETRN_MFTransaction ON
ETRN_MFTransactionGroup.PK_TransactionGroupID = ETRN_MFTransaction.PK_TransactionGroupID
INNER JOIN
PA_E_CUSTOMER.dbo.ECUS_Contact ON
ETRN_MFTransactionGroup.PrimaryApplicantContactID =
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID
INNER JOIN
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct ON
ETRN_MFTransaction.PK_SchemeProductID =
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID
**GROUP BY
ETRN_MFTransactionGroup.FolioNumber,**
ETRN_MFTransactionGroup.PrimaryApplicantContactID,
ETRN_MFTransaction.PK_TransactionGroupID,
ETRN_MFTransaction.PK_SchemeProductID,
ETRN_MFTransaction.Units,
PA_E_CUSTOMER.dbo.ECUS_Contact.PK_ContactID,
PA_E_CUSTOMER.dbo.ECUS_Contact.UserID,
PA_E_CUSTOMER.dbo.ECUS_Contact.FName,
PA_E_CUSTOMER.dbo.ECUS_Contact.MName,
PA_E_CUSTOMER.dbo.ECUS_Contact.LName,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.PK_SchemeProductID,
PA_E_INSTRUMENTS.dbo.EINS_MFSchemeProduct.RegistrarCode,
ETRN_MFTransactionGroup.PK_TransactionGroupID
The query above works absolutely fine but i need to group only by FolioNumber (i.e - ETRN_MFTransactionGroup.FolioNumber). Grouping by rest of the fields not required at all!

If you don't want to group on the other fields, you need some aggregate function to tell SQL how to treat the multiple records. MAX, or MIN work well for varchar fields. SUM, AVERAGE work well for numerics.

Related

SQL Grand total without subtotals

I'm making a large SQL report in Orderwise, very roughly simplified as follows;
SELECT Supplier.SupplierName, POHeader.PODate, POHeader.PORef, POLine.LineID, SUM(Subquery.Val)
FROM Supplier INNER JOIN POHeader ON POheader.supplier = Supplier.SupplierID
INNER JOIN POLine ON POLine.HeaderID = POHeader.PO_ID
INNER JOIN Subquery on Subquery.POLine = POLine.Line_ID
GROUP BY Supplier.SupplierName, POHeader.PODate, POHeader.PORef, POLine.Line_ID
I want a grand total at the bottom, without a bunch of subtotals dotted in throughout the report - therefore I don't think I can use ROLLUP. The Subquery in there is of course a sub query and in the real thing there will be twelve of them and all pretty complex, so I want to avoid a UNION just to total everything up if at all possible. Is there any other way I can put a Grand total row at the bottom of the report without subtotals?
Not completely sure of the SQL version but if it helps, google tells me Microsoft SQL Server Express or SQL Server Standard can be used with OrderWise
Use GROUPING SETS:
SELECT Supplier.SupplierName, POHeader.PODate, POHeader.PORef, POLine.LineID, SUM(Subquery.Val)
FROM Supplier INNER JOIN
POHeader
ON POheader.supplier = Supplier.SupplierID JOIN
POLine
ON POLine.HeaderID = POHeader.PO_ID JOIN
Subquery
ON Subquery.POLine = POLine.Line_ID
GROUP BY GROUPING SETS ( (Supplier.SupplierName, POHeader.PODate, POHeader.PORef, POLine.Line_ID), () )

Use group by with sum in query

These 3 tables that you see in the image are related
Course table and coaching table and sales table
I want to make a report from this table on how much each coach has sold by each course period.
The query I created is as follows, but unfortunately it has a problem and I do not know where the problem is.
Please help me fix the problem
Thank you
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM
dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.pid
For this use, SUM() is an Aggregate Function, so you need to refer all the
fields that you want to get in your result set.
Example:
SELECT
dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid, dbo.tblPost.postTitle,
dbo.tblArticleAuthor.authorName, SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblCustomersOrders.id, dbo.tblCustomersOrders.pid,
dbo.tblPost.postTitle, dbo.tblArticleAuthor.authorName
But this query does not solve the need for your report.
If you just need to get "how much each coach has sold by each course" , you can try the query bellow.
SELECT
dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle,
SUM(dbo.tblCustomersOrders.prodPrice) AS TotalBuys
FROM dbo.tblPost
INNER JOIN
dbo.tblArticleAuthor ON dbo.tblPost.id = dbo.tblArticleAuthor.articleID
INNER JOIN
dbo.tblCustomersOrders ON dbo.tblPost.id = dbo.tblCustomersOrders.pid
GROUP BY dbo.tblArticleAuthor.authorName, dbo.tblPost.postTitle
If you need, send more details regarding the desired result.
Here you can find more information about SQL SERVER Aggregate Functions:
https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql?view=sql-server-ver15
And here a quick example regarding SQL Aliases to build queries with a simple
and effective way:
https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_alias_table
Per your description of the task, the problem is that you only GROUPed BY dbo.tblCustomersOrders.pid, which is the period's id I guess, but you also need to GROUP BY the coach, which is dbo.tblArticleAuthor.authorName, I guess again. Plus in the SELECT field list you can not use more columns only that are aggregated + GROUPed.

COUNT is outputting more than one row

I am having a problem with my SQL query using the count function.
When I don't have an inner join, it counts 55 rows. When I add the inner join into my query, it adds a lot to it. It suddenly became 102 rows.
Here is my SQL Query:
SELECT COUNT([fmsStage].[dbo].[File].[FILENUMBER])
FROM [fmsStage].[dbo].[File]
INNER JOIN [fmsStage].[dbo].[Container]
ON [fmsStage].[dbo].[File].[FILENUMBER] = [fmsStage].[dbo].[Container].[FILENUMBER]
WHERE [fmsStage].[dbo].[File].[RELATIONCODE] = 'SHIP02'
AND [fmsStage].[dbo].[Container].DELIVERYDATE BETWEEN '2016-10-06' AND '2016-10-08'
GROUP BY [fmsStage].[dbo].[File].[FILENUMBER]
Also, I have to do TOP 1 at the SELECT statement because it returns 51 rows with random numbers inside of them. (They are probably not random, but I can't figure out what they are.)
What do I have to do to make it just count the rows from [fmsStage].[dbo].[file].[FILENUMBER]?
First, your query would be much clearer like this:
SELECT COUNT(f.[FILENUMBER])
FROM [fmsStage].[dbo].[File] f INNER JOIN
[fmsStage].[dbo].[Container] c
ON v.[FILENUMBER] = c.[FILENUMBER]
WHERE f.[RELATIONCODE] = 'SHIP02' AND
c.DELIVERYDATE BETWEEN '2016-10-06' AND '2016-10-08';
No GROUP BY is necessary. Otherwise you'll just one row per file number, which doesn't seem as useful as the overall count.
Note: You might want COUNT(DISTINCT f.[FILENUMBER]). Your question doesn't provide enough information to make a judgement.
Just remove GROUP BY Clause
SELECT COUNT([fmsStage].[dbo].[File].[FILENUMBER])
FROM [fmsStage].[dbo].[File]
INNER JOIN [fmsStage].[dbo].[Container]
ON [fmsStage].[dbo].[File].[FILENUMBER] = [fmsStage].[dbo].[Container].[FILENUMBER]
WHERE [fmsStage].[dbo].[File].[RELATIONCODE] = 'SHIP02'
AND [fmsStage].[dbo].[Container].DELIVERYDATE BETWEEN '2016-10-06' AND '2016-10-08'

"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 grouping function

I am using an odbc connection to an as400, I know the connection string is proper and working as all my other queries work without any problem.
why is it that this sql statement works:
select
rdqty,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
order by
actlvl asc
but as soon as I try to sum a quanity and group the results as the following it fails.
select
sum(rdqty) as quanity,
alib.rcvshpdt.mbsnodesc,
rdauthor,
rdtitle,
rdediton,
ifnull(iecactno,actvty) as actlvl,
class,
ifnull(ieclp,lp) as cst
from
alib.rcvshpdt
left outer join
alib.bkmaster on alib.rcvshpdt.mbsnodesc=alib.bkmaster.mbsno
left outer join
alib.iecostda on alib.rcvshpdt.rcvno=alib.iecostda.rcvno
and alib.rcvshpdt.mbsnodesc = alib.iecostda.mbsnodesc
where
alib.rcvshpdt.rcvno='3930697'
group by
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, actlvl, class, cst
order by
actlvl asc
As far as I know, you can't group by an alias name. Try changing the Group clause to use the calculations directly like the following:
alib.rcvshpdt.mbsnodesc, rdauthor, rdtitle, rdediton, ifnull(iecactno,actvty), class, ifnull(ieclp,lp)