SQL SELECT Query Issues - sql

I am having an issue with my SELECT query. I am trying to find items that do not start with "50700" and have an enabled flag of 1, are in category 4 and sub category 4. Below is the query i have but it is not returning any results when i know there are some. Please note TBL1.FIELD1 = ITM_ID, TBL1.FIELD2 = ENABLED, TBL2.FIELD1 = CAT_ID, TBL2.FIELD2 = SUBCAT_ID.
SELECT DB_NAME.dbo.TBL1.FIELD1
, DB_NAME.dbo.TBL1.FIELD2
, DB_NAME.dbo.TBL2.FIELD1
, DB_NAME.dbo.TBL2.FIELD2
FROM DB_NAME.dbo.TBL1
, DB_NAME.dbo.TBL2
WHERE DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700%'
AND DB_NAME.dbo.TBL1.FIELD2 = 1
AND DB_NAME.dbo.TBL2.FIELD1 = 4
AND DB_NAME.dbo.TBL2.FIELD2 = 4

You can break the query into different subqueries for debug purpose, this way you can figure out which "AND" condition is not working.
For Example:
Firstly I guess you should try a simple query on TBL1.FIELD1 with the simple query :
select TBL1.FIELD1 from DB_NAME.dbo.TBL1 where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700' ;(*if TBL1.FIELD1 is of String type, else go for TBL1.FIELD1 != 50700 * )
Then if you get results ,try to do "AND" with the TBL1.FIELD2 condition.
select TBL1.FIELD1
from DB_NAME.dbo.TBL1
where DB_NAME.dbo.TBL1.FIELD1 NOT LIKE '50700'
and DB_NAME.dbo.TBL1.FIELD2 =4 ;
This way you can proceed further and debug the query. Hope it helps :)

Related

ORA-00936: missing expression Java SQL Exception

I´ve been trying to find the error in this statement for a few hours and can´t seem to find it.
It must have something to do with the AND connecting the two WHERE´s since when deleting the first WHERE it works:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA,
E_AUFMASS_KOMMENTARE.AUFTR_NR,
E_AUFMASS_KOMMENTARE.KOMMENTAR,
AUFTR_EXT.ART_GRUPPE
FROM HHNG_AU.E_AUFMASS_KOMMENTARE
INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR
WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' )
AND WHERE NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )
Too many WHERE. You only need where once, then use ANDs and ORs to combine conditions:
SELECT E_AUFMASS_KOMMENTARE.FIBU_FIRMA, E_AUFMASS_KOMMENTARE.AUFTR_NR, E_AUFMASS_KOMMENTARE.KOMMENTAR, AUFTR_EXT.ART_GRUPPE FROM HHNG_AU.E_AUFMASS_KOMMENTARE INNER JOIN HHNG_AU.AUFTR_EXT ON E_AUFMASS_KOMMENTARE.AUFTR_NR = AUFTR_EXT.AUFTR_NR WHERE (E_AUFMASS_KOMMENTARE.AUFTR_NR = '1248823' ) AND NOT EXISTS( SELECT * FROM HHNG_AU.EX_KOMMENTARE WHERE EX_KOMMENTARE.AUFTR_NR = '1248823' )

how do I join two tables sql

I have an issue that I'm hoping you can help me with. I am trying to create charting data for performance of an application that I am working on. The first step for me to perform two select statements with my feature turned off and on.
SELECT onSet.testName,
avg(onSet.elapsed) as avgOn,
0 as avgOff
FROM Results onSet
WHERE onSet.pll = 'On'
GROUP BY onSet.testName
union
SELECT offSet1.testName,
0 as avgOn,
avg(offSet1.elapsed) as avgOff
FROM Results offSet1
WHERE offSet1.pll = 'Off'
GROUP BY offSet1.testName
This gives me data that looks like this:
Add,0,11.4160277777777778
Add,11.413625,0
Delete,0,4.5245277777777778
Delete,4.0039861111111111,0
Evidently union is not the correct feature. Since the data needs to look like:
Add,11.413625,11.4160277777777778
Delete,4.0039861111111111,4.5245277777777778
I've been trying to get inner joins to work but I can't get the syntax to work.
Removing the union and trying to put this statement after the select statements also doesn't work. I evidently have the wrong syntax.
inner join xxx ON onSet.testName=offset1.testName
After getting the data to be like this I want to apply one last select statement that will subtract one column from another and give me the difference. So for me it's just one step at a time.
Thanks in advance.
-KAP
I think you can use a single query with conditional aggregation:
SELECT
testName,
AVG(CASE WHEN pll = 'On' THEN elapsed ELSE 0 END) AS avgOn,
AVG(CASE WHEN pll = 'Off' THEN elapsed ELSE 0 END) AS avgOff
FROM Results
GROUP BY testName
I just saw the filemaker tag and have no idea if this work there, but on MySQL I would try something along
SELECT testName, sum(if(pll = 'On',elapsed,0)) as sumOn,
sum(if(pll = 'On',1,0)) as numOn,
sum(if(pll ='Off',elapsed,0)) as sumOff,
sum(if(pll ='Off',1,0)) as numOff,
sumOn/numOn as avgOn,
sumOff/numOff as avgOff
FROM Results
WHERE pll = 'On' or pll='Off'
GROUP BY testName ;
If it works for you then this should be rather efficient as you do not need to join. If not, thumbs pressed that this triggers another idea.
The difficulty you have with the join you envisioned is that the filtering in the WHERE clause is performed after the join was completed. So, you would still not know what records to use to compute the averages. If the above is not implementable with FileMaker then check if nested queries work. You would then
SELECT testName, on.avg as avgOn, off.avg as avgOff
FROM ( SELECT ... FROM Results ...) as on, () as off
JOIN on.testName=off.testName
If that is also not possible then I would look for temporary tables.
OK guys... thanks for the help again. Here is the final answer. The statement below is FileMaker custom function that takes 4 arguments (platform, runID, model and user count. You can see the sql statement is specified. FileMaker executeSQL() function does not support nested select statements, does not support IF statements embedded in select statements (calc functions do of course) and finally does not support the SQL keyword VALUES. FileMaker does support the SQL keyword CASE which is a little more powerful but is a bit wordy. The select statement is in a variable named sql and result is placed in a variable named result. The ExecuteSQL() function works like a printf statement for param text so you can see the swaps do occur.
Let(
[
sql =
"SELECT testName, (sum( CASE WHEN PLL='On' THEN elapsed ELSE 0 END)) as sumOn,
sum( CASE WHEN PLL='On' THEN 1 ELSE 0 END) as countOn,
sum( CASE WHEN PLL='Off' THEN elapsed ELSE 0 END) as sumOff,
sum( CASE WHEN PLL='Off' THEN 1 ELSE 0 END) as countOff
FROM Results
WHERE Platform = ?
and RunID = ?
and Model = ?
and UserCnt = ?
GROUP BY testName";
result = ExecuteSQL ( sql ; "" ; ""
; platform
; runID
; model
; userCnt )
];
getAverages ( Result ; "" ; 2 )
)
For those interested the custom function looks like this:
getAverages( result, newList, pos )
Let (
[
curValues = Substitute( GetValue( data; pos ); ","; ¶ );
sumOn = GetValue( curValues; 2 ) ;
countOn = GetValue( curValues; 3 );
sumOff = GetValue( curValues; 4 );
countOff = GetValue( curValues; 5 );
avgOn = sumOn / countOn;
avgOff = sumOff / countOff
newItem = ((avgOff - avgOn) / avgOff ) * 100
];
newList & If ( pos > ValueCount( data); newList;
getAverages( data; If ( not IsEmpty( newList); ¶ ) & newItem; pos + 1 ))
)

sql select query - alias name for column in joins is not working

I have a query which is working fine..
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND WSDEFN.OD_USER_NO = '-1'
AND WSDEFN.OD_GCIF = '-1'
ORDER BY
wsdefn.workspace_id, WSLMAP.POSITION
But I want to fetch the WSDEFN.WORKSPACE_ID as WORKSPACE_ID.
I tried this
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
but I get an error
'column ambiguously defined'
Could you give me a solution?
It seems to be missing the table alias in the where clause.
Did you try this?
SELECT
WSDEFN.WORKSPACE_DISPLAY_NM,
LAYDEFN.LAYOUT_DISPLAY_NM,
WSLMAP.POSITION,
LAYDEFN.LAYOUT,
LAYDEFN.PROPORTION,
LAYDEFN.LAYOUT_ID,
WSDEFN.WORKSPACE_ID AS WORKSPACE_ID,
LAYDEFN.BUNDLE_KEY
FROM
WORKSPACE_DEFINITION WSDEFN,
WORKSPACE_LAYOUT_MAP WSLMAP,
LAYOUT_DEFINITION LAYDEFN
WHERE
WSDEFN.WORKSPACE_ID = WSLMAP.WORKSPACE_ID
AND
WSLMAP.LAYOUT_ID = LAYDEFN.LAYOUT_ID
AND
WSDEFN.OD_USER_NO='-1' AND WSDEFN.OD_GCIF='-1'
ORDER BY
wsdefn.workspace_id,WSLMAP.POSITION

How to give change working of having function dynamicaly on executing an sql statement?

I'm having a Sql code like as follows
Select a.ItemCode, a.ItemDesc
From fn_BOM_Material_Master('A', #AsOnDate, #RptDate, #BranchID, #CompID)a
Left Outer Join fn_INV_AsOnDate_Stock(#StockDate, #AsOnDate, #RptDate, #BranchID, #CompID, #Finyear)b
On a.ItemCode=b.ItemCode and b.WarehouseCode<>'WAP'
and a.BranchID=b.BranchID and a.CompID=b.COmpID
Where a.ItemNatureCode = 'F' and a.BranchID = #BranchID and a.CompID = #CompID
Group by a.ItemCode, a.ItemDesc
Having sum(b.CBQty)<=0
Here the problem is that im passing an "#ShowZeroStock" value as as bit if the "#ShowZeroStock" value is '1' then Having should not be validated or (i.e: All values from the table should be returned including zero)
So How to change the query based on passed bit value "#ShowZeroStock"
I can Use "If else " condition at the top and remove having in else part, but for a lengthy query i can't do the same.
Is this the logic you want?
Having sum(b.CBQty) <= 0 or #ShowZeroStock = 1

sql select with one value of two where

This is the only place that I get all answer ;)
I want to select :
SELECT
RTRIM(LTRIM(il.Num_bloc)) AS Bloc,
RTRIM(LTRIM(il.num_colis)) AS Colis,
cd.transporteur AS Coursier,
cd.origine AS Origine,
cd.destination AS Destinataire,
cd.adresse AS [Adresse Destinataire],
cd.poids AS Poids,
il.Signataire, il.num_cin AS CIN, il.date_livraison AS [Date Livraison]
FROM
dbo.cd
INNER JOIN
dbo.il ON cd.bloc = il.Num_bloc AND dbo.cd.colis = dbo.il.num_colis
WHERE
(il.Num_bloc = RTRIM(LTRIM(#ParamBloc)))
AND (il.num_colis = RTRIM(LTRIM(#ParamColis)))
In the way of getting result if the user put ether #ParamBloc or #ParamColis
Try using IsNull() function.
A simple query would go like this
Select * from yourTable
Where
Num_bloc = ISNULL(#ParamBloc, Num_block) AND
num_colis = ISNULL(#ParamColis, num_colis)
The second parameter would make the expression to true if the #parameter Bloc or Colis is null. This query would be useful for all 4 possible combination of these two parameter.