I'm working with a data table that looks something like this:
Names City Date Color Shape
John Smith Baltimore 8/1/2015 Blue
John Smith Baltimore 8/1/2015 Green
John Smith Baltimore 8/1/2015 Rectangle
John Smith Baltimore 8/1/2015
John Smith Baltimore 8/1/2015 Square
John Smith Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Rob Johnson Baltimore 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015
Greg Jackson Philadelphia 8/1/2015 Circle
Greg Jackson Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015 Red
Tom Green Philadelphia 8/1/2015
Tom Green Philadelphia 8/1/2015
My goal with the query is to SELECT all five of the data types present, but to isolate those values in the Names field that have NULL values in the Color and Shape fields. I'm writing this SQL in MS Access. My query so far looks like this:
SELECT [Names], [City], [Date], [Color], [Shape]
FROM [databasename]
WHERE
(
([Color] IS NULL)
AND
([Shape] IS NULL)
);
From the sample data table, I'd like for the results to only include Rob Johnson, since all rows associated with that Name entry have NULL values for the Color and Shape fields. However, with this query, I'm getting all of the other names as well, with the specific rows with NULL values in the Color and Shape fields being returned.
So, the expected output would look like this:
Names City Date Color Shape
Rob Johnson Baltimore 8/1/2015
I suspect that I need to use a GROUP operator here, but I'm not quite sure how to do that. Any ideas?
I think you want this:
SELECT
DISTINCT [Names], [City], [Date], [Color], [Shape]
FROM [table]
WHERE [Names] NOT IN (
SELECT [Names] FROM [table] WHERE ([Color] IS NOT NULL) OR
([Shape] IS NOT NULL)
);
It can be done in other ways, but this should be close to your original query.
You can use an aggregate and inner join:
SELECT d1.* FROM [database-name] d1
INNER JOIN (
select Names,MAX(Color) as mc,MAX(Shape) as ms
from [database-name]
group by Names
) d2
ON d1.Names = d2.Names
WHERE mc IS NULL
AND ms IS NULL
Related
I have two tables that look like this
table_1
store_no
store_loc
ID
1234
CAL
ID123
6789
LAL
ID947
5678
PAA
ID456
5678
PAA
ID654
9876
LAS
ID789
table_2
ID
client_no
client_name
product
ID123
1029
John Doe
tent blue
ID947
1029
John Doe
tent red
ID456
4538
Jane Doe
skates 42
ID654
4538
Jane Doe
skates black red
ID789
9234
John Smith
bag green
I am trying to remove the parts of the 'product' that don't overlap if the 'store_no' and 'store_loc' match. So given these two tables I'm looking to get the following as a result:
ID
client_no
client_name
product
ID123
1029
John Doe
tent blue
ID947
1029
John Doe
tent red
ID456
4538
Jane Doe
skates
ID789
9234
John Smith
bag green
As in the example, I don't have a defined strings that I need removed, the string could be a number or a word. That's why I need a way to extract only the part that overlaps.
I think I need to use IF and REGEXP, but I'm not sure how to do it. I don't know how to make sure I'm only keeping the part of the string that overlaps given a condition.
Consider below simple approach
select t.*
from table_1
join table_2 t using (ID)
qualify row_number() over(partition by store_no, store_loc order by ID) = 1
if applied to sample data in your question - output is
Row ID client_no client_name product
1 ID123 1029 John Doe tent blue
2 ID456 4538 Jane Doe skates 42
3 ID947 1029 John Doe tent red
4 ID789 9234 John Smith bag green
as per sum per heading my question
I am stumped to find query statement in my ORACLE SQL Developer for shorten time as I have huge database and it take time to sorting by individual column with where and join table on each clause statement however it take two hours result outcome differs to testing for one profile customer output which is quicker than whole database.
Purchased table has customer purchased car, so I am trying to get one answer to find out if customer has particular RED colour any car type to output result column regardles past and present transaction.
Purchased table example
Data from Purchased table:
Customer_ID Last_Name First_name Colour Car SUV Truck 4WD
50 Smith John Black Yes
50 Smith John Red Yes
50 Smith John Red Yes Yes
50 Smith John Red Yes
20 McGregor Katie Blue Yes Yes
20 McGregor Katie Red Yes
20 McGregor Katie Black Yes
20 McGregor Katie Red Yes Yes
11 Yang Karen Red Yes
11 Yang Karen Red Yes
90 Wilkins Melissa Black Yes
90 Wilkins Melissa Red Yes Yes
90 Wilkins Melissa Blue Yes Yes
90 Wilkins Melissa Grey
135 Barnes Tom Red Yes Yes Yes
135 Barnes Tom Blue Yes
135 Barnes Tom Black Yes
output result
output result for any red colour car:
Customer_ID Last_Name First_name Car SUV Truck 4WD
50 Smith John Yes No No Yes
20 McGregor Katie Yes No No No
11 Yang Karen Yes Yes No No
90 Wilkins Melissa No No No Yes
135 Barnes Tom Yes No Yes Yes
Thank you for your help
currently I am using below method statements;
WITH
sorted_ as (
select
distinct (colour) ,
customer_id ,
last_name ,
car ,
SUV ,
truck ,
4wd
from purchased)
select
e. customer_id ,
e. last_name ,
nvl (e. car , 'No') car ,
nvl (e. SUV , 'No') SUV ,
nvl (e. truck , 'No') truck ,
nvl (e. 4wd , 'No') 4wd
from (
select
a. customer_id ,
a. last_name ,
a. car ,
b. SUV ,
c. truck ,
d. 4wd
from (
select
distinct(car),
customer_id
from sorted_
where colour = 'Red'
and car = 'Yes') a
left join (
select
distinct(SUV),
customer_id
from sorted_
where colour = 'Red'
and SUV = 'Yes') b
ON a. customer_id = b. customer_id
left join (
select
distinct(truck),
customer_id
from sorted_
where colour = 'Red'
and truck = 'Yes') c
ON a. customer_id = c. customer_id
left join (
select
distinct(4wd),
customer_id
from sorted_
where colour = 'Red'
and 4wd = 'Yes') d
ON a. customer_id = d. customer_id
) e
I think I understood what you want, but desired output (screenshot you posted) doesn't match. For example, McGregor has two "Red" rows with car and 4WD marked as such, while your output says that "Yes" is set to car only. Why? I can't run query you posted as it is invalid and - thus - returns nothing at all.
Sample data:
SQL> select * From purchased;
CUSTOMER_ID LAST_NAME FIRST_NAME COLOU CAR SUV TRUCK FOURWD
----------- ---------- ---------- ----- --- --- ----- -------
50 Smith John Black Yes
50 Smith John Red Yes
50 Smith John Red Yes Yes
50 Smith John Red Yes
20 McGregor Katie Blue Yes Yes
20 McGregor Katie Red Yes --> car is red
20 McGregor Katie Black Yes
20 McGregor Katie Red Yes Yes --> car and 4WD are red
11 Yang Karen Red Yes
11 Yang Karen Red Yes
90 Wilkins Melissa Black Yes
90 Wilkins Melissa Red Yes Yes
90 Wilkins Melissa Blue Yes Yes
90 Wilkins Melissa Grey
135 Barnes Tom Red Yes Yes Yes
135 Barnes Tom Blue Yes
135 Barnes Tom Black Yes
17 rows selected.
Query, as I understood it:
SQL> select customer_id, last_name, first_name,
2 max(case when colour = 'Red' then nvl(car, 'No') end) as car,
3 max(case when colour = 'Red' then nvl(suv, 'No') end) as suv,
4 max(case when colour = 'Red' then nvl(truck, 'No') end) as truck,
5 max(case when colour = 'Red' then nvl(fourwd, 'No') end) as fourwd
6 From purchased
7 where colour = 'Red'
8 group by customer_id, last_name, first_name
9 /
CUSTOMER_ID LAST_NAME FIRST_NAME CAR SUV TRUCK FOURWD
----------- ---------- ---------- --- --- ----- -------
50 Smith John Yes No Yes Yes
20 McGregor Katie Yes No No Yes
11 Yang Karen Yes Yes No No
90 Wilkins Melissa No No Yes Yes
135 Barnes Tom Yes No Yes Yes
SQL>
I am having some minor trouble with creating a query in Access using SQL. Any help would be greatly appreciated.
I am trying to create a query that looks like this
The database looks like this (I had to recreate it with generic names)
LOG
dispatcher tech zone serviceorder casesummary resolution date_
John Doe Trump MZ 13458215 MZ/ description Replaced Printer 8/1/2015
John Doe Clinton Core 45821654 Core/ description Added Paper 8/1/2015
John Doe Smith WA 12458254 WA/ description WOA 8/1/2015
John Doe Smith WA 15465224 WA/ description Replaced Printer 8/2/2015
John Doe Williams WB 15468521 WB/ description Replaced Scangun 8/2/2015
John Doe Jones WFF 48593258 WFF/ description Replaced Scangun 8/3/2015
John Doe Jones WFF 15487528 WFF/ description Replaced printer 8/4/2015
Bob Dole Jones WA 65328451 WA/ description WOA 8/4/2015
Bob Dole Williams WB 48521596 WB/ description WOA 8/5/2015
Bob Dole Trump WC 34852369 WC/ description WOA 8/5/2015
Bob Dole Trump WC 63214789 WC/ description Replaced Printer 8/5/2015
Bob Dole Williams WB 52369852 WB/ description Added Paper 8/6/2015
Bob Dole Johnson WE 25896325 WE/ description WOA 8/6/2015
Jack Hill Jones WC 78521478 WC/ description Replaced Printer 8/6/2015
Jack Hill Jones WC 52585258 WC/ description Replaced Scangun 8/7/2015
Jack Hill Johnson WD 12457898 WD/ description Replaced Scangun 8/8/2015
Jack Hill Clinton Core 45698523 Core/ description Replaced printer 8/8/2015
Jack Hill Smith WA 32589654 WA/ description WOA 8/8/2015
The current code I have is this
SELECT DISTINCT Format(Log.Date_,"ddd") AS [Day], LOG.Date_,
(select COUNT(*) FROM LOG
Where Zone_of_call = "WFF") AS WA
FROM LOG
WHERE (((LOG.Date_) >= [Start Date] And (LOG.Date_) <= [End Date]))
ORDER BY LOG.Date_;
Where the database is LOG. I skipped techs and dispatches and jumped to the Columns for the daily count of each case by zone.
I currently have this table.
name title
Robert Pattinson Twilight
Daniel Radcliff NULL
Leonardo Dicaprio NULL
Charlie Chaplin The kid
Charlie Chaplin The Circus
Charlie Chaplin Timelight
John Abraham Dhoom
John Abraham Race 2
Is there a way to avoid duplicate attributes like 'Charlie Chaplin' and get a result as follows?
name title
Robert Pattinson Twilight
Daniel Radcliff NULL
Leonardo Dicaprio NULL
Charlie Chaplin The kid, The Circus,Timelight
John Abraham Dhoom,Race 2
I'm having trouble understanding how to do a multi-table join without generating lots of duplicate fields.
Let's say that I have three tables:
family: id, name
parent: id, family, name
child: id, family, name
If I do a simple select:
select family.id, family.name from family
order by family.id;
I get a simple list:
ID Name
1 Smith
2 Jones
3 Wong
If I add an inner join:
select family.id, family.name, parent.first_name, parent.last_name
from family
inner join parent
on parent.family = family.id
order by family.id;
I get some duplicated fields:
ID Name Parent
1 Smith Howard Smith
1 Smith Janet Smith
2 Jones Phil Jones
2 Jones Harriet Jones
3 Wong Billy Wong
3 Wong Rachel Wong
And if I add another inner join:
select family.id, family.name, parent.first_name, parent.last_name
from family
inner join parent
on parent.family = family.id
inner join child
on child.family = family.id
order by family.id;
I get even more duplicated fields:
ID Name Parent Child
1 Smith Howard Smith Peter Smith
1 Smith Howard Smith Sally Smith
1 Smith Howard Smith Fred Smith
1 Smith Janet Smith Peter Smith
1 Smith Janet Smith Sally Smith
1 Smith Janet Smith Fred Smith
2 Jones Phil Jones Mark Jones
2 Jones Phil Jones Melissa Jones
2 Jones Harriet Jones Mark Jones
2 Jones Harriet Jones Melissa Jones
3 Wong Billy Wong Mary Wong
3 Wong Billy Wong Jennifer Wong
3 Wong Rachel Wong Mary Wong
3 Wong Rachel Wong Jennifer Wong
What I would prefer, because it's more human readable, is something like this:
ID Name Parent Child
1 Smith Howard Smith Peter Smith
Janet Smith Sally Smith
Fred Smith
2 Jones Phil Jones Mark Jones
Harriet Jones Melissa Jones
3 Wong Billy Wong Mary Wong
Rachel Wong Jennifer Wong
I know that one of the benefits of an inner join is to avoid presenting excess information through a Cartesian product. But it seems that I get something similar with a multi-table join. Is there a way to summarize each group as shown above or will this require post-processing with a scripting language like Python?
Thanks,
--Dan
This is precisely the way the relation databases work: each row must contain all information in itself, with every single field that you request. In other words, each row needs to make sense in isolation from all other rows. If you do a single query and you need to get all three levels of information, you need to deal with eliminating duplicates yourself for the desired formatting.
Alternatively, you can run three separate queries, and then do in-memory joins in code. Although this may be desirable in certain rare situations, it is generally a wrong way of spending your development time, because RDBMS are usually much more efficient at joining relational data.
You've hit it on the head. You'll need some post processing to get the results you're looking for.
SQL query results are always simple tabular data, so to get the results you're looking for would definitely not be a pretty query. You could do it, but it would involve quite a bit of query voodoo, storing things in temporary tables or using cursors, or some other funky workaround.
I'd definitely suggest using an external application to retrieve your data and format it appropriately from there.
ORMs like Entity Framework in .NET can probably do this pretty easily, but you could definitely do this with a few nested collections or dictionaries in any language.