How to use all in where clause - sql

I want to write this query using all, but it is not valid SQL:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and all
(Select PlankLoadStatusId from OrderAccountPlankLoad oal where oal.PlankLoadStatusId = 2
and ocl.PlankClientId = oal.PlankClientId
)
What I want to ensure is that all related records in the related table have a status of 2.
But I don't think I am writing this correctly - SSMS does not like the "All" as well as "ocl.PlankClientId" in the query.
What I am doing is ensuring that all the records are valid before I start processing them. I don't want to process the rows in ocl if there are related rows in oal that are not valid.
How do I write this correctly?

select *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2 and not exists
(Select 1 from OrderAccountPlankLoad oal where oal.PlankLoadStatusId <>2
and ocl.PlankClientId = oal.PlankClientId
)

I think this is what you mean:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND ocl.PlankClientId IN
(SELECT oal.PlankClientId
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 2
)

I came up with this which seems to be working:
SELECT *
FROM OrderCustPlankLoad ocl
WHERE ocl.PlankLoadStatusId = 2
AND NOT EXISTS (
SELECT 1
FROM OrderAccountPlankLoad oal
WHERE oal.PlankLoadStatusId = 5
AND ocl.PlankClientId = oal.PlankClientId
)

Related

How to get random id from database using random_between

I try something like
SELECT * FROM table WHERE aspect_id = (SELECT floor(random(aspect_id)::int FROM generate_series(1,8));
and I want to get several aspect_id from table.
Or I try to
SELECT * FROM table WHERE aspect_id = SELECT random_between(1,100) FROM generate_series(1,5);
But still nothing
Could you help me?
What about
SELECT * FROM atable
WHERE aspect_id BETWEEN 1 AND 100
ORDER BY random()
LIMIT 5;

select specific records using IN

I need to select records that has ID = 10,23,30 so I wrote this SQL
Select * from mytable where position(id in '10,23,30') > 0
But the problem I get additional records where ID = 1 and 2
Any ideas how to select only what I need ?
No need for position, just do IN:
Select * from mytable
where id in (10,23,30)
Use IN operator:
Select * from mytable where id in (10,23,30)

SQL server update table with missing values

I have 2 similar tables, one with all the data and the other contains a subset of the first. Every 2-3 days I need to insert in the second table the missing values, and I use this code
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and DB02.VoSe = DB01.VoSe
and DB02.VoBu = DB01.VoBu
)
It always worked but now I have some rows with NULL in column VoSe or VoBu and those values are not inserted correctly (even if executing only the SELECT statement seems to return all the differences). How can I handle these?
Add explicit check for NULL for both of these columns:
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and ((DB02.VoSe IS NULL AND DB01.VoSe IS NULL) OR DB02.VoSe = DB01.VoSe)
and ((DB02.VoBu IS NULL AND DB01.VoBu IS NULL) OR DB02.VoBu = DB01.VoBu)
)
#dotnetom's answer (+1) should work for your problem. However, making some assumptions on the problem you describe, I suspect the following would work just as well:
INSERT INTO [SRVDB2].[dbt].[curve].[curve_value]
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value]
WHERE TargetDate >= '20150505'
EXCEPT SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value]
Use ISNULL to handle the NULL values.
NOTE: Use some random value in ISNULL function which will not come in those two columns. For example i have kept 'AAA'
SELECT *
FROM [SRVDB1].[dbt].[curve].[curve_value] as DB01
WHERE TargetDate >= '20150505'
and NOT EXISTS (SELECT *
FROM [SRVDB2].[dbt].[curve].[curve_value] as DB02
WHERE DB02.TargetDate = DB01.TargetDate
and DB02.[Hour] = DB01.[Hour]
and DB02.[id_Mkt] = DB01.[id_Mkt]
and DB02.[Price] = DB01.[Price]
and ISNULL(DB02.VoSe,'AAA') = ISNULL(DB01.VoSe,'AAA')
and ISNULL(DB02.VoBu,'AAA') = ISNULL(DB01.VoBu,'AAA')
)

SQL Server check if where clause is true for any row

I'm going to select those provinces which intersects any railroad. So I do it like this (Using SQL Spatial):
SELECT * FROM ProvinceTable
WHERE (
SELECT count(*)
FROM RailroadTable
WHERE ProvinceTable.Shape.STIntersects(RailroadTable.Shape) > 1
) > 0
But it is not efficient because it has to check the intersection between every single railroad geometry and province geometry in order to calculate the count. However it is better to stop the where clause as soon as every first intersection detected and there is no need to check others. Here is what I mean:
SELECT * FROM ProvinceTable
WHERE (
--return true if this is true for any row in the RailroadTable:
-- "ProvinceTable.Shape.STIntersects(RailroadTable.Shape) > 1"
)
So is there a better way to rewrite this query for such a goal?
EDIT
Surprisingly This query takes the same time and returns no row:
SELECT * FROM ProvinceTable
WHERE EXISTS (
SELECT *
FROM RailroadTable
WHERE ProvinceTable.Shape.STIntersects(RailroadTable.Shape) > 1
)
You want to use exists:
SELECT pt.*
FROM ProvinceTable pt
WHERE EXISTS (SELECT 1
FROM RailroadTable rt
WHERE pt.Shape.STIntersects(rt.Shape) = 1
);

Fetch unique combinations of two field values

Probably it has been asked before but I cannot find an answer.
Table Data has two columns:
Source Dest
1 2
1 2
2 1
3 1
I trying to come up with a MS Access 2003 SQL query that will return:
1 2
3 1
But all to no avail. Please help!
UPDATE: exactly, I'm trying to exclude 2,1 because 1,2 already included. I need only unique combinations where sequence doesn't matter.
For Ms Access you can try
SELECT DISTINCT
*
FROM Table1 tM
WHERE NOT EXISTS(SELECT 1 FROM Table1 t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
EDIT:
Example with table Data, which is the same...
SELECT DISTINCT
*
FROM Data tM
WHERE NOT EXISTS(SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source)
or (Nice and Access Formatted...)
SELECT DISTINCT *
FROM Data AS tM
WHERE (((Exists (SELECT 1 FROM Data t WHERE tM.Source = t.Dest AND tM.Dest = t.Source AND tm.Source > t.Source))=False));
your question is asked incorrectly. "unique combinations" are all of your records. but i think you mean one line per each Source. so it is:
SELECT *
FROM tab t1
WHERE t1.Dest IN
(
SELECT TOP 1 DISTINCT t2.Dest
FROM tab t2
WHERE t1.Source = t2.Source
)
SELECT t1.* FROM
(SELECT
LEAST(Source, Dest) AS min_val,
GREATEST(Source, Dest) AS max_val
FROM table_name) AS t1
GROUP BY t1.min_val, t1.max_val
Will return
1, 2
1, 3
in MySQL.
To eliminate duplicates, "select distinct" is easier than "group by":
select distinct source,dest from data;
EDIT: I see now that you're trying to get unique combinations (don't include both 1,2 and 2,1). You can do that like:
select distinct source,dest from data
minus
select dest,source from data where source < dest
The "minus" flips the order around and eliminates cases where you already have a match; the "where source < dest" keeps you from removing both (1,2) and (2,1)
Use this query :
SELECT distinct * from tabval ;