how to do a self-join - sql

I have the following table with 3 columns, this is just a sample (not a real one)
Number Decription Value
1 Green 100
1 Yellow 101
1 Blue 102
2 Chair 200
2 Table 101
2 Green 150
3 Car 200
3 plane 205
3 green 105
My first query is to find any record or row which contain value "101" No problemo in this scenario I find 2 records [1, Yellow,101] and [2, table, 101] and all the records with number 3 and the rest are ignored just perfect. Now I need to select other records based on the RESULT of the FIRST query, the number 1 and 2 are the true results. So from column [number] in this case I found [1 and 2], I want to search and add the value of any description = [green]. Still ignoring [3] which has NO [101] value.
The ideal result I want to display is
[1, yellow, 101] and Green is 100
[2, table, 101] and Green is 150
I have got a headache to get it work so far NO good result. If anyone has a any idea how to make the script for this case please let me know. I hope the questions is clear.
P.S the content of the table is fake just to get an impression what is about and fyi it's SQL + PHP.

Try this:
select
concat('[', concat_ws(', ', s1.number, s1.description, s1.value), '] and ', s2.description, ' is ', s2.value)
from
sample s1
inner join
sample s2 on s2.number = s1.number
where
s1.value = 101 and
s2.description = 'Green'
SQL Fiddle Demo

Try this. I think this is what you are trying to do.
edit. Are you trying to only get the "Green"?
SELECT * from table WHERE table.Number IN (
SELECT Number FROM table WHERE table.Value = 101
) AND table.Description = "Green";

I have seen that thx a alot but it shows blanco fields :(.. however I have done many tests this morning after so much coffee I think what I am trying to do doesn't make sens. Because if I find the record [1, yellow, 101] based on the condition value "101" then it is impossible to add the content-field [Green] as I wanted, because number[1] matches both [101] and [Green].
I tried "OR" but it finds also the record [3 green, 105] which is not good because 3 has no [101] data. I know this is crazy puzzle. So now I want to add another Table which will have a UNIQUE number 1,2 and 3 and that number should correspond to the 1st column of the table sample only sample has duplicate of 1's 2' and 3's etc. If I do that would be possible to have a workable condition, where I can display data in one row or list as the following ==> 1, yellow, 101, and Green, 100. I hate to give up :(

Related

Counting from different categories within the same query

I am trying to make a query from a table in Access that would give me totals for different types of product based off of 2 categories, all within one query. For example my Table looks as follows:
Type
Description 1
Description 2
Date
New
Shiny
Black
1/1/2022
New
Black
Dull
1/1/2022
Old
Shiny
Grey
1/1/2022
Old
Grey
Dull
1/1/2022
The query results that I want to receive are as follows:
Description
New
Old
Shiny
1
1
Black
2
0
Dull
1
1
Grey
0
2
The dataset that I am working with isn't as clean as my example shown here and is causing some of the issues. I never had an issue with the code running, but I just felt that there had to be an easier way that I was missing.
They way I was doing it originally just turned into a bunch of separate query's and was messy to get around. I essentially wrote a query to separate the table into new and old types. From there I used a bunch of
SUM(IIF( Description 1 = "x" OR Description 2 = "x") AS X
SUM(IIF( Description 1 = "y" OR Description 2 = "y") AS Y
expressions to count my totals for each of the objects. This would give me a query where all the totals were displayed in columns. Then I created a separate query to join these data sets together into a presentable manner, but it was turning into too much for how many different "types" I had.
I was just looking for a way to combine all of this into 1 query that would make pulling reports much easier.
Strongly advise not to use space in naming convention nor reserved words as names. Date is a reserved word.
Consider:
Query1
SELECT Type, Description1 AS D, [Date], 1 AS Category FROM Table1
UNION SELECT Type, Description2, [Date], 2 FROM Table1;
UNION will not allow duplicate rows. Use UNION ALL to include all records, even if there are duplicates. There is no query designer or wizard for UNION - must type or copy/paste in SQLView of query builder.
Query2
TRANSFORM Nz(Count(Query1.Category),0) AS CountOfCategory
SELECT Query1.D
FROM Query1
GROUP BY Query1.D
PIVOT Query1.Type;

Trying to combine 2 columns into one distinct result

I'm trying to concatenate 2 rows, from different tables into one result. this is the code I currently have...
SELECT CONCAT(C.Correct_Answer, I.Incorrect_Answers) AS Answers
FROM Cor_A AS C, Inc_A AS I
and this is what it returns
Answers
----------------------------
45
410
450
4Green
4Red
4Potato
4Yellow
40
42
This is, however, not what I want.
How do I get them to return in separate records?
4
5
10
50
Green
Red
Potato
Yellow
(What should be "Blue")
any help is appreciated guys, as you can tell, I'm pretty new...
This will eventually return all the records in both fields but if a record is common it return only one record
SELECT Correct_Answer as Answer
FROM Cor_A
UNION
SELECT Incorrect_Answers
FROM Inc_A;

Sqlite query for getting index from sorted result

I need to make a sql query were in I have a table which consists below columns
id Name Color
1 Water red
5 Sun blue Light
7 Fire green
10 Wter red
21 Son blue Light
24 Fore green
So the requirement is I have a record say
5 Sun blue Light
Now I need to get the index of above record from the sorted result of the Name. Say below can be the select query.
SELECT * FROM MYTABLENAME WHERE COLOR LIKE “blue/%” ORDER BY Name ASC
Note:- I cannot load all the records in my memory and iterate as the records can be huge at times. So need to come up with a query that gives the exact indexof the record without loading the records.
Thanks In advance
If you haven't stored the sort result in a temporary table, the only way to do this is to count how many records would be sorted before this record:
SELECT COUNT(*)
FROM MYTABLENAME
WHERE COLOR LIKE “blue/%”
AND Name <= 'Sun'

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.

New to SQL, need help with query

I have a database table of my own that I am trying to construct a query for that seems simple enough, but I feel like I am messing up somewhere because the results are not what they should be.
I basically have a table that is like the following:
Table: Data
Columns:
Row ID Profile Import ID Field ID Product
1 5 Null 5 60 Can
2 0 Null 5 65 Hat
3 0 Null 5 70 Box
4 6 Null 6 60 Fish
I basically want to take the word "Hat" in row 2 and place it into the "Profile" column of row 1, replacing the null value there. I am doing this for multiple rows.
In the case of the multiple rows I want to take the "Profile" column and make it equal to the "Product" column. I only want this to happen in the rows where the "ID" value matches the "Import ID", and where the "Field ID" is 65 specifically. In the example above the "ID" 5 matches the "Import ID" 5, so I want to take the "Product" value "Hat" where the "Field ID" is 65, and place that value into the "Profile" column where the ID is 5. My table has over 9000 rows and 600 would have to be changed in this way, with various ID's needing various products inserted.
The result I would like would be:
Row ID Profile Import ID Field ID Product
1 5 Hat 5 60 Can
2 0 Null 5 65 Hat
3 0 Null 5 70 Box
4 6 Null 6 60 Fish
I pray that makes sense...
My query was this
UPDATE 'Data'
SET 'Profile'='Product'
WHERE 'ID'='Import ID' AND 'Field ID'=65;
I have also tried a subquery
UPDATE 'Data'
SET 'Profile'= (SELECT 'Product' FROM Data WHERE 'Field ID'=65)
WHERE 'ID'='Import ID';
This did not work and I am just wondering if there is some logic I missing. Thank you to anyone who can help, I have been up for a bit trying to understand this...
You need to join the data; something like:
UPDATE d1
SET d1.Profile = d2.Product
FROM [Data] d1 -- dest
INNER JOIN [DATA] d2 -- source
ON d2.[Import ID] = d1.[ID] AND d2.[Field ID] = 65
(note swapped 2 columns...)
A couple thing to keep in mind when learning sql:
it isnt a good idea to have spaces in column names. although they might be easier to read, it makes your queries more difficult. most databases dont allow them at all, and those that do have different ways to specify the columns in queries.
to work around your problem, perhaps you should try to enclose the column name in backticks (`), or in square brackets ([ ]).
in any case, instead of a space, please consider an underscore.
with that in mind you should also remember that not to put column names in quotes. something like
SELECT 'Product' FROM Data WHERE 'Field ID'=65
would not work for two reasons:
a. Selecting quoted text will return that quoted text. so were the where clause to return two rows, you would get the text 'Product' returned twice.
b. here your where clause is comparing the text 'Field ID' with the number 65, which would always be false.
hope that helps