literal group works in Access 2010 but not SQl Server 2012 - sql

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

Related

How to use STDEV over() in SQL Server 2017 Express?

I am trying to get the STDEV of MCW_NM column but I want it to be STDEV of all rows not per group by BLADEID. But in Variance_Blade_MCW I need it to be grouped by BLADEID. I have tried over() but I get this error:
Column 'ENG.DBO.MCW_BCL_WEDGE.MCW_NM' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone help me? Below is my query.
PS: I am having difficulty explaining the problem so please bear with me. Let me know if you have clarifications! thanks a lot!
SELECT
BladeID,
Total_Sigma_MCW = STDEV(MCW_NM) OVER (),
CountD_Blade = COUNT(BLADEID) OVER (),
Variance_Blade_MCW = SQUARE(STDEV(MCW_NM))
FROM
ENG.DBO.MCW_BCL_WEDGE
WHERE
TESTDATE > GETDATE() - 6
GROUP BY
BLADEID
HAVING
COUNT(BladeID) >= 5000
I don't have access to mssql at the moment, but this might work. The inner query returns 1 row per BladeID with what I think are the aggregates you want. Problem is window functions always return 1 row for each row in the source, so the outer query flattens this.
SELECT DISTINCT
BladeID,
Total_Sigma_MCW = STDEV(MCW_NM) OVER (PARTITION BY 1),
Variance_Blade_MCW,
CountD_Blade,
FROM
(
SELECT
BladeID,
MCW_NM,
CountD_Blade = COUNT() OVER (PARTITION BY BladeID),
Variance_Blade_MCW = SQUARE(STDEV(MCW_NM) OVER (PARTITION BY BLADEID))
FROM
ENG.DBO.MCW_BCL_WEDGE
WHERE
TESTDATE > GETDATE() - 6
) q
WHERE CountD_Blade >= 5000
It may be more efficient to create two queries, one to group by BladeID and one over the full dataset and join them.

SQL Oracle: Trying to pull a count with AND operators, New and needs experienced eyes

I am new to SQL and have had pretty good luck figuring things out thus far but I am missing something in this query:
The question is how to return a distinct count from two columns using another column and the criteria if the value is greater than 0.
I have tried IF and AND operators (My current query returns a 0 not an error, and it works when only using one .shp criteria)
select count (distinct ti.TO_ADDRESS)
from ti
where ti.input_id = 'xxx_029_01z_c_zzzzbab_ecrm.shp'
and ti.input_id = 'xxx_030_01z_c_zzzzbab_ecrm.shp'
and ti.OPENED>0;
Thanks so much!!
I think you want two levels of aggregation:
select count(*)
from (select ti.TO_ADDRESS
from ti
where ti.input_id in ('xxx_029_01z_c_zzzzbab_ecrm.shp', 'xxx_030_01z_c_zzzzbab_ecrm.shp') and
ti.OPENED > 0
group by ti.TO_ADDRESS
having count(distinct ti.input_id) = 2 -- has both of them
) ti;

Sql -after group by I need to take rows with newest date

I need to write a query in sql and I can't do it correctly. I have a table with 7 columns 1st_num, 2nd_num, 3rd_num, opening_Date, Amount, code, cancel_Flag.
For every 1st_num, 2nd_num, 3rd_num I want to take only the record with the min (cancel_flag), and if there's more then 1 row so take the the newest opening Date.
But when I do group by and choose min and max for the relevant fields, I get a mix of the rows, for example:
1. 12,130,45678,2015-01-01,2005,333,0
2. 12,130,45678,2015-01-09,105,313,0
The result will be
:12,130,45678,2015-01-09,2005,333,0
and that mixes the rows into one
Microsoft sql server 2008 . using ssis by visual studio 2008
my code is :
SELECT
1st_num,
2nd_num,
3rd_num,
MAX(opening_date),
MAX (Amount),
code,
MIN(cancel_flag)
FROM do. tablename
GROUP BY
1st_num,
2nd_num,
3rd_num,
code
HAVING COUNT(*) > 1
How do I take the row with the max date or.min cancel flag as it is without mixing values?
I can't really post my code because of security reasons but I'm sure you can help.
thank you,
Oren
It is very difficult like this to answer, because every DBMS has different syntax.
Anyways, for most dbms this should work. Using row_number() function to rank the rows, and take only the first one by our definition (all your conditions):
SELECT * FROM (
SELECT t.*,
ROW_NUMBER() OVER ( PARTITION BY t.1st_num,t.2nd_num,t.3rd_num order by t.cancel_flag asc,t.opening_date desc) as row_num
FROM YourTable t ) as tableTempName
WHERE row_num = 1
Use NOT EXISTS to return a row as long as no other row with same 1st_num, 2nd_num, 3rd_num has a lower cancel_flag value, or same cancel_flag but a higher opening_Date.
select *
from tablename t1
where not exists (select 1 from tablename t2
where t2.1st_num = t1.1st_num
and t2.2nd_num = t1.2nd_num
and t2.3rd_num = t1.3rd_num
and (t2.cancel_flag < t1.cancel_flag
or (t2.cancel_flag = t1.cancel_flag and
t2.opening_Date > t1.opening_Date)))
Core ANSI SQL-99, expected to work with (almost) any dbms.

SQL aggregate function

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

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"));