iis invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause - sql

I'm currently trying to add another table in here "dbo.ARTICLE_INCARNATION.article_incarnation_id", but I keep getting the error:
iis invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I have tried what i know to fix it but no progress.
SELECT
'Summary',SUM( OBJ#CAB.object_qty) AS 'Sum est, OBW exist',
AIN.article_incarnation_long_no AS 'Article no',
AIN.article_incarnation_name AS 'Article name',
SUM( OBJ#CAB.object_qty) AS 'Sum est, OBW exist',
(SELECT
isnull(sum(POI.purchase_order_item_qty_po),0)
FROM
[dbo].[PURCHASE_ORDER_ITEM] POI
WHERE
POI.article_incarnation_id = AIN.article_incarnation_id
AND POI.purchase_order_id IS NOT NULL),
(SELECT sum(isnull(stock_object_qty_qty,0)) FROM [dbo].[STOCK_OBJECT_QTY] S WHERE S.article_incarnation_id = OBJ#CAB.article_incarnation_id) AS 'Total on stock'
/*AS 'Manko'
AS 'PO',
AS 'Not deliverd PO'*/
--skalmed
FROM
OBJECT OBJ#CAB
JOIN OBJECT_TYPE OTY#CAB ON OTY#CAB.object_type_id = OBJ#CAB.object_type_id
AND OTY#CAB.object_type_no = 'CABLE'
JOIN [dbo].[ARTICLE_INCARNATION] AIN ON OBJ#CAB.article_incarnation_id = AIN.article_incarnation_id
AND AIN.article_unit_no = 'M'
JOIN OBJECT_WORK OBW#CAB ON OBW#CAB.object_id = OBJ#CAB.object_id
AND OBW#CAB.work_type_no IN ('EPU', 'EPA', 'IPA', 'XEPU', 'LI5', 'LE5')
AND OBW#CAB.object_work_installed_qty IS NULL
GROUP BY
AIN.article_incarnation_long_no,
AIN.article_incarnation_name,
ARTICLE_INCARNATION.article_incarnation_id,
OBJ#CAB.article_incarnation_id

You have same table name but schemas are different that cause the problem. :
ARTICLE_INCARNATION.article_incarnation_id
AND
[dbo].[ARTICLE_INCARNATION]
Choose which one you want to aggregate or display. If you want both then define proper alias.

Related

Azure Data Factory - Data Flow Join Conditions Substitution

Is it possible to implement the following SQL script via Join in Data Flow?
SELECT DISTINCT
[landing].[ACODETEMP].[objectid] AS ActObjID,
MIN(CASE [landing].[ACODETEMP].[name] WHEN 'Activity Priority' THEN [landing].[ACTIVITYCODE].
[codevalue] END) AS [Activity Priority],
MIN(CASE [landing].[ACODETEMP].[name] WHEN 'Area' THEN [landing].[ACTIVITYCODE].[codevalue] END) AS Area,
...
FROM
[landing].[ACODETEMP]
LEFT OUTER JOIN [landing].[ACTIVITYCODE] ON [landing].[ACODETEMP].[name] = [landing].[ACTIVITYCODE].[codetypename]
AND [landing].[ACODETEMP].[activitycodeobjectid] = [landing].[ACTIVITYCODE].[objected]
AND [landing].[ACODETEMP].[name] IN ('Activity Priority', 'Area', '...')
GROUP BY [landing].[ACODETEMP].[objected]
I don't know how to implement the substitution like: IN ('Activity Priority', 'Area', '...')
I haven't looked but if they didn't make an "in" available then do an series of "==" and "or"'s within brace brackets
example (name == 'Activity Priority' || name == 'Area' || ... )
It is a lot of typing but will work.
You can save the 'Activity Priority', 'Area', '...' to another file such csv file. Then set it as a data source and join it.
For example:
I saved John,Jane,Eloise into a txt or csv, as follows:
Set it as a data source named FilterSource in data flow, as follows:
This is the left outer join:
After the left outer join, you can set a right outer join to FilterSource:
Then it will filter the names, it has the same effect as in (John,Jane,Eloise).
You can always click "Custom (cross) join" and type in your expression there

concatenate 2 columns from different tables

Using the below query i am trying to get the db_unique_name from v$database combined with the PRIVILEGE column of DBA_PRIV_AUDIT_OPTS.But i am getting the error 'ORA-00918:column ambiguously defined'.Please suggest.Thanks.
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE from
(SELECT d.PRIVILEGE,d.PROXY_NAME,d.PRIVILEGE,d.SUCCESS,d.FAILURE from DBA_PRIV_AUDIT_OPTS d
where d.PRIVILEGE in ('AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM','GRANT ANY ROLE','GRANT ANY PRIVILEGE','GRANT ANY OBJECT PRIVILEGE','CREATE USER','ALTER USER','DROP USER')),v$database;
You specified d.privilege twice, so just removed extra one:
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE from
(SELECT d.PRIVILEGE,d.PROXY_NAMEd.SUCCESS,d.FAILURE from DBA_PRIV_AUDIT_OPTS d
where d.PRIVILEGE in ('AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM','GRANT ANY ROLE','GRANT ANY PRIVILEGE','GRANT ANY OBJECT PRIVILEGE','CREATE USER','ALTER USER','DROP USER')),v$database;
Also why not simply without subquery?
select db_unique_name||':'||PRIVILEGE DB_NAME_PRIV,PROXY_NAME,PRIVILEGE,SUCCESS,FAILURE
from DBA_PRIV_AUDIT_OPTS d, v$database
where d.PRIVILEGE in (
'AUDIT SYSTEM','AUDIT ANY','ALTER SYSTEM'
,'GRANT ANY ROLE','GRANT ANY PRIVILEGE'
,'GRANT ANY OBJECT PRIVILEGE','CREATE USER'
,'ALTER USER','DROP USER'
);

Case statement handling logic differently than expected

I'm trying to assign a status based on the number of IDs using a metric. This is the query I've written (and it works):
select
x.yyyy_mm_dd,
x.prov_id,
x.app,
x.metric,
x.is_100,
case
when ((x.is_100 = 'true') or size(collect_set(x.list)) >10) then 'implemented'
when ((x.is_100 = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'
else 'no contact'
end as impl_status,
size(collect_set(x.list)) as array_size,
collect_set(x.list) as list
from(
select
yyyy_mm_dd,
prov_id,
app,
metric,
is_100,
list
from
my_table
lateral view explode(ids) e as list
) x
group by
1,2,3,4,5
However, the impl_status is incorrect for the second condition in the case statement. In the result set, I can see rows with is_100 = false, array_size between 1 and 10, however the impl_status ends up being 'no contact' instead of 'first contact'. I was thinking maybe between isn't inclusive but it seems to be according to the docs.
I am curious if this works:
(case when x.is_100 or count(distinct x.list) > 10
then 'implemented'
when (not x.is_100) and count(x.list) > 0
then 'first contact'
else 'no contact'
end) as impl_status,
This should be the same logic without the string comparisons -- here is an interesting viewpoint on booleans in Hive. I also think that COUNT() is clearer than the array functionality.
Be sure you have not some hidden space in the string
when (( trim(x.is_100) = 'false') and size(collect_set(x.list)) between 1 and 10) then 'first contact'

SQL Server 2008 view error with case statement

I cant figure out why this code executes as a query but when I execute it in a view it throws a the multi-part identifier could not be bound, error.
When I take out the case statement It works in the view, so I believe it is something that has to do with the case statement.
Any suggestions are appreciated.
WITH [cteFrostSum] AS
(
SELECT ID AS ID, theMonth as Mo,
SUM(dbo.Frost.[DRAmount]) AS [DRAmount]
FROM dbo.Frost
GROUP BY [ID], theMonth
)
SELECT DISTINCT
TOP (100) PERCENT
dbo.ternean.MemberID,
dbo.ternean.SSN,
dbo.ternean.GroupName,
dbo.ternean.CustomerID,
dbo.ternean.GroupNumber,
dbo.ternean.LastName,
dbo.Frost.DRAmount,
dbo.Frost.HittheBank,
dbo.Frost.MonthofPremium,
cte.[DRAmount] AS [SUM_Frost_Balance],
dbo.ternean.TotalCost,
cte.[DRAmount] - dbo.ternean.TotalCost AS Diff,
dbo.ternean.ACH_RoutingNo,
dbo.Frost.RTNum,
dbo.ternean.ACH_AcctNo,
dbo.Frost.AccountNumber,
CASE
WHEN dbo.Frost.RTNum <> SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'DO not match'
WHEN dbo.Frost.RTNum = SUBSTRING(dbo.ternean.ACH_RoutingNo, 2, 20)
THEN 'match'
END AS [Routing # match],
CASE
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) <> dbo.Frost.AccountNumber
THEN 'DO not match'
WHEN SUBSTRING(dbo.ternean.ACH_AcctNo, 2, 20) = dbo.Frost.AccountNumber
THEN 'match'
END AS [Account # match],
dbo.Frost.theMonth
FROM dbo.Frost
INNER JOIN dbo.ternean ON dbo.Frost.ID = dbo.ternean.CustomerID
AND dbo.Frost.theMonth = dbo.ternean.theMonth
INNER JOIN [cteFrostSum] cte ON dbo.Frost.ID = cte.ID
AND dbo.Frost.theMonth = cte.Mo
ORDER BY dbo.ternean.theMonth
I tried to replicate your error but couldn't.
Why are you using multi-part identifiers the field names anyway? The list of fields in the select statement can only refer to the tables in the from clause, at first reading this query seems to be referring to the tables directly in the dbo schema.
Give your tables some nice easy aliases i.e.
FROM dbo.Frost AS F
and use them like this
F.RTNum
Secondly you can simplify your case statements and only do one test i.e.:
CASE WHEN SUBSTRING(T.ACH_AcctNo, 2, 20) <> F.AccountNumber
THEN 'DO not match'
ELSE 'match'
END AS [Account # match]

SQL Server 2005 Issue Column name or number of supplied values does not match table definition

I wish to DELETE the data from a table before performing an INSERT INTO, however I keep recieving an error stating:
Insert Error: Column name or number of supplied values does not match table definition.
I've also tried defining the columns the data should be entered into as part of the INSERT INTO statement, but then get issues with column names, even though they are correct. I have a feeling the issues relates to me selecting 2 PostCode entries and converting them into 1, but if someone could shed light on this it would be a big help.
My code can be found below, if you want me to add the code where I was sepcifing column names let me know. So you know the fields selected are all the fields in the Course table other than AutoNum which is a auto number primary key and SSMA_TimeStamp, which is a TimeStamp.
BEGIN
DELETE dbo.Course
INSERT INTO dbo.Course
SELECT
RTRIM( CAST (sd.[RefNo] AS nvarchar(50))) AS 'Student Ref No',
sd.[FirstForeName] AS Forename,
sd.[Surname],
sd.[Address1],
sd.[Address2],
sd.[Address3],
sd.[Address4],
sd.[DateOfBirth] AS DOB,
sd.[PostCodeOut] + ' ' + sd.[PostCodeIn] AS 'Post Code',
o.[Name] AS 'Course Name',
o.[Code] As 'Course Code',
e.[StartDate] AS 'Start Date',
e.[ExpectedGLH] AS 'Exp GLH',
e.[ExpectedEndDate] AS 'Expected End Date',
e.[ActualEndDate] AS 'Actual End Date',
e.[Grade] AS 'Grade',
ou.[Description] AS Outcome,
cs.[Description] AS 'Completion Status',
sd.[Tel1] AS 'Tel 1'
FROM [xxxxxxx].[xxxxxx].[dbo].[StudentDetail] sd
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[Enrolment] e
ON sd.[StudentDetailID] = e.[StudentDetailID]
Inner JOIN [xxxxxxx].[xxxxxx].[dbo].[Offering] o
ON o.[OfferingID] = e.[OfferingID]
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[CompletionStatus] cs
ON cs.[CompletionStatusID] = e.[CompletionStatusID]
INNER JOIN [xxxxxxx].[xxxxxx].[dbo].[Outcome] ou
ON ou.[OutcomeID] = e.[OutcomeID]
WHERE sd.[AcademicYearID] = '09/10'
AND
o.[Code] LIKE '%-ee%'
AND
o.[Name] LIKE '%-%dl%'
ORDER BY
sd.[RefNo]
It sounds like your 'Course' table does not match your insert statement, either in the number or names of the columns specified (as per the error message).
Could you add the create table code for the 'Course' table as that will show where the discrepancy lies.
Thanks.
I would explicitly list the columns in the course table that you are inserting into - this may solve your problem/help find your issue, but also reduce maintenance problems in the future.
To fix this issue you need explicitly specify list of the table's columns in the INSERT INTO statement.
you should add a list of columns to the INSERT statement, see below, where you explicitly list each column from dbo.Course that you intend to populate in your INSERT:
INSERT INTO dbo.Course
---<<<<<
(col1, col2, col3, col4, clo5....) ---<<<<<Add this here
---<<<<<
SELECT
RTRIM( CAST (sd.[RefNo] AS nvarchar(50))) AS 'Student Ref No',
sd.[FirstForeName] AS Forename,
sd.[Surname],
sd.[Address1],
sd.[Address2],
sd.[Address3],
sd.[Address4],
sd.[DateOfBirth] AS DOB,
sd.[PostCodeOut] + ' ' + sd.[PostCodeIn] AS 'Post Code',
o.[Name] AS 'Course Name',
o.[Code] As 'Course Code',
e.[StartDate] AS 'Start Date',
e.[ExpectedGLH] AS 'Exp GLH',
e.[ExpectedEndDate] AS 'Expected End Date',
e.[ActualEndDate] AS 'Actual End Date',
e.[Grade] AS 'Grade',
ou.[Description] AS Outcome,
cs.[Description] AS 'Completion Status',
sd.[Tel1] AS 'Tel 1'
FROM ....
then make sure that each column in the SELECT list matches each of these columns and in order. From your error, it sounds like you have too many any or too few returned columns in the SELECT.