Lookup in other table & get the data - sql

Need your help in getting the below solved.
I have data in tables
1. Extract entire dump
2. Product table
i need a view where in All the dump to display & another column looking up with the Product SKU in product table with and display as eligible. if not not eligible.
Select CompanyLocationId, ProductSku
From ETL_Extract,
Product_Eligibility_List
where ETL_Extract.ProductSku = Product_Eligibility_List.ProductSku
Msg 209, Level 16, State 1, Line 1 Ambiguous column name 'ProductSku'.

Alias your objects, qualify your columns and stop using syntax that has been outdated for 27 years (Bad habits to kick : using old-style JOINs). The error, however, is telling you the problem; ProductSku is ambigious as it is in both the tables ETL_Extract and Product_Eligibility_List. As a result SQL Server doesn't know which you are trying to reference in your SELECT (even if they do have the same value due to the ON clause).
Once you do all the things I initially listed, you get a query that looks like this that should provide you with the dataset you're after:
SELECT E.CompanyLocationId, --Guessed Alias prefix
PEL.ProductSku
FROM ETL_Extract E
INNER JOIN Product_Eligibility_List PEL ON E.ProductSku = PEL.ProductSku;

Related

Access 16 append query - Syntax error (missing operator) in query expression WHERE clause

I am still not too experienced with access, SQL, VBA and this is my first post. So please go easy on me! Okay lets get to it. I have been updating an access file due to irrelevant and duplicate records showing up on reports.
First I was having trouble appending records due to Key violations that I determined was due to appending to primary key fields, which I remedied by creating an autonumber ID Primary key on the destination table. I am having trouble updating the query with a WHERE clause to not add records already on the destination table. Field and table names have been changed below, but I am trying to insert into "dandy" from "bag" and "rug".
Error message reads:
Syntax error (missing operator) in query expression '"NOOB"
WHERE [rug].[TENT] AND [rug].[CANDY] NOT IN (SELECT [TENT] AND [CANDY] FROM [dandy])'.
See SQL Code below:
INSERT INTO dandy(CANDY, FUN, DOG, TREAT, BUMPY, TENT, System )
SELECT Val([CANDY]) AS Burger,
Val([FUN]) AS FUN_VAL,
rug.STOVE,
rug.TREAT,
IIf([Bag]![Water] Is Not Null,[Bag]![Water],[rug]![BUMPY]) AS BUMPY,
rug.TENT,
"NOOB" AS System
FROM rug
INNER JOIN [crib] ON rug.CANDY= [crib].Dip)
INNER JOIN Bag ON ([crib].FacNum = Bag.[Trip]) AND (rug.TENT = Bag.Lip)
GROUP BY
Val([CANDY]),
Val([FUN]),
[rug].STOVE,
[rug].TREAT,
IIf([Bag]![Water] Is Not Null,
[Bag]![Water]
[rug]![BUMPY]),
rug.TENT,
"NOOB"
WHERE [rug].[TENT] AND [rug].[CANDY] NOT IN (SELECT [TENT] AND [CANDY] FROM [dandy])
HAVING (((IIf([Bag]![Water] Is Not Null,
[Bag]![Water],
[rug]![BUMPY])) Is Null));
If you want to prevent duplicate pairs of Tent and Candy in table, just set a compound Index in table with those two fields. Records that would cause duplication will be rejected. The IN() criteria can be eliminated.
As for the IN() criteria, I tested your original syntax. It does not error but also does not retrieve records. Splitting into two NOT IN() expressions also failed - no records met the AND condition.
WHERE TENT NOT IN (SELECT TENT FROM dandy) AND CANDY NOT IN (SELECT CANDY FROM dandy)
However, concatenation did retrieve correct records.
SELECT * FROM datasource
WHERE TENT & CANDY NOT IN (SELECT TENT & CANDY FROM dandy)

Using dot notation with sum() to query the same, but multiple columns, in multiple databases

SQLite
There are multiple databases, one database for each time period (i.e. quarter). The column headers in each table are the same. Some of the columns. The data is identical between databases (e.g. ID, Name, Address, State, Website, etc). Some of the columns, the column header is the same but the
data in the column is different between databases.
The goal is to:
Select multiple columns from multiple databases, sum each column, convert the output from 000000000 to $000,000,000,000, adding three zero's to the output
(currently the data is represented in 000's).
Following is an iteration of queries that work, ending in the queries that fail.
Selecting one column from one database. This query works.
select dep
From AllReports19921231AssetsAndLiabilities;
output
"11005"
"34396"
"42244"
Adding a sum(columnName) method to this same query works.
select sum(dep)
From AllReports19921231AssetsAndLiabilities;
results: 3562807353
Attempting to sum(columnName) from multiple databases causes an error.
select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
error:
ambiguous column name: dep: select sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities;
Using dot notation to attach a database to a column. Query works.
select AllReports19921231AssetsAndLiabilities.dep
From AllReports19921231AssetsAndLiabilities;
Output:
"11005"
"34396"
"42244"
However when I attempt to include dot notation and add sum(columnName) to the query, it fails.
select AllReports19921231AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities;
I receive this error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
What are correct ways to write this query?
The end goal is to select the same columns (e.g. col1, col2, col3, etc) from multiple databases (Q1, Q2, Q3, Q4).
Sum each column, add three zero's the output, then convert from 000000000 to $000,000,000,000
Note: There are 103 databases (i.e. one for each time period/quarter).
select AllReports19921231AssetsAndLiabilities.sum(dep),
AllReports19930331AssetsAndLiabilities.sum(dep),
AllReports19930630AssetsAndLiabilities.sum(dep)
From AllReports19921231AssetsAndLiabilities,
AllReports19930331AssetsAndLiabilities,
AllReports19930630AssetsAndLiabilities;
The above query outputs an error:
near "(": syntax error: select AllReports19921231AssetsAndLiabilities.sum(
Your syntax is wrong :
select sum(AllReports19921231AssetsAndLiabilities.dep)
From AllReports19921231AssetsAndLiabilities
Learn to use aliases!
select sum(aal.dep)
From AllReports19921231AssetsAndLiabilities aal;
The query is much easier to write and to read. The table alias (whether the full table name or an abbreviation) is attached to the column name. In SQL, this results in a qualified column reference. The qualification specifies what table it is coming from.
The table alias is not attached to a function, because SQL does not currently allow tables to contain functions.

Summing a Column with Multiple Data Types in SQL

I'm fairly new to SQL and am trying to write a query in sql server to get sums of documents week. My query looks like this:
SELECT
[table].[week],
SUM(ISNULL([table].[documents],0))
FROM
[table]
JOIN
(VALUES (15,187293),...other pairs...,(127,120918)) AS Outside ([ID],[Organization]) ON Outside.[ID]=[table].[ID]
AND Outside.[Organization] = [table].[Organization]
GROUP BY
week
This same query worked on a different table (formatted exactly the same) outputting documents per week, but when I run it on this table, I get the error
Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '#N/A' to data type int.
There is nothing in the documents column that looks like #N/A. How can I make this sum appropriately?
To find the offending rows, run the query:
select t.*
from table t
where id = '#N/A' or organization = '#N/A';
Or better yet:
select t.*
from table t
where try_convert(int, id) is null or try_convert(int, organization) is null;
You can fix the problem by using appropriate types in the outside set of values. That is, one or both of the values should be in single quotes so they are strings rather than numbers.

Ms Access Error - The field is too small to accept the amount of data you attempted to add

I am trying to build a select query in ms access, however am getting the error message: "The field is too small to accept the amount of data you attempted to add."
The sql is :
SELECT ProjectList.ProjectID, UpcomingOpenMilestone.MinOfPlannedDate,
UpcomingOpenMilestone.FirstOfMilestone
FROM UpcomingOpenMilestone RIGHT JOIN ProjectList ON
UpcomingOpenMilestone.ProjectID = ProjectList.ProjectID;
The query is pulling from one other query and a table. The ProjectList.ProjectID field is an autonumber field.
The UpcomingOpenMilestone qry is generated from one table and the sql is as follows:
SELECT MilestoneTracking.ProjectID, Min(MilestoneTracking.PlannedDate) AS
MinOfPlannedDate, First(MilestoneTracking.Milestone) AS FirstOfMilestone
FROM MilestoneTracking
GROUP BY MilestoneTracking.ProjectID, MilestoneTracking.ActualDate
HAVING (((MilestoneTracking.ActualDate) Is Null));
The ProjectID is the foreign key, therefore number type field, Milestone is a short text and limited to 200 characters and then PlannedDate and ActualDate are both date type fields.
I don't understand why the field would be too small, I've tried limiting the query that is generating the error to only try and pull in the ProjectID or other single fields however it does not work. The only way that it will run is if I drop the Right Join.
Any help is appreciated!

error with a sql query because of ambiguous column name

I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'