I am trying to include a selection for the MAX date range in my where clause. I am using Having to bring it in, but it errors out saying that "Column 'dbo.PURCHLINE.PURCHID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." But it won't let me group, throwing other errors. Maybe i'm approaching this wrong. Essentially, I need to get the latest date in in a set of columns for this query:
Select
PURCHLINE.PURCHID as 'PO'
,SUBSTRING(Cast(Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) as varChar), 0, 12) as 'DeliveryDate'
,PURCHLINE.QTYORDERED as 'QtyOrdered'
FROM dbo.PURCHLINE
INNER JOIN dbo.INVENTTABLE
ON dbo.PURCHLINE.ITEMID = dbo.INVENTTABLE.ITEMID
INNER JOIN dbo.INVENTITEMGROUP
ON dbo.INVENTTABLE.ITEMGROUPID = dbo.INVENTITEMGROUP.ITEMGROUPID
INNER JOIN dbo.INVENTPOSTING
ON dbo.INVENTITEMGROUP.ITEMGROUPID = ITEMRELATION
INNER JOIN dbo.PURCHTABLE
ON dbo.PURCHLINE.PURCHID = dbo.PURCHTABLE.PURCHID
INNER JOIN dbo.LEDGERTABLE
ON LEDGERACCOUNTID = ACCOUNTNUM
INNER JOIN VENDPACKINGSLIPJOUR
on VENDPACKINGSLIPJOUR.PURCHID = PURCHLINE.PURCHID
WHERE
dbo.PURCHLINE.PURCHSTATUS = 3 --open Order
AND dbo.PURCHLINE.PURCHASETYPE = 3 --Purchase Order
AND dbo.PURCHLINE.DIMENSION IN (#department)
AND INVENTACCOUNTTYPE = 0 --Purchase Packing Slip
AND PURCHLINE.DIMENSION2_ = #division
HAVING Max(VENDPACKINGSLIPJOUR.DELIVERYDATE) between #start and #end
You have an aggregate, so you'll need to group on the nonaggregate fields:
GROUP BY
PURCHLINE.PURCHID
,PURCHLINE.QTYORDERED
This goes before the HAVING clause.
Related
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
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
Here is my SQL Server query. When I execute this query, Group by was giving me a error
Msg 8120, Level 16, State 1, Line 1
Column 'master_order.order_id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
My tables in relation but he did not find why if cannot use group by its work
SELECT
master_order.order_id, master_order.order_no, article_order.article_name,
size.size_name, transaction_order.quantity, transaction_order.unit_name,
transaction_order.rate, transaction_order.amount, transaction_order.discount,
transaction_order.net_amount, master_order.order_date, buyer.buyer_name,
master_order.shipment_date, bank.bank_name, bank.bank_branch, bank.account_title,
master_order.confirmation_date, payment.payment_terms, agent.agent_name,
agent.company_name, currency.currency_symbol, master_order.half_day,
master_order.half_date, master_order.shipped_board_date, master_order.port_category,
port_info_setup.port_name, port_info_setup.country_name, master_order.revised,
master_order.confirmed, master_order.ex_factory, master_order.comments,
master_order.dis_type
FROM
master_order
INNER JOIN
transaction_order ON master_order.order_id = transaction_order.order_id
INNER JOIN
size ON transaction_order.size_id = size.size_id
INNER JOIN
article_order ON transaction_order.article_id = article_order.article_id
INNER JOIN
buyer ON master_order.buyer_id = buyer.buyer_id
INNER JOIN
port_info_setup ON master_order.port_id = port_info_setup.port_id
INNER JOIN
payment ON master_order.payment_id = payment.payment_id
INNER JOIN
currency ON master_order.currency_id = currency.currency_id
INNER JOIN
bank ON master_order.bank_id = bank.bank_id AND buyer.buyer_id = bank.buyer_id
INNER JOIN
agent ON master_order.agent_id = agent.agent_id
GROUP BY
size.size_name
In the grouped query ether your columns in the select list should be inside an aggregate function or the column without aggregate should mention in the group by statement.
If you want a column with no aggregate and dont want grouping by that column, so use CTE for grouping and use join to have that column in your query results.
Still trying to get used to writing queries and I've ran into a problem.
Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)
The inner query gives an ID number in one column, the amount of times they appear in the table, and then a bit attribute if they are in region A. The outer query works but the error I get is incorrect syntax near the keyword IN. Of the inner query, I would like a number of how many of them are in region A
You must specify table name in query before where
Select count(region)
from table
where (regionTable.A=1) in
And you must choose one of them.
where regionTable.A = 1
or
where regionTable.A in (..)
Your query has several syntax errors. Based on your comments, I think there is no need for a subquery and you want this:
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A
which can be further simplified to:
select jxn.id, count(jxn.id) as counts
, 1 as A --- you can even omit this line
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
You are getting the error because of this line:
where (regionTable.A=1)
You cannot specify a condition in a where in clause, it should only be column name
Something like this may be what you want:
SELECT COUNT(*)
FROM
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from
jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
) sq
WHERE sq.a = 1
I dont know why this is coming up as invalid and I can not figure it out. I was given a legacy database as my supervisor left and I am in charge until someone comes to replace him. I am trying to run this query...
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH INNER JOIN
tblM ON tblCH.guidMId = tblM.guidMId INNER JOIN
ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
The error I keep getting is...Msg 8120, Level 16, State 1, Line 1
Column 'tblM.guidRId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Why is this error occuring?
You are forgetting to group guidRId. (you are aggregating the data)
SELECT
tblM.guidRId,
SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM ON tblM.strNumber = ViewLM.strNumber
WHERE
tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId
Because you need a GROUP BY if you are going to use an aggregate functon like SUM() [or COUNT, AVG(), etc...] with another non-aggregate column:
SELECT tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26'
GROUP BY tblM.guidRId;
Try:
SELECT
tblM.guidRId, SUM(dbo.tblCH.curTotalCost) AS curValue
FROM
tblCH
INNER JOIN tblM
ON tblCH.guidMId = tblM.guidMId
INNER JOIN ViewLM
ON tblM.strNumber = ViewLM.strNumber
WHERE (tblM.guidRId = '4d832bc8-1827-4054-9896-6111844b0f26')
GROUP BY tblM.guidRId