SQL query for filtering data - sql

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")
;

Related

Blob in query with joins to create a view

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.

DISTINCT SQL query with inner joins that omits a column from considerations,

I have a DB2 query as follows:
SELECT DISTINCT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD",
RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO",
#XENOS.CUSTREF AS "XENOS_CUSTREF",
#XENOS.ADDUDT AS "XENOS_ADDUDT",
#XENOS.ADUPDD AS "XENOS_ADUPDD",
#XENOS.ADUPDT AS "XENOS_ADUPDT",
#XENOS.ADSTAT AS "XENOS_ADSTAT"
FROM RETAILMASTERFILE INNER JOIN
#XENOS ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP
AND RETAILMASTERFILE.COCOMO = #XENOS.ADDELN
WHERE (RETAILMASTERFILE.DOIDCD = 'CUST008')
AND (RETAILMASTERFILE.COCOMO = '345126032')
AND (RETAILMASTERFILE.DOCOMP = 'LONDON')
The problem is #XENOS.ADUPDT may not be unique which gives me an unwanted duplicate record.
Is there any way I can exclude this from consideration ? Everything I've tried so far within my limited knowledge and crude understanding of group by has so far broken my query.
Use GROUP BY instead:
SELECT RETAILMASTERFILE.DOIDCD AS "RETAILMASTERFILE_DOIDCD",
RETAILMASTERFILE.COCOMO AS "RETAILMASTERFILE_COCOMO",
#XENOS.CUSTREF AS "XENOS_CUSTREF",
#XENOS.ADDUDT AS "XENOS_ADDUDT",
#XENOS.ADUPDD AS "XENOS_ADUPDD",
MAX(#XENOS.ADUPDT) AS "XENOS_ADUPDT",
#XENOS.ADSTAT AS "XENOS_ADSTAT"
FROM RETAILMASTERFILE INNER JOIN
#XENOS
ON RETAILMASTERFILE.DOCOMP = #XENOS.ADCOMP AND
RETAILMASTERFILE.COCOMO = #XENOS.ADDELN
WHERE (RETAILMASTERFILE.DOIDCD = 'CUST008') AND (RETAILMASTERFILE.COCOMO = '345126032') AND
(RETAILMASTERFILE.DOCOMP = 'LONDON')
GROUP BY RETAILMASTERFILE.DOIDCD,
RETAILMASTERFILE.COCOMO,
#XENOS.CUSTREF,
#XENOS.ADDUDT,
#XENOS.ADUPDD,
#XENOS.ADSTAT;

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

SQL Query not working

I have these four tables:
SELECT [B_Key]
,[B_FiscalYear]
,[B_OrgCode]
,[B_SubObject]
,[B_Explanation]
,[B_CIPrefNo]
,[B_OrgBudgetAmt]
,[B_BudgetAmt]
,[B_Initials]
FROM [NAOLI].[dbo].[BudgetTbl]
SELECT [F_Fykey]
,[F_FiscalYear]
,[F_Year]
FROM [NAOLI].[dbo].[codeFiscalYearTbl]
SELECT [O_OrgKey]
,[O_OrgCode]
,[O_OrgDesc]
,[O_Divisions]
FROM [NAOLI].[dbo].[codeOrgCodeTbl]
SELECT [S_SubKey]
,[S_SubObject]
,[S_SubDescrip]
FROM [NAOLI].[dbo].[codeSubObjectTbl]
I need to combine different pieces of the information in these tables in order to make the table of information below:
[B_FiscalYear]
,[O_OrgCode]
,[O_OrgDesc]
,[S_SubObject]
,[S_SubDescrip]
,[B_BudgetAmt]
,[B_Initials]
,[B_CIPrefNo]
,[B_OrgBudgetAmt]
I tried the below query but it returns 0 of the 20750 records.. How do I accomplish this? thanks
SELECT [B_FiscalYear]
,[B_OrgCode]
,[O_OrgDesc]
,[B_SubObject]
,[S_SubDescrip]
,[B_BudgetAmt]
,[B_Initials]
,[B_CIPrefNo]
,[B_OrgBudgetAmt]
INTO dbo.BudgetsTbl
FROM [BudgetTbl] BT, [codeFiscalYearTbl] FY, [codeOrgCodeTbl] OC, [codeSubObjectTbl] SO
WHERE BT.B_FiscalYear = FY.F_Year and BT.B_OrgCode = OC.O_OrgCode and BT.B_SubObject = SO.S_SubObject
The join should be like this:
SELECT [B_FiscalYear]
,[B_OrgCode]
,[O_OrgDesc]
,[B_SubObject]
,[S_SubDescrip]
,[B_BudgetAmt]
,[B_Initials]
,[B_CIPrefNo]
,[B_OrgBudgetAmt]
FROM BudgetTbl BT
JOIN codeFiscalYearTbl FY ON BT.B_FiscalYear = FY.F_Year
JOIN codeOrgCodeTbl OC ON BT.B_OrgCode = OC.O_OrgCode
JOIN codeSubObjectTbl SO ON BT.B_SubObject = SO.S_SubObject
You can look at http://sqlfiddle.com/#!3/8ff6b/7 for more info.
Also you can add left joins if needed.
The proper join syntax is:
SELECT [B_FiscalYear], [B_OrgCode], [O_OrgDesc], [B_SubObject], [S_SubDescrip],
[B_BudgetAmt], [B_Initials], [B_CIPrefNo], [B_OrgBudgetAmt]
INTO dbo.BudgetsTbl
FROM BudgetTbl BT join
codeFiscalYearTbl FY
on BT.B_FiscalYear = FY.F_Year join
codeOrgCodeTbl OC
on BT.B_OrgCode = OC.O_OrgCode join
codeSubObjectTbl SO
on BT.B_SubObject = SO.S_SubObject
Presumably, one or more of your lookup tables are empty. If you want all rows, replace the "join" with "left outer join" in the above query.

SQL - Query Select Joins through multiple tables

Here is the situation:
I search for Persons with ID (empr.empr_cb)
that has bill(s) to pay (transactions.montant)
this refers to transactions.compte_id
which is identical to comptes.id_compte
this refers to comptes.proprio.id
which is identical to empr.id_empr
that would give us the Person ID (empr.empr_cb)
I tried this, but I don't know what joins to set (cross Join?):
SELECT `empr`.`empr_cb`,`transactions`.`montant`
FROM `empr`,`comptes`,`transactions`
WHERE `transactions`.`montant` > `0`
AND `transactions`.`encaissement` = `0`
AND `transactions`.compte_id` = `comptes`.`id_compte`
AND `comptes`.`proprio_id` = `id_empr`
Any ideas how to put the joins?
This query is already using implicit INNER JOINs. It can be rewritten this way:
SELECT empr.empr_cb
, transactions.montant
FROM empr
JOIN comptes ON comptes.proprio_id = empr.id_empr
JOIN transactions ON transactions.compte_id = comptes.id_compte
WHERE transactions.encaissement = 0
AND transactions.montant > 0