sql select query - alias name for column in joins is not working - sql

I have a query which is working fine..
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND WSDEFN.OD_USER_NO = '-1'
AND WSDEFN.OD_GCIF = '-1'
ORDER BY
wsdefn.workspace_id, WSLMAP.POSITION
But I want to fetch the WSDEFN.WORKSPACE_ID as WORKSPACE_ID.
I tried this
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
but I get an error
'column ambiguously defined'
Could you give me a solution?

It seems to be missing the table alias in the where clause.
Did you try this?
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WSDEFN.WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND
WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND
WSDEFN.OD_USER_NO='-1' AND WSDEFN.OD_GCIF='-1'
ORDER BY
wsdefn.workspace_id,WSLMAP.POSITION

Related

An object or column name is missing or empty. But its not?

I am on the 7th set of queries I have been working on and all of them have used SELECT * INTO some_table without an issue. For some reason tho the below query in SQL Server is throwing the error
An object or column name is missing or empty. For SELECT INTO
statements, verify each column has a name. For other statements, look
for empty alias names. Aliases defined as "" or [] are not allowed.
Change the alias to a valid name.
Any Idea on what it could be?
Note that running the query without the select into will result in data being returned and displayed as expected.
Select * into MYDB.MY_TBL
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHAA"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'
You have two selects. I think you just want:
SELECT OT.U_ID AS "U_ID",
. . .
INTO MYDB.MY_TBL
FROM . . .
The INTO should follow the SELECT column list.
Or alternatively, you could use a subquery, but that does not seem necessary.
Where #GordonLinoff example works I realized what I forgot and it was my FROM () sub-query setup I normally use.
I will provide an example in contrast to Gordon's method.
IE:
Select * into MYDB.MY_TBL
FROM(
SELECT OT.U_ID AS "U_ID"
...
FROM MYDB.OTHER_TBL OT
WHERE OT.U_ID = 'TEST'
) SUB
Please note these two points:
1.You have two columns with given "CHA" alias
2.It Seems you need to rewrite the query as:
SELECT OT.U_ID AS "U_ID"
,R.E AS "E"
,R.IR AS "CKT"
,A.RI AS "OC"
,A.EQ AS "SEQ"
,A.HA AS "CHA_DUPLICATE"
,A.A_HA AS "ATE"
,A.BIL AS "BIL"
,A.CHA AS "CHA_DUPLICATE2"
,A.RAT AS "AMT"
,A.PRM AS "PREM"
,A.T_CHG AS "RAT"
,A.PER AS "LAS"
,A.S_BIL AS "BIL_A"
,A.CD AS "CDE"
,A.CBIL_J AS "BIL_J"
,A.AMT_D AS "TB"
,A.CRY AS "CTRY"
,A.RVW AS "RVW"
INTO MYDB.MY_TBL -- <<<<<<< NOTE
FROM MYDB.OTHER_TBL OT
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblR R
ON OT.E = R.E
JOIN [LINKEDSERVER\INST,0000].FE.dbo.tblA A
ON R.IR = A.IR
WHERE OT.U_ID = 'TEST'

SQL SELECT Query Issues

I am having an issue with my SELECT query. I am trying to find items that do not start with "50700" and have an enabled flag of 1, are in category 4 and sub category 4. Below is the query i have but it is not returning any results when i know there are some. Please note TBL1.FIELD1 = ITM_ID, TBL1.FIELD2 = ENABLED, TBL2.FIELD1 = CAT_ID, TBL2.FIELD2 = SUBCAT_ID.
SELECT DB_NAME.dbo.TBL1.FIELD1
, DB_NAME.dbo.TBL1.FIELD2
, DB_NAME.dbo.TBL2.FIELD1
, DB_NAME.dbo.TBL2.FIELD2
FROM DB_NAME.dbo.TBL1
, DB_NAME.dbo.TBL2
WHERE DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700%'
AND DB_NAME.dbo.TBL1.FIELD2 = 1
AND DB_NAME.dbo.TBL2.FIELD1 = 4
AND DB_NAME.dbo.TBL2.FIELD2 = 4
You can break the query into different subqueries for debug purpose, this way you can figure out which "AND" condition is not working.
For Example:
Firstly I guess you should try a simple query on TBL1.FIELD1 with the simple query :
select TBL1.FIELD1 from DB_NAME.dbo.TBL1 where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700' ;(*if TBL1.FIELD1 is of String type, else go for TBL1.FIELD1 != 50700 * )
Then if you get results ,try to do "AND" with the TBL1.FIELD2 condition.
select TBL1.FIELD1
from DB_NAME.dbo.TBL1
where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700'
and DB_NAME.dbo.TBL1.FIELD2 =4 ;
This way you can proceed further and debug the query. Hope it helps :)

How to give change working of having function dynamicaly on executing an sql statement?

I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1

Getting error ORA-00936: missing expression

When I trying the below SQL I am getting the error ORA-00936: missing expression. Please help me on this, I want distinct on those to columns in Oracle SQL
SELECT rr.RPT_QUE_I,
DISTINCT (rr.ed_sbmt_m, rr.RPT_RUN_STAT_C),
rr.rpt_cstm_x,
rr.rpt_cmplt_m,
CASE
WHEN rr.rpt_run_stat_c = 25453 THEN 'PENDING'
WHEN rr.rpt_run_stat_c = 25454 THEN 'ACTIVE'
WHEN rr.rpt_run_stat_c = 25455 THEN 'FINISHED'
WHEN rr.rpt_run_stat_c = 25458 THEN 'ERROR'
WHEN rr.rpt_run_stat_c = 25460 THEN 'SCHEDULED'
END,
cc.pro_sym_I,
rr.usr_wad_ownr_i
FROM audit_REPORT_RUN rr,
CLIENT_COMPANY cc
WHERE rr.ED_SBMT_M > TO_DATE('06/01/2012','mm/dd/yyyy')
AND rr.ED_SBMT_M < TO_DATE('07/01/2012','mm/dd/yyyy')
AND rr.ORG_I = cc.ORG_I
ORDER BY rr.RPT_QUE_I
You should use DISTINCT keyword at first after the SELECT keyword, and remove the parenthesis,
or if you need to categorize some of it, use GROUP BY functions. hope you're getting the desired result.

SQL server syntax error in update statement, but I can't see it

I'm getting a syntax error on this query, but I can't figure it out.
Incorrect syntax near the keyword
'group'.
I believe its on the last group by, but I don't see whats wrong. Can anyone suggest how to correct this?
UPDATE [NCLGS].[dbo].[CP_CustomerShipTo]
SET TimesUsed = TimesUsed + B.NewCount
from [NCLGS].[dbo].[CP_CustomerShipTo] CST
INNER JOIN (
Select
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip,
Count(recid) As NewCount
from avanti_packingslipheader PKH
where pksdate > dbo.ufn_StartOfDay(DATEADD(d, -1, GETDATE() ) )
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
) B
ON CST.CustomerCode = B.CompanyCode
AND CST.ShipToName = B.CompanyName
AND CST.ShipToAddress1 = B.Addr1
AND CST.City = B.City
AND CST.PostalCode = B.Zip
group by
PKH.CompanyCode,
PKH.CompanyName,
PKH.Addr1,
PKH.Addr2,
PKH.City,
PKH.State,
PKH.Zip
BACKGROUND - I'm trying to do an update statement with a Count(), but of course you can't use agg. functions in an update set statement, so I'm trying to use a subquery.
You have already got GROUP BY inside the subselect, so what does the outer GROUP BY stand for?
You can't reference an alias in a subselect from an outer GROUP BY. But in any event you can't use GROUP BY with an UPDATE statement, and that's what the error message is about.
Try removing the last Group By. What exactly are you hoping this last group by will do?
Change the code to this:
update mytable set
mycolumn = mycolumn + (select x from ...);