Blob in query with joins to create a view - sql

I am struggling with a blob-type-column to join it to a view with multiple joins in it.
The blob-type is the MAINAPL.GRAPHICS.GRAF and highlighted below in my query.
I get following Error from my Oracle Database:
ORA-00932 and it leads to exactly that column.
Is there a possibility to join the blob-type anyways? I need it as a normal column in this complex view.
My main query is this:
CREATE OR REPLACE FORCE VIEW SECAPL.VIEW_DATAFEED2 AS
SELECT
MIN(CASE WHEN MAINAPL.ARTCOPY.SPRID = 'EN' AND MAINAPL.ARTCOPY.ARTCOPYNUM = 1 THEN MAINAPL.ARTCOPY.ART-COPY-1 ELSE NULL END ) COPY1-EN,
MAX(CASE WHEN MAINAPL.CONT.TONGID = 'EN' AND MAINAPL.CLASS1.CLASSTFRLE1 = '1' THEN MAINAPL.CONT.COPY-ONLY END) AS TYPE-EN,
MAINAPL.ARTICLEGRAPHICS.GRAPHID,
**MAINAPL.GRAPHICS.GRAF**,
MAINAPL.GRAPHICS.GRAFFORMAT
FROM
MAINAPL.ARTICLE
LEFT JOIN MAINAPL.ARTCOPY ON MAINAPL.ARTICLE.ARTID = MAINAPL.ARTCOPY.ARTID
INNER JOIN MAINAPL.ARTICLEGRAPHICS ON MAINAPL.ARTICLE.ARTID = MAINAPL.ARTICLEGRAPHICS.ARTID
INNER JOIN MAINAPL.GRAPHICS ON MAINAPL.GRAPHICS.GRAPHICSID = MAINAPL.ARTICLEGRAPHICS.GRAPHICSID
GROUP BY MAINAPL.ARTICLE.ARTID,
MAINAPL.ARTICLEGRAPHICS.GRAPHID,
**MAINAPL.GRAPHICS.GRAF**,
MAINAPL.GRAPHICS.GRAFFORMAT

I think the problem is that you are trying to group by the blob column. You can work around that easily. Use two selects. The first select is basically what you have but without the blob (and possibly without other columns that can be fetched later). You still get the graphid. The second outer select joins the inner select with the mainapl.grapics table on graphid and returns everything including the blob. Hope that made sense?
EDIT:
CREATE OR REPLACE FORCE VIEW SECAPL.VIEW_DATAFEED2 AS
select sub.copy1-en, sub.type-en, sub.graphicsid,
graph.graf, graph.grafformat
from (SELECT
MIN(CASE WHEN MAINAPL.ARTCOPY.SPRID = 'EN' AND MAINAPL.ARTCOPY.ARTCOPYNUM = 1 THEN MAINAPL.ARTCOPY.ART-COPY-1 ELSE NULL END ) COPY1-EN,
MAX(CASE WHEN MAINAPL.CONT.TONGID = 'EN' AND MAINAPL.CLASS1.CLASSTFRLE1 = '1' THEN MAINAPL.CONT.COPY-ONLY END) AS TYPE-EN,
MAINAPL.ARTICLEGRAPHICS.GRAPHICSID
FROM MAINAPL.ARTICLE
LEFT JOIN MAINAPL.ARTCOPY ON MAINAPL.ARTICLE.ARTID = MAINAPL.ARTCOPY.ARTID
INNER JOIN MAINAPL.ARTICLEGRAPHICS ON MAINAPL.ARTICLE.ARTID = MAINAPL.ARTICLEGRAPHICS.ARTID
INNER JOIN MAINAPL.GRAPHICS ON MAINAPL.GRAPHICS.GRAPHICSID = MAINAPL.ARTICLEGRAPHICS.GRAPHICSID
GROUP BY MAINAPL.ARTICLEGRAPHICS.GRAPHICSID
) sub
join MAINAPL.GRAPHICS ON MAINAPL.GRAPHICS.GRAPHICSID = sub.GRAPHICSID
This may not work, but should illustrate the point. I rewrote the inner SQL a bit as you seemed to group by too much, I'm not sure if that was good or bad. I also corrected a possible mistake with the graphicsid column name.
Anyway, the point should be clear - find the records you need with an inner SQL that includes the id you need in order to fetch the blob in the outer SQL.

Related

SQL run doesn't show column values

I've been given a sql query to fetch data from postgres to be used in tableau, but a field was missing so I added it in the query. However, when loading tableau it shows that there are no values for the columns I need. Values for the other columns show, no issue. What I added was "party_space_requirement_status_type_desc". My sql knowledge is limited so I'm not sure if the order plays a part or what I need to do.
SELECT psrpa.related_property_building_skey ,psr.party_space_requirement_skey ,pf.floor_number,ps.suite_number,psrpa.current_space_lease_area, party_space_requirement_status_type_desc
FROM master.party_space_requirement psr
join master.party_space_requirement_property_association psrpa on psrpa.party_space_requirement_skey = psr.party_space_requirement_skey and psrpa.transaction_f is distinct from 'D'
left join master.property pf on pf.property_skey = psrpa.related_property_floor_skey and pf.property_type_desc = 'Floor'and pf.transaction_f is distinct from 'D'
left join master.property ps on ps.property_skey = psrpa.related_property_suite_skey and ps.property_type_desc = 'Suite'and ps.transaction_f is distinct from 'D'
where psr.transaction_f is distinct from 'D'
Any help is appreciated, thank you.

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

Select a field called "return" in postgreSQL

I'm having a problem with a query in postgres, the table cgporders_items has a field called return, I cannot get actual result of that field with this query, it returns me al ceros.
SELECT "Cgporder".id AS "Cgporder__id"
,"Sale".preorder_number AS "Sale__preorder_number"
,"Contact".id AS "Contact__id"
,"Contact".NAME AS "Contact__name"
,"Ptype".NAME AS "Ptype__name"
,(
SELECT code
FROM products
WHERE id = "CgporderItem".parent_id
) AS "Product__parent_code"
,"Product".id AS "Product__id"
,"Product".code AS "Product__code"
,"Product".NAME AS "Product__name"
,"CgporderItem".quantity AS "CgporderItem__quantity"
,"CgporderItem".return AS "CgporderItem__return"
,"CgporderItem".cep_id AS "CgporderItem__cep"
FROM cgporders AS "Cgporder"
INNER JOIN contacts AS "Contact" ON ("Contact".id = "Cgporder".contact_id)
INNER JOIN cgporders_items AS "CgporderItem" ON ("Cgporder".id = "CgporderItem".cgporder_id)
INNER JOIN products AS "Product" ON ("Product".id = "CgporderItem".product_id)
INNER JOIN ptypes AS "Ptype" ON ("Ptype".id = "Product".ptype_id)
LEFT JOIN cgporders_sales AS "CgporderSale" ON ("Cgporder".id = "CgporderSale".cgporder_id)
LEFT JOIN sales AS "Sale" ON ("Sale".id = "CgporderSale".sale_id)
WHERE "CgporderItem".parent_id != 0
AND "Cgporder"."issue_date" >= '2015-11-27'
AND "Cgporder"."issue_date" <= '2015-11-27'
AND "Cgporder"."status" = 'confirmed'
ORDER BY "Ptype".NAME
,"Product"."code";
There are actually a lots of rows that matches the select condition, but it return cero on "CgporderItem".return AS "CgporderItem__return"
If I make a simple query like select "return" from cgporders_items it works. But in this query it does not work.
Can you help me please?
"return" is a reserved word in SQL, but not in Postgres. See the list here. The following code works find in Postgres (SQL Fiddle is here):
create table dum (return int);
select dum.return from dum;
Your problem is something else. If I had to guess, the where clause is too restrictive (the condition on dates is a bit suspect).

SQL query for filtering data

I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;

Joining tables based on values from other tables

I have the following tables. I want to run a query but I think my beginner tsql level won't help here.. It probably also is a situation where I have a bad database design.
Basically I need to select all fields from tblPhotoGalleries. Also I need to create a seperate field named GalleryCategoryName.
GalleryCategoryName field will be the pCatName in tblPhotoGalleryCats.
If pCatName in tblPhotoGalleryCats = '0', then that would mean, ConnectedNewsCatID is something other than 0. In that case;
GalleryCategoryName will be the CategoryName field from tblNewsCategories where CategoryID = ConnectedNewsCatID
Use a left join on the news category table, and use a case expression to choose between the names:
select
g.pgID, g.gName,
GalleryCategoryName = case c.pCatName when '0' then n.CategoryName else c.pCatName end
from tblPhotoGalleries g
inner join tblPhotoGFalleryCats c on c.pCatID = g.FK_pCatID
left join tblNewsCategories n on n.CategoryOd = c.ConnectedNewsCatID
Try starting here:
select *,
case when PGC.pCatName = '0' then NC.CategoryName else PGC.pCatName end as [CatName]
from tblPhotoGalleries as PG inner join
tblPhotoGalleryCats as PGC on PGC.pCatID = FK_pCatID left outer join
tblNewsCategories as NC on NC.CategoryId = ConnectedNewsCatID