SQL aggregate function - sql

I want to run a query in which I have to count no of itemsModel in the database which is in MS Access 2007 and also to multiply that count with the rate with i have kept in the different table.
SELECT
AllocateAsset.Item, AllocateAsset.ItemModel,
COUNT(AllocateAsset.ItemModel) AS CountOfItem,
(COUNT(AllocateAsset.ItemModel) * rateList.Rate) AS Amount
I'm getting an error that:
it is not right expression ...

you need to add a group by statement:
group by AllocateAssettItem, AllocateAsset.ItemModel

Related

How to select the max value of the last 6 digits of the entries in the column in SQL

I am trying to select the maximum value of the last 6 digits in a list of strings
This is for creating an Inbox Query in Infor EAM
OBJ_CODE is the column and R5OBJECTS is the table. I have tried the following code but the number returned is 0.
SELECT MAX(RIGHT(OBJ_CODE,6)) FROM R5OBJECTS
My list looks like this
AAAA100000
AAAA100001
AAAA100002
AAAA100003
AAAA100004
AAAA100005
...
AAAA100999
...
BBBB100006
BBBB100007
BBBB100008
BBBB100009
BBBB100010
So the expected output would be 100999
It seems this table R5OBJECTS is too big, and your sql query performance didn't pass the base configuration parameter.
If using Inbox -> Set your INBXSCOR to 50 and try your query again.
If using KPI -> Set your KPISCOR to 50
SQL Statement
Enter the SQL statement to calculate the number of applicable records
for the inbox entry. The system automatically populates SQL Statement
Text. Note: SQL Statement cannot exceed the performance score limit
defined in the INBXSCOR installation parameter.
https://docs.infor.com/eam/11.3.2/en-us/eamolh/cdh1498150395934.html
Although this code works perfectly form me in SQL Server 2016, I add additional function to convert string to int to be sure:
SELECT MAX(CONVERT(INT,RIGHT(OBJ_CODE,6))) FROM R5OBJECTS

MS Access - Extract single value for calculation

This an MS Access related question.
I get the Collateral divided 50 times because I have 50 rows in my ExchangeRates table... however the SELECT statement is supposed to only extract the value associated to CurrencyCode="EUR". How can I change the below statement to have the division being applied once only?
SELECT tbl_A.Security, tbl_A.Typ, Sum(([Collateral]/(SELECT tblExchangeRates.RateToUSD
FROM tblExchangeRates
WHERE (((tblExchangeRates.CurrencyCode)="EUR"))))) AS CollateralUSD
FROM tbl_A, tblExchangeRates
GROUP BY tbl_A.Security, tbl_A.Typ
HAVING (((tbl_A.Typ)="PR"));
It looks like this is what I was willing to get, just an Alias. SQL gurus, you are welcome to review.
SELECT tbl_A.Security, Sum(([Collateral]/[RateToUSD])) AS CollateralUSD
FROM tbl_A, (SELECT RateToUSD
FROM tblExchangeRates
WHERE CurrencyCode = 'EUR') AS MyAliasQ
GROUP BY tbl_A.Security
HAVING (((tbl_A.Typ)="PR"));

literal group works in Access 2010 but not SQl Server 2012

Hi the following query works in access 2010 but not SQL Server 2012. The error I am getting says 'Each GROUP BY expression must contain at least one column that is not an outer reference'. From my research it is because the query groups by a literal.
How can I achieve the same result in SQL Server
SELECT
1 AS ID, Sum([Meds].TotalMeds) AS TotalMeds,
Sum([Meds].Presc) AS Presc
INTO Stats_Meds
FROM
[Meds], Rounds
WHERE
((([Meds].Round)<[Rounds].[Round]))
GROUP BY
1;
thanks
Why do you even need a group by? Why not just use:
SELECT Sum(Meds.TotalMeds) AS TotalMeds, Sum(Meds.Presc) AS Presc
INTO Stats_Meds
FROM Meds, Rounds
WHERE Meds.Round < Rounds.Round
I am guessing you need the Id to inset into the Stats table. See if this query works for you.
DISCLAIMER(for down-voters): I have not tested this query on performance or actually run the query just a suggestion that might work for OP.
SELECT ID, SUM (WithId.TotalMeds), SUM(WithId.Presc)
INTO Stats_Meds
FROM
(SELECT 1 AS ID, Meds.TotalMeds, Rounds.Presc
FROM Meds, Rounds WHERE Meds.Round < Rounds.Round) WithId GROUP BY ID
OR you could just do -
SELECT 1, Sum(Meds.TotalMeds) AS TotalMeds, Sum(Meds.Presc) AS Presc
INTO Stats_Meds
FROM Meds, Rounds
WHERE Meds.Round < Rounds.Round

Max function with where clause

I am using MS access 2007 and this is my original SQL query:
SELECT Rates.UNIT, Rates.PROJECT, Rates.[Date_of_CM Memo], Rates.Rates, Rates.REMARKS
FROM Rates, Checks
WHERE (((Rates.UNIT)=[Checks].[Unit_Name]) AND ((Rates.[Date_of_CM Memo])<[Checks].[Reg_Date]));
but when I modified this query (below) to display the records for the latest date I get an error saying that I cannot use the max function with the where clause:
SELECT Rates.UNIT, Rates.PROJECT, Rates.[Date_of_CM Memo], Rates.Rates, Rates.REMARKS
FROM Rates, Checks
WHERE (((Rates.UNIT)=[Checks].[Unit_Name]) AND (MAX((Rates.[Date_of_CM Memo])<[Checks].[Reg_Date])));
You can't just put MAX (or any other aggregate function) into WHERE,
but you can put a query like that:
SELECT Rates.Unit,
Rates.Project,
Rates.[Date_of_CM Memo],
Rates.Rates,
Rates.Remarks
FROM Rates,
Checks
WHERE (((Rates.Unit) = [Checks].[Unit_Name]) AND
((SELECT MAX (Rates.[Date_of_CM Memo])
FROM Rates) < [Checks].[Reg_Date])));

Sum of distinct values in field SSRS 2005

I'm working on SSRS report builder that is using a dataset calling a SQL Server 2000 database.
The query is getting sums of a few different fields and is also pulling out all records that have to do with that client number. I want to get the sum of the sum but it is way over because of the detail rows. Basically what I want is the sum of the distinct sum column values.
=Sum(Fields!tot.Value, "table1_Group3")
I saw that you can get sums by the groups and I tried the expression above but it comes back with an error:
The Value expression for the textbox 'tot' has a scope parameter that is not
valid for an aggregate function...
table1_Group3 is the name of the group that holds the sum value in the report.
Any suggestions on how to get the distinct values to sum them in this report.
=Sum(Fields!tot.Value, "table1_Group3")
The code above will give you the sum of "tot" for all rows in the current "table1_Group3." This means that this expression only makes sense somewhere within table1_Group3. Otherwise, SSRS doesn't know which is the current instance of that group.
Sounds like you would like to sum this value across multiple groups, but only take one "tot" from each instance of the group. (Are you sure that all rows in that group will have the same "Tot?")
If tot is the total of other fields in your returned data, then simply add those up in your formula. This may have the added benefit of simplifying your SQL query as well.
Some other options that could work:
- Change your SQL query so that only one row per group gets the Tot field set.
- Use Embedded code in the report to keep a running total which is added to only once per group, such as in the group header.
(If upgrading to 2008R2 SSRS is an option, then the Lookup function could be used here, maybe even to look back at the same dataset.)
change the query/ dataset to sum(distinct tot) using the temp table on the sql server
I suppose you need to write sum(distinct columnName).