SQL get values in multiple rows with same identifiers - sql

For example, this is my response of
select *
from x
where id = 1
Result:
ID data
1 mouse
1 england
1 computer
Now, how do I search for a mouse, in the country England? I can't really check with this:
AND data = 'mouse'
AND data = 'england'
(Rather not use a query in a query if possible)

If I understand correctly, you have a "set-within-sets" query. You want to find all three attributes for a given id. I recommend using group by and having for this purpose:
select id
from x
where data in ('mouse', 'england')
group by id
having count(*) = 2;

Use subquery to constrain the country. For example for this data
ID DATA
---------- --------
1 computer
1 england
1 mouse
2 austria
2 computer
2 mouse
3 mouse
3 mouse
you first filters only IDs that belongs to England and than
constrains the Mouse.
SELECT *
FROM test
WHERE id IN
(SELECT id FROM test WHERE data = 'england'
)
AND data = 'mouse';
ID DATA
---------- --------
1 mouse

Related

SQL using fallback column for match

Say I have a table in an sql database like
name age shoesize
---------------------
tom 20 NULL
dick NULL 4
harry 30 5
and I want an SQL statement that selects names that have age == X, or as a fallback, if no such names exist, use those with shoe size == Y. In other words, in this table, for X=20,Y=4 I should only get 'tom', while for X=25,Y=4 I should get only 'dick'. I can't do that with
SELECT name FROM table WHERE age = 20 OR shoe size = 4;
because that will select both tom and dick. I'm currently using
SELECT COALESCE ((SELECT name FROM tab WHERE age = 20),(SELECT name FROM tab WHERE shoesize = 4));
but is there a neater way? Also using coalesce like this doesn't allow me to get the whole row - i.e. I can't use SELECT * FROM tab, I can only select a single name.
You can use ORDER BY and FETCH FIRST 1 ROW ONLY or some similar clause:
SELECT name
FROM tab
ORDER BY (CASE WHEN age = X THEN 1
WHEN shoesize = Y THEN 2
ELSE 3
END)
FETCH FIRST 1 ROW ONLY;
Some databases spell FETCH FIRST 1 ROW ONLY like LIMIT or TOP or even something else.

Retrieve a record that has 2 specific features in SQL

I have the following database of pictures (a picture can have multiple features):
Table Picture
-------------
ID Name
1 Mona Lisa
2 Scream
Table Features
-------------
ID Name PictureID (FK to Picture)
1 Portrait 2
2 Expressive 2
3 Big 2
4 Small 1
5 Expressive 1
5 Big 1
I'd wish to do a query that retrieves all the Pictures that are a Portrait AND Big, (so the result would be in this case "Scream"). I've come up with this query to retrieve it, but I'm not sure if it is the prettiest and most efficient way to do it. Here's my proposal:
SELECT *
FROM Picture o
WHERE
(select count(*)
from Feature c
where o.id = c.pictureID and c.Name like '%Portrait%') >= 1
AND
(select count(*)
from Feature c
where o.id = c.pictureID and c.Name like '%Big%') >= 1
In this case, I have to go through the Features table twice (which, to my personal taste, I find it "ugly").
Thanks and regards
SELECT picture.name FROM picture
JOIN features
on picture.id=features.pictureID
WHERE features.name IN('Portrait','Big')
GROUP BY features.pictureID
HAVING COUNT(DISTINCT features.ID)>=2

Sql Query to get number of floors

I am working on a hotel management software and I need to display floors and the rooms on that floor......
I have a wing_master table in the database with following columns -:
wing_id,
wing_name,
floor,
floor_room_count,
status
Its like I have a record for one wing in that hotel which has 4 floors, but when I write a query to get the floors in that wing it just gives me "4" as the result in sql.....
I want the query to return it as follows -:
1
2
3
4
I want it this way so that I can use nested data-list control in asp.net....
My query is "select floors from wing_master where wing_id = 1"
For most databases (not MySQL), you can use a recursive query to obtain all floors:
with all_floors as (
select floors from wing_master where wing_id = 1
union all
select floors - 1 as floors from all_floors
where floors > 1
)
select * from all_floors order by floors;
SQLFiddle example.
In MySQL, the easiest way would be to create a numbers table that has a sequence of numbers up to the highest possible floor. Then join to that table to get all floors:
select num from wing_master
join numbers on
wing_id = 1 and
num <= floors;
SqlFiddle example.
Your query is ok, and also it seems that query and table structure will be fulfilling your requirements. can you show your data, because as per the structure, there should be four rows in the table, showing floor 1, 2, 3, 4
something like this
floor wing_id
1 1
2 1
3 1
4 1
If that is how your data looks, then your query must be ok, else there is some other issue. so do share your structure with few rows of data.

Conditioning on multiple rows in a column in Teradata

Suppose I have a table that looks like this:
id attribute
1 football
1 NFL
1 ball
2 football
2 autograph
2 nfl
2 blah
2 NFL
I would like to get a list of distinct ids where the attribute column contains the terms "football", "NFL", and "ball". So 1 would be included, but 2 would not. What's the most elegant/efficient way to do this in Terdata?
The number of attributes can vary for each id, and terms can repeat. For example, NFL appears twice for id 2.
You can use the following:
select id
from yourtable
where attribute in ('football', 'NFL', 'ball')
group by id
having count(distinct attribute) = 3
See SQL Fiddle with Demo (fiddle is showing MySQL, but this should work in TeraData)

List category/subcategory tree and display its sub-categories in the same row

I have a hierarchical table of Regions and sub-regions, and I need to list a tree of regions and sub-regions (which is easy), but also, I need a column that displays, for each region, all the ids of it's sub regions.
For example:
id name superiorId
-------------------------------
1 RJ NULL
2 Tijuca 1
3 Leblon 1
4 Gavea 2
5 Humaita 2
6 Barra 4
I need the result to be something like:
id name superiorId sub-regions
-----------------------------------------
1 RJ NULL 2,3,4,5,6
2 Tijuca 1 4,5,6
3 Leblon 1 null
4 Gavea 2 4
5 Humaita 2 null
6 Barra 4 null
I have done that by creating a function that retrieves a STUFF() of a region row,
but when I'm selecting all regions from a country, for example, the query becomes really, really slow, since I execute the function to get the region sons for each region.
Does anybody know how to get that in an optimized way?
The function that "retrieves all the ids as a row" is:
I meant that the function returns all the sub-region's ids as a string, separated by a comma.
The function is:
CREATE FUNCTION getSubRegions (#RegionId int)
RETURNS TABLE
AS
RETURN(
select stuff((SELECT CAST( wine_reg.wine_reg_id as varchar)+','
from (select wine_reg_id
, wine_reg_name
, wine_region_superior
from wine_region as t1
where wine_region_superior = #RegionId
or exists
( select *
from wine_region as t2
where wine_reg_id = t1.wine_region_superior
and (
wine_region_superior = #RegionId
)
) ) wine_reg
ORDER BY wine_reg.wine_reg_name ASC for XML path('')),1,0,'')as Sons)
GO
When we used to make these concatenated lists in the database we took a similar approach to what you are doing at first
then when we looked for speed
we made them into CLR functions
http://msdn.microsoft.com/en-US/library/a8s4s5dz(v=VS.90).aspx
and now our database is only responsible for storing and retrieving data
this sort of thing will be in our data layer in the application