How can I enter an Alias and Have all "" in this Field? - sql-server-2008

I'm using SQL Server. I'm trying to enter an Alias field, as a placeholder (haven't received this data yet), and I keep getting an error. Please see the image bellow.
I'm not sure how to set this up in SQL Server, but in Access it would simply be like this:
Any idea how I can make this work?
Thanks to all.
Here is the SQL Script:
SELECT dbo.Plan_ID_Mapping.[Level 3 Service], dbo.Plan_ID_Mapping.[PLAN ID],
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Application_Systems_Software_ID_CI_ID AS CI_ID,
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Application_Systems_Software_Name_CI_Name AS CI_Name,
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Description_Product_Name, dbo.Service_Taxonomy.[Critical Y/N],
dbo.Service_Taxonomy.[Criticality Rationale], dbo.qry_BE.BE, dbo.qry_BE.Street, dbo.qry_BE.City, dbo.qry_BE.ST, dbo.qry_BE.Zip, dbo.qry_BE.Country,
dbo.qry_BE.O_L, dbo.qry_BE.ENTITY, dbo.qry_BE.Comp_Code
FROM dbo.Plan_ID_Mapping INNER JOIN
dbo.All_Enteprise_NONProduction_and_Production_Hardware ON
dbo.Plan_ID_Mapping.[PLAN ID] = dbo.All_Enteprise_NONProduction_and_Production_Hardware.Plan_Id INNER JOIN
dbo.LocationWorkSpaceByPlanWithAllIn ON
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Plan_Id = dbo.LocationWorkSpaceByPlanWithAllIn.Plan_Id INNER JOIN
dbo.qry_BE ON dbo.LocationWorkSpaceByPlanWithAllIn.Location_ID = dbo.qry_BE.BE INNER JOIN
dbo.Service_Taxonomy ON dbo.Plan_ID_Mapping.[Level 3 Service] = dbo.Service_Taxonomy.[Level 3]
GROUP BY dbo.Plan_ID_Mapping.[Level 3 Service], dbo.All_Enteprise_NONProduction_and_Production_Hardware.Application_Systems_Software_ID_CI_ID,
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Application_Systems_Software_Name_CI_Name, dbo.Plan_ID_Mapping.[PLAN ID],
dbo.All_Enteprise_NONProduction_and_Production_Hardware.Description_Product_Name, dbo.Service_Taxonomy.[Critical Y/N],
dbo.Service_Taxonomy.[Criticality Rationale], dbo.qry_BE.BE, dbo.qry_BE.Street, dbo.qry_BE.City, dbo.qry_BE.ST, dbo.qry_BE.Zip, dbo.qry_BE.Country,
dbo.qry_BE.O_L, dbo.qry_BE.ENTITY, dbo.qry_BE.Comp_Code

Quick Answer:
Uncheck the GROUP BY for PACKAGE_ID
EXPLANATION
It looks like PACKAGE_ID is the the alias column you are creating. Which, if there is nothing there, you can't group on it. I rewrote your query below using table alias for clarity and commented out what it really would look like. What is happening is since you don't have anything for PACKAGE_ID the SQL statement literally would reflect:
SELECT NULL AS PACKAGE_ID
Your error lies in trying to group on theLITERAL of NULL. You can't group on a literal. For example the query below wouldn't work since we are attempting to group on the literal 2007. In order to make it work, we'd have to remove the 2007 from the grouping.
SELECT
COLUMN1,
COLUMN2,
2007
FROM
TABLE1
GROUP BY
COLUMN1,
COLUMN2,
2007
YOUR CODE
SELECT
map.[Level 3 Service],
map.[PLAN ID],
eh.Application_Systems_Software_ID_CI_ID AS CI_ID,
eh.Application_Systems_Software_Name_CI_Name AS CI_Name,
eh.Description_Product_Name,
tax.[Critical Y/N],
tax.[Criticality Rationale],
qry.BE,
qry.Street,
qry.City,
qry.ST,
qry.Zip,
qry.Country,
qry.O_L,
qry.ENTITY,
qry.Comp_Code,
NULL as PACKAGE_ID --this is your blank alias field you created
FROM
dbo.Plan_ID_Mapping map
INNER JOIN
dbo.All_Enteprise_NONProduction_and_Production_Hardware eh ON
map.[PLAN ID] = eh.Plan_Id
INNER JOIN
dbo.LocationWorkSpaceByPlanWithAllIn loc ON
eh.Plan_Id = loc.Plan_Id
INNER JOIN
dbo.qry_BE qry ON
loc.Location_ID =qry.BE
INNER JOIN
dbo.Service_Taxonomy tax ON
map.[Level 3 Service] = tax.[Level 3]
GROUP BY
map.[Level 3 Service],
eh.Application_Systems_Software_ID_CI_ID,
eh.Application_Systems_Software_Name_CI_Name,
map.[PLAN ID],
eh.Description_Product_Name,
tax.[Critical Y/N],
tax.[Criticality Rationale],
qry.BE,
qry.Street,
qry.City,
qry.ST,
qry.Zip,
qry.Country,
qry.O_L,
qry.ENTITY,
qry.Comp_Code,
NULL --This is your query trying to group on the blank field

Related

SQL error: Ambiguous column name

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.

Inner join in combination with sum and group by

I am having some strange issue with the SQL statement below. The result groups by user IDs and some of them turn out right but for one of them (user ID = 1) the "initial_average" is multiplied by 3. I really have no idea why.. Is there something wrong with the structure of the statement? If it is not clear, the aim is to sum the field "initial_avg" in the "tasks" table and have it broken out by user. Some help with this is much appreciated. I am using MySQL.
SELECT sum(initial_avg) AS initial_average
, sum(initial_std) AS initial_standard_dev
, tasks.user
, hourly_rate
FROM tasks
INNER JOIN user_project
ON tasks.user=user_project.user
AND tasks.project=59
AND tasks.user=1
GROUP BY tasks.user
I just solved it by adding another "and" clause (AND user_project.project=59 )
Optimize your query (Try it):
SELECT SUM(initial_avg) AS initial_average, SUM(initial_std) AS initial_standard_dev, tasks.user, hourly_rate FROM tasks INNER JOIN user_project ON tasks.user = user_project.user AND tasks.project = User_project.project WHERE tasks.project = 59 AND tasks.user = 1 GROUP BY tasks.user, hourly_rate

Nested Select Statement Query with error "Incorrect syntax near the keyword 'GROUP' "

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;

How do I convert this SQL query into hibernate

Can someone please tell me how to convert this SQL query into hibernate?
SELECT * FROM sys_quote_master AS g1
INNER JOIN
(SELECT order_base_id, order_id FROM sys_quote_master
GROUP BY order_base_id, order_date_last_revised
ORDER BY order_date_last_revised desc) AS g2
ON g2.order_id = g1.order_id;
Basically,
I have tried this and it doesn't work:
DetachedCriteria crit1 = DetachedCriteria.forClass(QuoteMaster.class);
ProjectionList pList = Projections.projectionList();
pList.add(Projections.groupProperty("orderBaseId"));
session = HibernateSessionFactory.currentSession();
Criteria crit = session.createCriteria(QuoteMaster.class);
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
c.setProjection(pList);
I get this error:
org.hibernate.QueryException: could not resolve property: this of:
QuoteMaster
And I think it has to do with this line of code were I am trying to create an inner join to the same table:
Criteria c = crit.createCriteria("QuoteMaster", CriteriaSpecification.INNER_JOIN);
So basically, my question is 'How to create a join to the same table using Criteria.createCriteria or Criteria.createAlias?' The doc for Criteria class states the first parameter is a dot-separated property path, but what the heck does that mean? :)
Every example I found so far shows how to do this with 2 or 3 tables but not to the same table and I have no idea what to use for the first argument.
I dont know how your entities are, this should give rough idea.
from SystQuoteMasterEntity sqm join ( select sqm1.orderbaseid, sqm1.orderId from SystQuoteMasterEntity sqm1 group by orderbaseid,orderdatelastrev order by orderdatelast desc) g2

MySQL SubQuery Conditional

I am trying to build a rather complex view in MySQL and want to do a conditional, but it sems always to fail.
My view (simplified) is
Select entry AS Entry, ,(select count(`poitems`.`entry`) AS `count(poitems.entry)` from `poitems` where (`poitems`.`PurchaseOrder` = `purchaseorder`.`entry`)) AS `TotalEntries`, from purchase orders
this is OK but what I am trying to do is add something like
if ((select count(`poitems`.`entry`) = 0),'query.png',NULL) AS Queryflag
or just test the value of TotalEntries.
Help appreciated! Thanks!
I'm not 100% sure on the names of the columns in purchaseorder or poitems tables but the following should get you headed in the right direction:
select t.Entry,
case when t.TotalEntries > 0 then 'query.png' else null end as Queryflag
from
(
select po.entry as Entry,
count(*) as TotalEntries
from purchaseorder po
left outer join poitems poi on poi.purchaseorder = po.entry
group by po.entry
) t;