Nested selects not excluding data - sql

I'm building this select statement, and got to a point where my conditions are not working. Here is the code:
select *
from (
select ref,usr1,design,forref,replace(stock,'.',',')as stock
from st
where inactivo like '0'
and usr1 not in ('Serv','Reciclagem','portes','pbl','ctb','')
and forref not in ('','0')
) total
where ref not in ('10159%','13159%')
order by usr1
The thing is, when I write the line to exclude certain values on ref table, nothing happens.
where ref not in ('10159%','13159%')
Where is it wrong?

try using
WHERE LEFT(ref, 5) NOT IN ('10159','13159')
HTH!
Thanks.

Related

Combining 2 tables without losing any data

My first table (actually a view) is:
SELECT * FROM VW_MAIN_INFO
My second table is:
SELECT * FROM TBL_POINTS_AND_CYCLES
In a query, I combine both like this:
SELECT TP.TYPE,VMI.*
FROM VW_MAIN_INFO VMI,
TBL_POINTS_AND_CYCLES TP
WHERE VMI.START_INLET_TEMP=TP.TEMP1
AND VMI.START_OUTLET_TEMP=TP.TEMP2
AND VMI.TIME_FORMATTED=CONVERT(DATETIME, TP.DATE, 101)
What you can tell, what really matters for me in the second table (TBL_POINTS_AND_CYCLES) is the field "TYPE".
What do I need help with:
I need to return everything from VW_MAIN_INFO and TYPE (from TBL_POINTS_AND_CYCLES).
However, if I cannot find a type in TBL_POINTS_AND_CYCLES, I should return a specific value (for example, "EMPTY" or null).
How can I achieve? Is the best path to use "minus" like this?
Finally, my problem with minus is that I don't have the same structure in both tables.
Any help? Ideas?
Thank you.
SELECT TP.TYPE ,
VMI.*
FROM VW_MAIN_INFO VMI
LEFT JOIN TBL_POINTS_AND_CYCLES TP ON VMI.START_INLET_TEMP = TP.TEMP1
AND VMI.START_OUTLET_TEMP = TP.TEMP2
AND VMI.TIME_FORMATTED = CONVERT(DATETIME, TP.DATE, 101);

Using SELECT with a display condition

SELECT DISTINCT Invoice.InvNo, Invoice.OrderNo, Part.PartNo,
orders.orddate AS Order_Date, Invoice.InvDate AS Bill_Date,
MiscChg.Descr, MiscChg.RegFee, Invoice.InvAmt,
Orders.ClaimNo, Firm.FirmName AS Ordering_Firm,
**oppatty.attyid(WHERE oppatty.attyfor = 13)**, Location.Name1 AS Location
The bolded section is the part I'm having trouble with. I know what I have isn't right, but it demonstrates what I would like to accomplish. In the oppatty table, there could be several items listed. I want it to only display "AttyID for the entry that has an ATTYFOR = 13".
Hope this make sense, thanks
Jack
You need to add a CASE WHEN to the select statement.
SELECT DISTINCT
Invoice.InvNo,
Invoice.OrderNo,
Part.PartNo,
orders.orddate AS Order_Date,
Invoice.InvDate AS Bill_Date,
MiscChg.Descr,
MiscChg.RegFee,
Invoice.InvAmt,
Orders.ClaimNo,
Firm.FirmName AS Ordering_Firm,
CASE WHEN oppatty.AttyFor = 13
THEN oppatty.AttyId
ELSE '' END AS attyfor,
Location.Name1 AS Location
FROM
.........
This will display the AttyId field when the row's AttyFor field is equal to 13 and show an empty string when it's not.
Your query has no from or where clause and your question is a bit jumbled, but even so, I think I understand what you want to do. Assuming it's acceptable to fill the "AttyID" values with null where "AttyFor" isn't equal to 13, then you could just use a case statement. Try something like this
select
stuff.things,
case
where oppatty.attyfor <> 13 then null
else oppatty.attyid
end as attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
If that's not your desired result, and you'd rather entirely exclude rows where "AttyFor" isnt equal to 13, then just use a where clause.
select
stuff.things,
oppatty.attyid,
stuff.others
from
oppatty
join stuff on oppatty.ID = stuff.ID
where
oppatty.attyfor = 13

Operation must be an updatable query - Access

I'm writing a database and I simply want to update tblSchedule with the ItemNo from tblStock but I get an error when trying to run this:
Operation must be an updatable query
I can't seem to figure out why it's not working.
UPDATE [tblSchedule]
SET [tblSchedule].ItemNo =
(SELECT DISTINCT Item
FROM [tblStock], [tblSchedule]
WHERE [tblStock].Bookcode=[tblSchedule].[PartCode]
)`;
Any help would really be appreciated
You are missing a closing bracket in your SQL.
UPDATE [tblSchedule] Set
[tblSchedule].ItemNo = (
SELECT DISTINCT Item
FROM [tblStock], [tblSchedule -- Missing closing bracket
WHERE ((([tblStock].Bookcode)=[tblSchedule].[PartCode]))
)
Try closing the bracket on tblSchedule.
I do not have an Access database to test this on for you, though.
My guess is your inner SELECT is returning 2 records instead of one.
You can do this to validate.
SELECT Items.ItemNo, count(*) total
FROM
(
SELECT DISTINCT Sc.ItemNo, St.Item
FROM
[tblSchedule] Sc INNER JOIN
[tblStock] St ON Sc.PartCode = St.Bookcode
) as Items
GROUP BY Items.ItemNo
HAVING count(*) > 1;
Due to the simplicity of what I wanted I've gone down the Dlookup route which works successfully.
UPDATE [tblSchedule], [tblStock] SET [tblSchedule].ItemNo = DLookUp("Item","[tblStock]","[tblStock].Bookcode='" & [tblSchedule].[PartCode] & "'")
WHERE (([tblStock].[Bookcode]=[tblSchedule].[PartCode]));
It's probably not the best method but due to the small amount of records it updates (252) it works perfectly without any noticable time delay.
Thanks Again!
Chris

SQL - Getting Name Starting with a particular letter

I have a query here that doesn't work and having trouble pin pointing my mistake.
Any help would be great.
Thanks
I am trying to retrieve records with a program name starting with 'C' but my query returns zero records.
My PROGRAM table has an entry of a ProgName of Chemistry.
SELECT P.ProgNumber, ProgName, StudID, DateEnrolled
FROM PROGRAM AS P, STUDENT AS S
WHERE P.ProgNo = S.ProgNo
AND ProgName LIKE 'C%';
Use
LIKE "C*"
MSAccess doesn't use % as the wildcard
SELECT
P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM
PROGRAM P
JOIN STUDENT S ON S.ProgNo = P.ProgNo
WHERE
P.ProgName LIKE 'C%';
should work... you said you changed it to ='Chemistry', do you get the same result if you use lowercase c in chemistry?
You need to join the different tables like this...Try this...
SELECT P.ProgNumber, P.ProgName, S.StudID, S.DateEnrolled
FROM PROGRAM P
JOIN STUDENT S
ON P.ProgNo = S.ProgNo
WHERE P.ProgName LIKE 'C*'; -- Asterisk because its Access not MS-SQL

need to envelop query to count results

i have this query:
SELECT
[QUERY1].[py],
[QUERY1].[al],
[QUERY1].[ga],
[QUERY1].[sy],
[QUERY1].[pl]
FROM [tab-Sample] as QUERY1, [tab-Sample]
WHERE [tab-Sample].[py] = [QUERY1].[py] AND
[tab-Sample].[al] <> [QUERY1].[al]
I would like to write a query that jsut counts the results of this one, i cannot find where to insert the Count(*) also tried with Over() but im unable to manage this.
Wrap everything as a sub selection in the FROM. Make sure you name it however (Total).
COUNT (*) AS Amount FROM
(
SELECT
[QUERY1].[py],
[QUERY1].[al],
[QUERY1].[ga],
[QUERY1].[sy],
[QUERY1].[pl]
FROM [tab-Sample] as QUERY1, [tab-Sample]
WHERE [tab-Sample].[py] = [QUERY1].[py] AND
[tab-Sample].[al] <> [QUERY1].[al]
) Total