Getting SQL Error: ORA-00957: duplicate column name, while creating view - sql

I am trying to make a view, but I am getting a duplicate column name error. If I run the select query separately, then the query returns a result like:
SELECT distinct app.APP_REF_NO, app.APP_STATUS, app.APP_DT, app.ATTEND_STAFF,
app.ATTEND_BRANCH, app.PRODUCT_TYPE, cust.CUST_ID,
cust.APP_JOINT_T, cust.ID1_TYPE, cust.ID1, cust.ID2_TYPE,
cust.ID2, cust.FIRST_NAME, cust.LAST_NAME, cust.FULL_NAME,
cust.FULL_NAME_CAP, cust.DOB, fac.FACILITY_NO, fac.PRODUCT_TYPE,
fac.PRODUCT_CODE, fac.MAIN_PROD_IND, fac.AMT_APPLIED
FROM
LOSA_APP app
LEFT JOIN
LOSA_CUST cust
ON
cust.APP_REF_NO = app.APP_REF_NO
LEFT JOIN
LOSA_FACILITIES fac
ON
fac.APP_REF_NO = app.APP_REF_NO
LEFT JOIN
OS_CURRENTSTEP STEP
ON
STEP.REF_ID = app.APP_REF_NO
WHERE (app.APP_STATUS ='P' OR app.APP_STATUS ='T' OR
((app.APP_STATUS='R' OR app.APP_STATUS='S') AND STEP.STEP_NAME='011'));
This query works fine. But when I try to run it as a view like:
CREATE VIEW basit_test1 AS
SELECT distinct app.APP_REF_NO, app.APP_STATUS, app.APP_DT, app.ATTEND_STAFF,
app.ATTEND_BRANCH, app.PRODUCT_TYPE, cust.CUST_ID,
cust.APP_JOINT_T, cust.ID1_TYPE, cust.ID1, cust.ID2_TYPE,
cust.ID2, cust.FIRST_NAME, cust.LAST_NAME, cust.FULL_NAME,
cust.FULL_NAME_CAP, cust.DOB, fac.FACILITY_NO, fac.PRODUCT_TYPE,
fac.PRODUCT_CODE, fac.MAIN_PROD_IND, fac.AMT_APPLIED
FROM
LOSA_APP app
LEFT JOIN
LOSA_CUST cust
ON
cust.APP_REF_NO = app.APP_REF_NO
LEFT JOIN
LOSA_FACILITIES fac
ON
fac.APP_REF_NO = app.APP_REF_NO
LEFT JOIN
OS_CURRENTSTEP STEP
ON
STEP.REF_ID = app.APP_REF_NO
WHERE (app.APP_STATUS ='P' OR app.APP_STATUS ='T' OR
((app.APP_STATUS='R' OR app.APP_STATUS='S') AND STEP.STEP_NAME='011'));
Then I get the duplicate column name error. Why am I getting this error?

you have two product_type columns:
fac.PRODUCT_TYPE
and
app.PRODUCT_TYPE
you should alias one of them eg
app.PRODUCT_TYPE app_prod_type

Related

Looking to add in a Count query with Group by INTO an existing working query

Goal:
I wish to get the Count of how many times a WorkItem was re-assigned
From what I understand the proper query is the following:
SELECT
WorkItemDimvw.Id,
COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM WorkItemDimvw INNER JOIN WorkItemAssignedToUserFactvw
ON WorkItemDimvw.WorkItemDimKey = WorkItemAssignedToUserFactvw.WorkItemDimKey
GROUP BY WorkItemDimvw.Id
The EXISTING query is below and I'm wondering / forgeting if I should:
Just add in COUNT(WorkItemAssignedToUserFactvw.WorkItemAssignedToUser_UserDimKey) AS Assignments since joins are existing, except it is group by WorkItemDimvw.Id
Should it instead be a subquery in the Select below?
Query:
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey
FROM
SLAConfigurationDimvw
INNER JOIN SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN ServiceRequestDimvw AS SRD
INNER JOIN WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN WorkItemAssignedToUserFactvw AS IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey
AND IATUFact.DeletedDate IS NULL
The trick is to aggregate the data on a sub query, before you join it.
SELECT
SRD.ID,
SRD.Title,
SRD.Description,
SRD.EntityDimKey,
WI.WorkItemDimKey,
IATUFact.DateKey,
IATUFact.Assignments
FROM
SLAConfigurationDimvw
INNER JOIN
SLAInstanceInformationFactvw
ON SLAConfigurationDimvw.SLAConfigurationDimKey = SLAInstanceInformationFactvw.SLAConfigurationDimKey
RIGHT OUTER JOIN
ServiceRequestDimvw AS SRD
ON <you're missing something here>
INNER JOIN
WorkItemDimvw AS WI
ON SRD.EntityDimKey = WI.EntityDimKey
LEFT OUTER JOIN
(
SELECT
WorkItemDimKey,
DateKey,
COUNT(WorkItemAssignedToUser_UserDimKey) AS Assignments
FROM
WorkItemAssignedToUserFactvw
WHERE
DeletedDate IS NULL
GROUP BY
WorkItemDimKey,
DateKey
)
IATUFact
ON WI.WorkItemDimKey = IATUFact.WorkItemDimKey

Getting data from a join tables tu sub categories in access with SQL

I created this relationship with Access database but when I am creating a query to return the values I dont get the all the data.
How can I get all the data from tube and tmc tables by ProjectType or ProjectId ?
So I should get something like this query but the query generated by access is not working
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId,
Tube.TubeName, Tube.Duration,
Tmc.TmcName
FROM Tmc
INNER JOIN
(Tube INNER JOIN
(Project INNER JOIN ProjectSubTypesId ON Project.ProjectId = ProjectSubTypesId.ProjectId)
ON Tube.TubeId = ProjectSubTypesId.TubeId) ON Tmc.TmcId = ProjectSubTypesId.TmcId;
Try this:
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId
FROM (
(ProjectSubTypes
INNER JOIN Project USING (ProjectId)
)
INNER JOIN Tube USING(TubeId)
)
INNER JOIN Tmc USING(TmcId)
GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;
Something like this work?
SELECT Project.ProjectName,
ProjectSubTypesId.ProjectSubTypesId,
Tube.TubeName, Tube.Duration,
Tmc.TmcName
FROM ((ProjectSubTypes
INNER JOIN Project ON Project.ProjectId = ProjectSubTypes.ProjectId)
INNER JOIN Tube ON Tube.TubeId = ProjectSubTypes.TubeId)
INNER JOIN Tmc ON Tmc.TmcId = ProjectSubTypes.TmcId
ORDER BY ProjectSubTypes.ProjectId;
This should give you a list of all sub-types along with their corresponding Tube, Tmc, and Project data.
If it is possible to have multiple ProjectSubTypes rows with the same TubeId and TmcId then consider adding this line before the ORDER BY line:
GROUP BY ProjectSubTypes.ProjectId, ProjectSubTypes.TubeId, ProjectSubTypes.TmcId

SELECT from SELECT in SQL

I'm working in small food depot where we preparing deliveries for small shops. We using WMS to get our work done. For some reasons i need to get some data from database using SQL builder in system. So the case is.
To get information to which store particular product has been despatched I'm using this query:
select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
inner join item_master im on im.sku_id=cd.sku_id
INNER JOIN pkt_hdr pk ON pk.pkt_ctrl_nbr=cd.pkt_ctrl_nbr
where im.barcode = '105683004'
and cd.open_cage_batch = 31102014
To see list of stores and quantities of product allocated by our customer i'm using this query:
select sd.to_store, sd.alloc_qty, sd.alloc_batch, im.size_desc
from store_distro sd
inner join item_master im on im.sku_id=sd.sku_id
where im.barcode = '105683004'
and sd.alloc_batch = 31102014
But i need a SQL to show me any stores to which product has not been despatched.
I will appreaciate any help
Thanks
You can probably try doing a LEFT JOIN for your first query and check for NULL. Something like
select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
left join item_master im
on im.sku_id = cd.sku_id and im.barcode = '105683004'
LEFT JOIN pkt_hdr pk
ON pk.pkt_ctrl_nbr = cd.pkt_ctrl_nbr
where cd.open_cage_batch = 31102014
and pk.to_store IS NULL
I learned how to SELECT FROM (SELECT's) here:
SQL SELECT Union SELECT FROM (Select...)
In this case, it would be something like...
SELECT (columns)
FROM
(select cd.cage_nbr, cd.units_of_product, pk.to_store
from cage_dtl cd
inner join item_master im on im.sku_id=cd.sku_id
INNER JOIN pkt_hdr pk ON pk.pkt_ctrl_nbr=cd.pkt_ctrl_nbr
where im.barcode = '105683004'
and cd.open_cage_batch = 31102014
) AS dispatched
JOIN
(select sd.to_store, sd.alloc_qty, sd.alloc_batch, im.size_desc
from store_distro sd
inner join item_master im on im.sku_id=sd.sku_id
where im.barcode = '105683004'
and sd.alloc_batch = 31102014
) AS allocated
ON (stuff)

Subquery with multiple joins involved

Still trying to get used to writing queries and I've ran into a problem.
Select count(region)
where (regionTable.A=1) in
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
)
The inner query gives an ID number in one column, the amount of times they appear in the table, and then a bit attribute if they are in region A. The outer query works but the error I get is incorrect syntax near the keyword IN. Of the inner query, I would like a number of how many of them are in region A
You must specify table name in query before where
Select count(region)
from table
where (regionTable.A=1) in
And you must choose one of them.
where regionTable.A = 1
or
where regionTable.A in (..)
Your query has several syntax errors. Based on your comments, I think there is no need for a subquery and you want this:
select jxn.id, count(jxn.id) as counts, regionTable.A
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id, regionTable.A
which can be further simplified to:
select jxn.id, count(jxn.id) as counts
, 1 as A --- you can even omit this line
from jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
where regionTable.A = 1
group by jxn.id
You are getting the error because of this line:
where (regionTable.A=1)
You cannot specify a condition in a where in clause, it should only be column name
Something like this may be what you want:
SELECT COUNT(*)
FROM
(
select jxn.id, count(jxn.id) as counts, regionTable.A
from
jxn inner join
V on jxn.id = V.id inner join
regionTable on v.regionID = regionTable.regionID
group by jxn.id, regionTable.A
) sq
WHERE sq.a = 1

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.