No column was specified for column 1 of 'VIEW_NAME' - sql

I'm trying to create a simple little view but I'm keep getting errors all over the query. [Accepted by Month] has the error No column was specified for column 1 of 'Accepted by Month'. I have looked up pretty much everything online and I know that I need to give the columns I select an alias but whenever I do that it just gives me an error under the AS I use for the column aliases saying incorrect syntax near. Plus I'm getting errors with the comma in the SELECT statement and an error for FROM both saying incorrect syntax.
Someone show me how I should write this query because I haven't made any progress in over an hour on a simple CREATE VIEW.
USE Database_Name
GO
CREATE VIEW [Accepted by Month] AS
SELECT Case.Accepted, Case.CaseID
FROM Case;

You can use the following to solve your problem:
USE Database_Name
GO
CREATE VIEW [Accepted by Month] AS
SELECT [Case].Accepted, [Case].CaseID
FROM [Case];

The brackets are required if you use keywords or special chars in the column names or identifiers or names with white space.
USE Database_Name
GO
CREATE VIEW [Accepted by Month] (Accepted, CaseID) AS
SELECT Accepted, CaseID
FROM [Case];

Related

Snowflake tables with TO, FROM as column names

Looks like we've loaded some snowflake tables via ELT with "TO, FROM" as column names and they are both classic functions in any sql tool
Whenever I run a query for specifically those columns, there's always an error - how do I fix it apart from changing column names? Don't want to change column names as ELT process always happens from mongoDB via log based replication (stitch data)
select * - works perfectly , all other columns work too. Just "to" , "from" is the issue - should that never be used a columns?
select to, from from table limit 10 ; // tested [to, "to", 'to'] - none work
Error: SQL compilation error: error line 1 at position 7 invalid identifier '"to"'
Any ideas how to fix this apart from source column change or snowflake column changes?
Snowflake uses the standard double quotes to escape identifiers. However, when identifiers are escaped, the case of the letters matters, So, these are not the same:
select "to"
select "To"
select "TO"
You need to choose the one that is correct for your column names.
In addition spaces matter, so these are not the same:
select "to "
select " to"
select "to"
That is, what looks like to might be something else. You need to know what that is to escape the name properly.
If you can't figure them out, there is a trick to create a view to give the table reasonable names. Something like this:
create view v_t (to_date, from_date, . . .) as
select *
from t;
You need to be sure to include all the column names in the table in the column name list, in the same order as they are in the table. Then you can use the view with reasonable names.

Using a bit column in a where clause

I am writing a very simple query where I need to retrieve the records based on a column that has two values: 0 and 1. I didn't realize this column has a bit type, therefore when I write the query SQL Server is giving me the message
"Incorrect syntax near the keyword 'Primary'."
My query is simple:
select * from [table name]
where Primary = '1'
I've tried searching the site but couldn't find a good answer. BTW, I only have access to retrieve the data from the table. I can't declare variables or create a stored procedure or any of that stuff. Surely this can't be that complicated. Please assist!
PRIMARY is reserved word and it needs to be quoted:
select * from [table name] where [Primary] = 1;
The problem has nothing to do with the datatype being a bit, the error, in fact, is telling you exactly what the problem is:
Incorrect syntax near the keyword 'Primary'.
Emphasis added.
PRIMARY is a reserved keyword. Ideally don't use keywords for object names, but if you "must" then you must delimit identify the object. In fact, really you should avoid any names for objects that require delimit identifing:
SELECT {Your Columns} --Define the columns, don't use *
FROM dbo.[Table Name] --I hope you don't have white space in your object names too
WHERE [Primary] = 1; --Don't wrap bit/numerical values in quotes.
That's because primary is a reserved word in SQL Server. You would need to quote it:
select * from [table name] where [Primary] = 1
I would warmly recommend changing the column name so it does not conflict with a langauge keyword. There are less than 200 reserved works in SQL Server, which leaves a lot of room for alternatives.
So:
sp_rename '[table name].[primary]', 'prim', 'COLUMN';

Error show when update using phpmyadmin

When I update a column using phpmyadmin in database with following query
UPDATE members
SET `refered` = (SELECT COUNT (*)
FROM `user_details`
WHERE `user_details.sponser`=`members.username`
)
It show a error message like this
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '*) FROM `user_details` WHERE `user_details.sponser`=`members.username`)' at line 1
What may be reason?
Error is in
COUNT (*)
-----^
Remove the space between COUNT and (*).
Try the below query
UPDATE members SET refered = (SELECT COUNT(*) FROM user_details
WHERE user_details.sponser=members.username)
Does the Select query returns any result. If so what is the result. Looks like all your query is inside '' single quotes that you are using it should be removed. Single quotes need to be removed for example .
UPDATE members
SET refered = (SELECT COUNT (*)
FROM user_details
WHERE user_details.sponser=members.username
)
-- there is not single quotes in the query above. please remove it from yours.
Part of your problem, or maybe the whole problem, is the WHERE clause. You've used backticks for the table name, which is correct (or, at least, it's optional in this case; it's needed if your database name or table name has a MySQL reserved name or is otherwise ambiguous). The problem, though, is that the dot separating the database from the table needs to be outside the backticks. So your WHERE clause should look like this instead:
WHERE `user_details`.`sponser`=`members`.`username`

Unknown field name error. Can't figure out SQL statement

I'm sure it's something small, but I can't figure out why this SQL statement isn't working.
INSERT INTO tempTable
SELECT *
FROM
(
SELECT * FROM [table] AS x RIGHT JOIN
(
SELECT DISTINCT a.[Part number], Count(a.[Part number]) AS [Part Count]
FROM [table] AS a
GROUP BY a.[Part number]
)
AS y
ON x.[Part number] = y.[Part number]
);
Every time I run the query in access, I get the error "The INSERT INTO statement contains the following unknown field name: 'Part number'. Make sure you have typed the name correctly, and try the operation again.
When I take out the INSERT INTO wrapping and use just the select statement, it returns the results I want to insert into tempTable.
tempTable was created using this select statement, so I don't think it's an issue of mismatching column names.
Can anybody see an error with my statement?
Are you saying that you created the temp table by using the exact same select query in a Make Table query? Have you looked at tempTable in design view and confirmed the field names? Without seeing all of your fields, my guess is that your select statement outputs multiple fields with the same name.
Additionally, note that 'table' is a reserved word and should not be used as an object name, and the use of spaces in field names is considered poor practice. Just because you CAN do something, doesn't mean that you SHOULD.

Postgresql confuses property as table

I'm trying to use a property that has a .(d0t) in the the propery name, but it seems like postgresql is confused and thinks that i'm trying to access a table property.
Here is the sql query i'm trying to run
select count(metadata.username), metadata.username from appointment join appointment_wrapper a on a.id=appointment_wrapper_id where property='state' and string_value='Kano' and timestamp > '2014-08-01' and timestamp < '2014-11-01' group by metadata.username;
When i run that query it says
ERROR: missing FROM-clause entry for table "metadata"
When i add quotes it says
ERROR: non-integer constant in GROUP BY
Any help on how to solve this?
Thank you
What you're looking for is a "quoted identifier" that you obtain by enclosing your column name in doublequotes to prevent it from being interpreted as a keyword:
SELECT count("metadata.username") from app group by "metadata.username";
Just a reminder that you really, really want to avoid the confusion that having reserved characters in your column or table names bring.