I have troubles with execution sql
any time I execute it gives me an error Ambiguous column name 'salesYTD'
my statement is :
SELECT COUNTRYREGIONCODE, NAME, AVG(SALESQUOTA),AVG(BONUS), AVG(SALESYTD)
FROM SALES.SALESPERSON SP
INNER JOIN SALES.SALESTERRITORY ST
ON SP.TERRITORYID = ST.TERRITORYID
GROUP BY NAME, COUNTRYREGIONCODE;
the name of that column is correct. I don't understand what I am doing wrong. Thanks for any help
This means that SALESYTD is in both tables. I don't know which you want.
When you have more than one table in a query always qualify your column names.
SELECT ST.NAME, ST.COUNTRYREGIONCODE,
AVG(SP.SALESQUOTA), AVG(SP.BONUS), AVG(SP.SALESYTD)
FROM SALES.SALESPERSON SP INNER JOIN
SALES.SALESTERRITORY ST
ON SP.TERRITORYID = ST.TERRITORYID
GROUP BY ST.NAME, ST.COUNTRYREGIONCODE;
I'm just guessing where the columns come from.
Does that column exist in more than one table?
If so, you should name the field like this:
SP.salesYTD
or
ST.salesYTD
Depending on what you want to show.
Good luck.
Related
I would like to add a NOT clause to this SQL query:
Select
members.Member_Id,
members.Title,
members.FirstName,
members.LastName,
members.Po_Box,
members.Street,
members.City,
members.Del,
members.Mobile,
members.eMail,
members.WFTD,
ship_info.Renewal_Date
From
members
Left Join
ship_info on (members.Member_Id = ship_info.Member_Id)
Order By
ship_info.Renewal_Date
The NOT clause is this:
Where Member_Id Not Between 2000 And 3000;
I have tried to place this line in different places, but get an error each time, and since Wamp reports errors in french, find it unhelpful.
Yes there are similar questions like this, but they confuse me more, as I don't understand them enough to modify my script accordingly. I don't know much about arrays and complex code.
I export data from the database for mail merge purposes, and Members whose Ids are in the 2000s are deleted Members, whose Id's were moved from their original Id to that of higher numbers, as I don't like deleting people permanently in case they change their mind later down the track.
Thank you.
You could try it like this:
Select members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
From members
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)
WHERE (members.Member_ID < 2000 OR members.member_ID > 3000)
ORDER BY ship_info.Renewal_Date
Since you are using a JOIN and member_id field resides in both tables of the JOIN, you must specify for which table you need the where statement. Try:
SELECT members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
AND 3000
ORDER BY ship_info.Renewal_Date
According to "Database Administration Fundamentals", the following would be the appropriate syntax:
Select members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
From members
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id)
WHERE NOT members.Member_Id BETWEEN 2000 AND 3000
ORDER BY ship_info.Renewal_Date ;
In the previously mentioned book, it states,
" JOIN statements can be specified in either the FROM or the WHERE
clause, but it is recommended that you specify them in the FROM clause
".
As for clause precedence, here is the correct order to follow:
SELECT -- "what" we want
FROM -- "where" to look for it
JOIN
WHERE -- condition
GROUP BY
HAVING
ORDER BY -- sorting order
I think below should work, if not then please specify error which you got, you just need to prefix table name members (better if you use this table name as alias)
SELECT members.Member_Id
,members.Title
,members.FirstName
,members.LastName
,members.Po_Box
,members.Street
,members.City
,members.Del
,members.Mobile
,members.eMail
,members.WFTD
,ship_info.Renewal_Date
FROM members
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id)
WHERE members.Member_Id NOT BETWEEN 2000
AND 3000
ORDER BY ship_info.Renewal_Date
I have been through a few other posts relating to my error, but none of the solutions seem to work. I'm fairly new to SQL so sorry if its something really simple. I have two tables
Movie Inventory - which has columns movie_title, onhand_qty, and replacement_price
NotFlix - which has subscriber_name, queue_nbr, and movie_title
I am trying to join the two tables to output the total replacement price cost per customer, but when I do it gives me the error titled above. Here is my code, thanks in advance for any help!
SELECT subscriber_name, SUM (replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
)
GROUP BY subscriber_name;
You are missing an alias:
SELECT AliasNameHere.subscriber_name, SUM (AliasNameHere.replacement_price) as replacement
FROM
(SELECT NotFlix.subscriber_name as subscriber_name, NotFlix.movie_title, NotFlix.queue_nbr, MovieInventory.replacement_price as replacement_price
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
) AliasNameHere
GROUP BY subscriber_name;
I Just don't get why are you doing a temporary table in FROM clause, you could just do a basic INNER JOIN here and potientialy avoid problem with alias name :
SELECT NotFlix.subscriber_name, SUM (MovieInventory.replacement_price) as replacement
FROM NotFlix
INNER JOIN MovieInventory
ON NotFlix.movie_title = MovieInventory.movie_title
GROUP BY subscriber_name;
My data set is like so:
Total_Order_Table
Order_no (unique)
Shipped_quantity
Order_Detail_Table
Order_number (not unique)
Quantity_per_bundle
I need to take the sum of Quantity_per_bundle for each order_number from Order_Detail_Table and compare it to the Shipped_quantity.
My idea is an outer join so that my data will look like so:
I need to be able to see quantity discrepancies and if the order number exists in both tables.
Thanks in advance!
Normaly with a FULL OUTER JOIN in sql:
SELECT to.Order_no AS Order_no_Total_Order_Table, od.Order_number AS Order_No_Ordr_detail_Table, SUM(od.Order_number) AS sum_Quanitty_Per_Bundle, od.Order_number
FROM Total_Order_Table AS to
FULL OUTER JOIN Order_Detail_Table AS od ON to.Order_no = od.Order_number
GROUP BY to.Order_no
But FULL OUTER JOIN don't exist in mysql. But you can simulate it : http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
Unless I'm missing something subtle in the question, isn't this just as simple as a SELECT query with a join between the tables.
Something along the lines of this should achieve the result:
SELECT tot.Order_no,
odt.Order_no,
SUM(odt.Quantity_per_bundle),
tot.Shipped_quantity
FROM Total_Order_Table tot
LEFT JOIN Order_Detail_Table odt ON odt.Order_Number = tot.Order_Number
GROUP BY tot.Order_no, odt.Order_no, tot.shipped_quantity
(Code not tested in MySQL so forgive errors)
I'm a new code having trouble getting a subquery to work with the main query data. Essentially I want to count how many PCs are HP PCs from the results. When I run it I get the error: The multi-part identifier "dbo.softwareapplications.softwareid" could not be bound.
select distinct appname, version, linkid,
(select count(make) as totalhp from dbo.workstations where make = 'Hewlett-Packard' and linkdid = t1.linkid)as totalhp
from dbo.softwareapplications as t1
join dbo.assignments on dbo.softwareapplications.softwareid = dbo.IQCSassignments.softwareid
join dbo.workstations on dbo.assignments.wsid = dbo.workstations.wsid
group by assetdescription, version, linkid, totalhp
Any help would be appreciated.
Since you've aliased the "dbo.softwareapplications" table as t1, that's how you have to refer to it in subsequent clauses:
join dbo.assignments on t1.softwareid = dbo.IQCSassignments.softwareid
(Or alternatively, remove the "as t1" alias and the other reference to it.)
Use the alias you created : t1.softwareid instead of dbo.softwareapplications.softwareid
I need to get a title from table 2, table 2 has title and id column.
Table 1 has some data and three of these columns concatenated together makeup the id that can be found in table 1.
I used CONCAT_WS() function and gave this column an alias name and need to use the Alias for the on argument(At least this is what I understood I needed to do)
I thought this could be a simple left join, yet it is not working for me.
This is my query
SELECT
table_openers.mail,
table_openers.f_name,
table_openers.l_name,
table_openers.Quality,
CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group) as 't1aid',
table_groups.aid,
table_groups.group_name
FROM
lance_mailstats.table_openers
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = t1aid;
I get results for mail, f_name, l_name, Quality and t1aid, but the aid and group_name columns of the second table return null.
I feel like you can't use an alias in the ON clause.
Try doing
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group);
"You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column" (from dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html).
And "The conditional_expr used with ON is any conditional expression of the form that can be used in a WHERE clause" (from dev.mysql.com/doc/refman/5.1/en/join.html).
So as a logical inference you're not allowed to use aliases in ON clauses.
try to use a subquery..
it goes like this.........
ex.
SELECT
tbl1.mail, tbl1.f_name, tbl1.l_name,tbl1.Quality, tbl1.t1aid,table_groups.aid,
table_groups.group_name
FROM
(SELECT
table_openers.mail,
table_openers.f_name,
table_openers.l_name,
table_openers.Quality,
CONCAT_WS('-',
table_openers.esp,
table_openers.acc,
table_openers.group) as 't1aid',
FROM
lance_mailstats.table_openers )tbl1
LEFT JOIN
lance_mailstats.table_groups ON table_groups.aid = tbl1.t1aid;