Select all Items which don't have a particular value in another table - tsql

Table: Car
Columns: CarID, Value
Table: Tyres
Columns: TyreID, CarID, Brand
How can I select all Cars which don't have a corresponding Tyre which is of a certain Brand. In this scenario a car could have a number of different tyres each of which has a different brand.
E.g.
Car1 has 4 tyres, 2 x Brand A, 1 x Brand B, 1 x Brand C.
Car2 has 3 tyres, 1 x Brand A, 2 x Brand B.
Car3 has 4 tyres, 3 x Brand A, 1 x Brand C.
I want to find out the CarID of all vehicles which don't have a single tyre of Brand C. In this instance the result would be Car2.
Example Data:
Car
CarID Value
--------------
1 Abc
2 Def
3 Geh
Tyre
TyreID CarID Brand
----------------
1 1 Brand A
2 1 Brand A
3 1 Brand B
4 1 Brand C
5 2 Brand A
6 2 Brand B
7 2 Brand B
8 3 Brand A
9 3 Brand A
10 3 Brand A
11 3 Brand C
Result
ID 2

Use NOT EXISTS instead NOT IN because IN clause have a limit of 2500 rows
SELECT CarID
FROM Car
WHERE NOT EXISTS (
SELECT 'X'
FROM Tyres
WHERE Brand='Brand C'
and Tyres.carId = Car.carId
)

You can use NOT IN clause
SELECT CarID
FROM Car
WHERE CarID NOT IN (
SELECT CarID
FROM Tyres
WHERE Brand='Brand C'
)

Related

How to collect rows in a batch

I have a table that looks like this:
id
values
1
a
2
b
3
c
4
d
5
e
6
f
and I need to generate group_id column to be able to collect rows in a batch using
select collect_list(values) from table group by group_id
For example, for batchSize = 2
id
values
group_id
1
a
1
2
b
1
3
c
2
4
d
2
5
e
3
6
f
3
to get it out:
group_id
collect_list(values)
1
[a, b]
2
[c, d]
3
[e, f]
or, for batchSize = 3
id
values
group_id
1
a
1
2
b
1
3
c
1
4
d
2
5
e
2
6
f
2
out
group_id
collect_list(values)
1
[a, b, c]
2
[d, e, f]
How do I generate this column group_id so I can collect the values and group by group_id?
You could use row_number and DIV to generate group_id
To expand my answer, we use the Integer division properties to get group id
Row_number will give consecutive Numbers from 1 to N
But we need the numbers to start with 0, so we subtract 1 of the row number
rownumber Div (3)
0 0
1 0
2 0
3 1
4 1
5 1
6 2
This can be proven to be true for all integers to infinity
As group_id must start with 1(not necessary actually) we need to add anither 1 to the result
After with the resulting Group-id you can collect_list(values) tpo get your arrays
SELECT
id, `values`,
((ROW_NUMBER() OVER (ORDEr By id) -1) DIV 3) + 1 group_id
FROM tab1
id
values
group_id
1
a
1
2
b
1
3
c
1
4
d
2
5
e
2
6
f
2
7
g
3
8
h
3
9
i
3
SELECT
id, `values`,
((ROW_NUMBER() OVER (ORDEr By id) -1) DIV 2) + 1 group_id
FROM tab1
id
values
group_id
1
a
1
2
b
1
3
c
2
4
d
2
5
e
3
6
f
3
7
g
4
8
h
4
9
i
5
From my understanding what you're trying to do is chunking your selection.
Then this should do the trick: https://stackoverflow.com/a/29975781/21188126

Group data based on higest value of a partcular column

I have two tables department and category.In my query result listing same department multiple times.I want to group departments based on category table priority column.
Eg Data:
id dept_name dept_category priority
1 Cardio category 2 2
2 Ortho category 3 3
3 Ortho category 1 1
4 ENT category 1 1
5 Ortho category 2 2
I wannt the reslt like:
id dept_name dept_category priority
1 Cardio category 2 2
3 Ortho category 1 1
4 ENT category 1 1
How can i fetch the resul like this.In my category table cantain priority 1,2,3 then 1 is highest priority.I need to group department based on highest priority.
We can use DISTINCT ON with Postgres:
SELECT DISTINCT ON (dept_name) *
FROM yourTable
ORDER BY dept_name, priority;

Get Data from 2 Tables using Join

I have 2 tables :
1. transfer
2. data
in table data 2 records :
id name
1. 2 PQR
2. 3 XYZ
in table transfer 5 records :
id to from amount type
1. 1 2 3 100.00 C
2. 2 3 2 200.00 C
3. 3 2 3 150.00 D
4. 4 3 2 150.00 C
5. 5 2 3 300.00 D
now I want to form query that will take 2 in where condition and give me result
from transfer table that when 2 is in to column then from data should be shown
and when 2 is in from column then to data should be print.
And in result I want other columns that are amount and type.
I want data using join (Any), I am totally confused that how to perform this task.
Expected Result :
from/to amount type
3 100.00 C
3 200.00 C
3 150.00 D
3 300.00 D
Any Guidance on this..
Try Like this
select
case when "from"=2 then "to" when "to"=2 then "from" end "from/to"
,amount,type from transfer
Out put is
form/to amount type
3 100 C
3 200 C
3 150 D
3 150 C
3 100 D
OR
select case when "from"=2 then d.name when "to"=2 then data.name end "from/to",
amount,type from transfer inner join data on ("to"=data.id)
inner join data as d on("from"=d.id)
Out put is
form/to amount type
XYZ 100 C
XYZ 200 C
XYZ 150 D
XYZ 150 C
XYZ 100 D
ADDITION:
prove of working query: http://ideone.com/64kIov

Making a query in MS Access

I am having a problem with my query. I have 2 tables:
Table 1 is AutoCompany it has fields company and CodeCar. CodeCar can be 3 or 4 depending on the type of car that company has.
table 1: AutoCompany
company| CodeCar|
jora 3
jora 4
jora 3
ghita 3
ghita 3
ghita 4
gheorghe 4
gheorghe 3
gheorghe 3
Table 2 CodeCarCompanies has the codes:
car | codeCar
mers 3
vW 4
I need to select the companies with the count of the occurance of the 2 codeCars
resulting in something like this:
company | MERS| VW
jora 2 1
ghita 2 1
gheorghe 2 1
My attempt so far:
SELECT COUNT(dbo.AutoComany) AS MERS, dbo.Company, COUNT(dbo.AutoComany.
[CodeCar]) AS VW,
FROM dbo.AutoComany FULL OUTER JOIN
dbo.AutoComany ON dbo.АВТОМОБ.КодПредпр = AutoCompany.company
WHERE (dbo.CodeCarComapnies.[CodeCar] = 3)
GROUP BY dbo..company, dbo.CodeCarComapnies.[CodeCar]
HAVING (dbo.CodeCarComapnies.[CodeCar] = 4)
In MS Access, I think you want:
SELECT codecarcomapnies.car,
Count(autocompany.codecar) AS CountOfCodeCar
FROM autocompany
INNER JOIN codecarcomapnies
ON autocompany.codecar = codecarcomapnies.codecar
WHERE autocompany.codecar IN ( 3, 4 )
GROUP BY codecarcomapnies.car;
The above was built using the MS Access query design window and the Sum Σ button
Edit re Comment
SELECT Sum(IIf([autocompany].[codecar]=3,1,0)) AS mers,
Sum(IIf([autocompany].[codecar]=4,1,0)) AS vw
FROM autocompany
Or
TRANSFORM Count(autocompany.CodeCar) AS CountOfCodeCar
SELECT "Total" AS Total
FROM autocompany
INNER JOIN CodeCarComapnies
ON autocompany.CodeCar = CodeCarComapnies.codeCar
WHERE autocompany.CodeCar In (3,4)
GROUP BY "Total"
PIVOT CodeCarComapnies.car

How to hide duplicates values in column

I have 2 tables in sql table_a and table_b this is the output: (1 to many relationship)
table_a table_b
id_no (pk) name id_no (fk) id_tabl(pk) order_code order_item
1 a 1 1 11 aple
1 a 1 2 12 orange
1 a 1 3 13 ice
2 b 2 4 12 orange
2 b 2 5 13 ice
3 c 3 6 13 ice
3 c 3 7 12 orange
3 c 3 8 11 aple
I want to display only 1 name with all his order_item.
How can I display it using iReport in the xml?
The output sample:
id_no name order_item
1 a aple
orange
ice
2 b orange
ice
3 c ice
orange
aple
Using only 2 (order_item) field pattern in every pages of my invoice
the other display will be displayed in invoice pages 2.
You should use Data Grouping.
You can read this article about data grouping.
Your can use the query like this:
SELECT table_a.id_no, table_a.name, table_b.order_item FROM table_a, table_b WHERE table_a.id_no=table_b.id_no ORDER BY table_a.name
Note: may be you need to add sort by table_a.id_no column.
You should create the iReport's group for the name field
Note: may be you need to create two groups - for id_no and name fields.
You can use the Group and Details bands for drawing the data row. Or you can put all textField elements to the Detail band. In this case you should set false value to the isPrintRepeatedValues textField's property (for id_no and name fields).