MS Access excluding specific criteria - ms-access-2007

Im trying to filter out certain numbers in MS Access of tried "Not", "Not Like", and <> and having no luck can anyone help?

You could use <> if you are excluding only one number, like.
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
yourNumberField <> 500;
If you want a series of Numbers then you could use,
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
(
(yourNumberField <> 1)
Or
(yourNumberField <> 2)
Or
(yourNumberField <> 3)
Or
(yourNumberField <> 4)
Or
(yourNumberField <> 500)
);
I would also use,
SELECT
yourFieldName1,
yourFieldName2
FROM
yourTableName
WHERE
yourNumberField Not In (1, 2, 3, 4, 500);

Related

USING CASE AS PART OF JOIN ORACLE SQL

I have a query in Oracle SQL Developer to grab data from a raw table (all the data on the raw table are varchars) where I need to "clean" the data when I select it. Have to use a CASE statement to get the store number because sometimes that data isn't in the STORENBR column but can be found in a substring of another column -
SELECT
CASE WHEN m.STORENBR = '0' AND (SUBSTR(m.SENDING_QMGR, 1, 5) = 'PDPOS')
THEN TO_NUMBER((SUBSTR(m.SENDING_QMGR, 8, 4)))
WHEN m.STORENBR = '0' AND (SUBSTR(m.SENDING_QMGR, 1, 8) = 'PROD_POS')
THEN TO_NUMBER((SUBSTR(m.SENDING_QMGR, 9, 4)))
ELSE TO_NUMBER(NVL(m.STORENBR, '0'))
END AS STORENBR,
TO_NUMBER(NVL(m.CONTROLNBR,'0')) AS CONTROLNBR,
TO_NUMBER(NVL(m.LINENBR,'0')) AS LINENBR,
TO_DATE(m.TRANDATE,'YYYY-MM-DD') AS TRANDATE,
TO_NUMBER(NVL(m.NUMMISPRINTED,'0.00'),'99.99') AS NUMMISPRINTED
FROM MISPRINTS_RAW m
WHERE TO_DATE(m.TRANDATE,'YYYY-MM-DD') = '15-MAR-21'
ORDER BY m.STORENBR;
Now I need to also pull an account number from another table (TRANSACTIONS t - not a raw table, so I don't need any CASE or TO_NUMBER to pull data) but I need to join that table on STORENBR, CONTROLNBR, and LINENBR. So how do I use that CASE statement as part of the join to JOIN m.STORENBR on t.STORENBR?
Even if I am not sure about the data structure the following statement should be helpful.
The table with the raw data is "converted" in a subselect, so that a normal join is possible.
SELECT *
FROM (SELECT CASE
WHEN m.storenbr = '0'
AND ( Substr(m.sending_qmgr, 1, 5) = 'PDPOS' ) THEN
To_number(
( Substr(m.sending_qmgr, 8, 4) ))
WHEN m.storenbr = '0'
AND ( Substr(m.sending_qmgr, 1, 8) = 'PROD_POS' ) THEN
To_number(( Substr(m.sending_qmgr, 9, 4) ))
ELSE To_number(Nvl(m.storenbr, '0'))
END AS STORENBR,
To_number(Nvl(m.controlnbr, '0')) AS CONTROLNBR,
To_number(Nvl(m.linenbr, '0')) AS LINENBR,
To_date(m.trandate, 'YYYY-MM-DD') AS TRANDATE,
To_number(Nvl(m.nummisprinted, '0.00'), '99.99') AS NUMMISPRINTED
FROM misprints_raw m) m1,
TRANSACTION t
WHERE t.storenbr = m1.storenbr
AND t.controlnbr = m1.controlnbr
AND t.linenbr = m1.linenbr
AND m1.trandate = DATE '2021-03-15'
ORDER BY m1.storenbr;

query with calculations most efficient way

I have written the query below which works fine & produces the correct results. However I feel this is probably not terribly efficient as my SQL experience is quite limited.
The main thing that sticks out is where I calculate the nominal differences & price differences, these two lines.
1. isnull(hld.Nominal, 0) - isnull(nav.Nominal, 0) NomDiff
2. isnull((hld.Price / nav.LocalPrice - 1) * 100, 0)
Because I also have to put both these lines in the where condition, so the same calculations are being calculated twice. What is a better way of writing this query?
;WITH hld AS
(
SELECT id,
name,
Nominal,
Price
FROM tblIH
),
nav AS
(
SELECT id,
name,
Nominal,
LocalPrice
FROM tblNC
)
SELECT COALESCE(hld.id, nav.id) id,
COALESCE(nav.name, hld.name) name,
ISNULL(hld.Nominal, 0) HldNom,
ISNULL(nav.Nominal, 0) NavNom,
ISNULL(hld.Nominal, 0) - ISNULL(nav.Nominal, 0) NomDiff,
ISNULL(hld.Price, 0) HldPrice,
ISNULL(nav.LocalPrice, 0) NavPrice,
ISNULL((hld.Price / nav.LocalPrice - 1) * 100, 0)
FROM hld
FULL OUTER JOIN nav ON hld.id = nav.id
WHERE ISNULL(hld.Nominal, 0) - ISNULL(nav.Nominal, 0) <> 0
OR ISNULL((hld.Price / nav.LocalPrice - 1) * 100, 0) <> 0
First you select without where condition, you have result as table tmp, then you add where condition with column NomDiff and PriceDiff
;WITH hld AS
(
SELECT id,
name,
Nominal,
Price
FROM tblIH
),
nav AS
(
SELECT id,
name,
Nominal,
LocalPrice
FROM tblNC
)
select *
from (SELECT COALESCE(hld.id, nav.id) id,
COALESCE(nav.name, hld.name) name,
ISNULL(hld.Nominal, 0) HldNom,
ISNULL(nav.Nominal, 0) NavNom,
ISNULL(hld.Nominal, 0) - ISNULL(nav.Nominal, 0) NomDiff,
ISNULL(hld.Price, 0) HldPrice,
ISNULL(nav.LocalPrice, 0) NavPrice,
ISNULL((hld.Price / nav.LocalPrice - 1) * 100, 0) PriceDiff
FROM hld
FULL OUTER JOIN nav ON hld.id = nav.id) tmp
where NomDiff <> 0 or PriceDiff <> 0
The calculation part you can include in the second CTE, then you can simply select or filter the calculated field as a normal column in your final select query, without further calculations.

SQL Column Not Found

I'm trying to build a SQL query to cross checks a couple of columns.
My issue is with the calcontvalue column.
The query works fine until I want to include a Where clause containing it AND calcontvalue = job.contvalue.
SELECT
job.accountno,
job.groupno,
job.contvalue,
job.z1total,
job.z2equip,
job.z3freight,
job.z4pack,
job.z5ancil,
job.z6roy,
job.zrpay,
job.z7paid,
job.z8invtype,
IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue,
group.groupno,
group.grouptype,
group.title FROM job
LEFT JOIN group
ON job.groupno = group.groupno
WHERE grouptype = 'J' AND calcontvalue = job.contvalue
The I'm presented with this error:
SQL:Column 'CALCONTVALUE' is not found.
Not sure what to try next.
You cannot use alias in WHERE, use full expression:
SELECT
job.accountno,
job.groupno,
job.contvalue,
job.z1total,
job.z2equip,
job.z3freight,
job.z4pack,
job.z5ancil,
job.z6roy,
job.zrpay,
job.z7paid,
job.z8invtype,
IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) AS calcontvalue,
group.groupno,
group.grouptype,
group.title
FROM job
LEFT JOIN group
ON job.groupno = group.groupno
WHERE grouptype = 'J'
AND IIF(job.z8invtype <> 0, job.z2equip+job.z5ancil,
IIF(job.z8invtype <> 1, job.z2equip+job.z3freight+job.z4pack+job.z5ancil, 0)) = job.contvalue
Also naming table group is bad practise because GROUP is keyword GROUP BY.

SQL Syntax Error - Multiple Count Multiple Criteria - MS ACCESS

First question...
I've been researching this site and found a SQL that should help me...but I'm getting an error that I can't solve. Find below SQL and Error:
SELECT field1,
Sum(IIf(status = "Accepted", 1, 0)) AS [field1_Accepted]
Sum(IIf(status = "Rejected", 1, 0)) AS [field1_Rejected]
Sum(IIf(status = "Cancelled", 1, 0)) AS [field1_Cancelled]
FROM tbl1
GROUP BY field1;
Error: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect. (Error 3141)
My expectations from this query is this one:
field1/accepted/rejected/cancelled
a/1/2/3
b/2/3/5
c/2/3/4
Letters should be my fld1 names and the other numbers should be a count on how much field has accepted,rejected and cancelled status...
Expressions in the SELECT statement need to be separated by commas. You are missing commas between the column expressions:
SELECT field1,
Sum(IIf(status = "Accepted", 1, 0)) AS [field1_Accepted], -- <<== Here
Sum(IIf(status = "Rejected", 1, 0)) AS [field1_Rejected], -- <<== Here
Sum(IIf(status = "Cancelled", 1, 0)) AS [field1_Cancelled]
FROM tbl1
GROUP BY field1;

Conditional select statement

Consider the following table (snapshot):
I would like to write a query to select rows from the table for which
At least 4 out of 7 column values (VAL, EQ, EFF, ..., SY) are not NULL..
Any idea how to do that?
Nothing fancy here, just count the number of non-null per row:
SELECT *
FROM Table1
WHERE
IIF(VAL IS NULL, 0, 1) +
IIF(EQ IS NULL, 0, 1) +
IIF(EFF IS NULL, 0, 1) +
IIF(SIZE IS NULL, 0, 1) +
IIF(FSCR IS NULL, 0, 1) +
IIF(MSCR IS NULL, 0, 1) +
IIF(SY IS NULL, 0, 1) >= 4
Just noticed you tagged sql-server-2005. IIF is sql server 2012, but you can substitue CASE WHEN VAL IS NULL THEN 1 ELSE 0 END.
How about this? Turning your columns into "rows" and use SQL to count not nulls:
select *
from Table1 as t
where
(
select count(*) from (values
(t.VAL), (t.EQ), (t.EFF), (t.SIZE), (t.FSCR), (t.MSCR), (t.SY)
) as a(val) where a.val is not null
) >= 4
I like this solution because it's splits data from data processing - after you get this derived "table with values", you can do anithing to it, and it's easy to change logic in the future. You can sum, count, do any aggregates you want. If it was something like case when t.VAL then ... end + ..., than you have to change logic many times.
For example, suppose you want to sum all not null elements greater than 2. In this solution you just changing count to sum, add where clause and you done. If it was iif(Val is null, 0, 1) +, first you have to think what should be done to this and then change every item to, for example, case when Val > 2 then Val else 0 end.
sql fiddle demo
Since the values are either numeric or NULL you can use ISNUMERIC() for this:
SELECT *
FROM YourTable
WHERE ISNUMERIC(VAL)+ISNUMERIC(EQ)+ISNUMERIC(EFF)+ISNUMERIC(SIZE)
+ISNUMERIC(FSCR)+ISNUMERIC(MSCR)+ISNUMERIC(SY) >= 4