I need to select some columns from two tables in a SQL Server database. I need to use something like the following:
SELECT
c.itmtxt, a.attr1val, a.attr2val, a.attr3val
FROM
code c
JOIN
codeattribute a ON c.itmcd = a.itmcd
WHERE
c.catcd = 1
AND c.itmtxt = 2
AND a.attr1val = 'A'
However, when I add the last where AND statements the query does not work. If I end with the single WHERE clause the code works as expected.
Can anyone see what is wrong?
Thanks!
You'll need to move AND a.attr1val = 'A' into your join clause.
SELECT c.itmtxt, a.attr1val, a.attr2val, a.attr3val FROM code c
JOIN codeattribute a ON c.itmcd = a.itmcd AND a.attr1val = 'A'
WHERE c.catcd = 1
AND c.itmtxt = 2
Related
I have 2 DB2 tables. I want to find out records that are in table A is not Table B with the following condition
I wrote this query and it is not working
SELECT A.CL_BATCH_DEPT,
A.CL_TRANS_CODE, A.CL_CUR_DOC_NO
FROM DBPA60AC.TB_ACCOUNT_EVENT A
LEFT JOIN DBPA60AC.TB_DOCUMENT B ON A.CL_CUR_DOC_NO = B.CL_DOCNO
WHERE A.CL_BATCH_DEPT = 'R07' AND A.CL_TRANS_CODE = '210'
AND A.CL_CUR_DOC_NO = "PI%" AND
B CL_DOCNO IS NULL
I am guessing that you want:
SELECT e.*
FROM DBPA60AC.TB_ACCOUNT_EVENT e LEFT JOIN
DBPA60AC.TB_DOCUMENT d
ON e.CL_CUR_DOC_NO = d.CL_DOCNO
WHERE e.CL_BATCH_DEPT = 'R07' AND
e.CL_TRANS_CODE = '210' AND
e.CL_CUR_DOC_NO LIKE 'PI%' AND
d.CL_DOCNO IS NULL;
That is, the comparison to PI% suggests that you really want LIKE.
Note that I also changed the aliases so they are meaningful.
This is my query .After i comment the part OR (NITransactionStatus = 'SUCCESS') ,there is no slowness.
How can i modify this query such that there is no slow and also i need to include both the conditions thats inside 'WHERE' clause?
SELECT COUNT(*)
FROM IN_CTD D
INNER JOIN IN_CTC C
ON C.InwardCustFileId = D.InwardCustFileId
WHERE (D.CurrentStatusId = 30) OR (NITransactionStatus = 'SUCCESS')
Trying to make it brief :
While i excute this query,it takes too much time to complete its execution . After i comment the 'OR' checking,there is no slowness.The 'IN_CTD' table mentioned in the query contains 2830539 records and the table 'IN_CTC' have 1965 records.How can i modify this query including the 'OR' checking such that it won't take much time to execute ?
Maybe you can try sub query. If NITransactionStatus column is on IN_CTD table, try this:
SELECT COUNT(*)
FROM IN_CTC C
INNER JOIN (SELECT InwardCustFileId FROM IN_CTD WHERE (CurrentStatusId = 30) OR (NITransactionStatus = 'SUCCESS')) AS D
ON C.InwardCustFileId = D.InwardCustFileId
If NITransactionStatus column is on IN_CTC table, try this:
SELECT COUNT(*)
FROM IN_CTC C
INNER JOIN (SELECT InwardCustFileId FROM IN_CTD WHERE CurrentStatusId = 30) AS D
ON C.InwardCustFileId = D.InwardCustFileId
WHERE (NITransactionStatus = 'SUCCESS')
Hope it can help.
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
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