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).
Related
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.
Having this simple group of tables, I would like to make an inventory discriminating equal products that came from different providers, but I'm getting an "ambiguity error" running a query I though it would work. I don't know how to solve this.
Here's the query I tried:
SELECT tblProducts.product_Name,
tblProviders.provider,
Nz(Sum(tblIntakes.intake_QTY),0)-
Nz(Sum(tblExits.exit_QTY)) AS Stock
FROM tblProviders,
(tblProducts LEFT JOIN
tblExits
ON tblProducts.product_ID = tblExits.product_ID
) LEFT JOIN
tblIntakes
ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_Name, tblProviders.provider;
You may use subqueries in this case:
SELECT
tblProducts.product_Name,
tblProviders.provider,
Nz((
SELECT SUM(intake_QTY)
FROM tblIntakes
WHERE
tblIntakes.product_ID = tblProducts.product_ID AND
tblIntakes.provider_ID = tblProviders.provider_ID
), 0) -
Nz((
SELECT SUM(exit_QTY)
FROM tblExits
WHERE
tblExits.product_ID = tblProducts.product_ID AND
tblExits.provider_ID = tblProviders.provider_ID
), 0) AS Stock
FROM tblProviders, tblProducts;
I've got a query running that pulls out the records I need.
I want to run another query that pulls out all the other records (excluding the ones in the first query).
I've read up on NOT IN and NOT LIKE but can't seem to get them to work.
The first query is named: qryHunnersPatients
Here's the code for the second query that I have so far:
Right now this is just pulling all the records - but I want to exclude those records in the qryHunnersPatients query
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID);
UPDATE:
I just tried the WHERE NOT EXISTS using the code below:
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID)
WHERE NOT EXISTS
(SELECT ID
FROM qryHunnersPatients AS hunners
WHERE hunners.ID = tblPatientHistoryBaseline.ID);
You need a SubQuery. As In understand that your Query qryHunnersPatients gives you the list of records that you do not wish to see, you need to include that in the NOT IN part of the Query.
SELECT
tblPatientHistoryBaseline.ID,
tblPatientHistoryBaseline.Age,
[tblPatientHistoryBaseline].[Age]-[tblPatientHistoryBaseline].[UrinarySxBegan] AS Duration,
tblPatientHistoryBaseline.IBS,
tblQuestionnaires.UPOINTTotal,
tblQuestionnaires.U,
tblQuestionnaires.P,
tblQuestionnaires.O,
tblQuestionnaires.I,
tblQuestionnaires.N,
tblQuestionnaires.T,
tblQuestionnaires.ICSITotal,
tblQuestionnaires.ICPITotal
FROM
tblPatientHistoryBaseline
INNER JOIN
tblQuestionnaires
ON
(tblPatientHistoryBaseline.Visit = tblQuestionnaires.Visit)
AND
(tblPatientHistoryBaseline.ID = tblQuestionnaires.ID)
WHERE
tblPatientHistoryBaseline.ID
NOT IN
(SELECT qryHunnersPatients.ID FROM qryHunnersPatients);
Assuming ID is unique, you can use WHERE NOT EXISTS:
SELECT {FieldList}
FROM tblPatientHistoryBaseline AS baseline
INNER JOIN tblQuestionnaires AS quest
ON (baseline.Visit = quest.Visit)
AND (baseline.ID = quest.ID);
WHERE NOT EXISTS (
SELECT ID
FROM qryHunnersPatients AS hunners
WHERE hunners.ID = baseline.ID
)
You don't need to bother using the aliases I've added to; I've just added them for readability.
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")
;
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