Generate 'source' column value when joining tables to form a view - sql

I'm creating an android app using sqlite and have the following question:
Is there any way I can generate a value for a 'source' column when joining two or more tables to create a view? For instance, Say I have the following two tables
Table 1 (Fruit) Table 2 (Vegies)
Name | Colour Name | Colour
-------|-------- --------|--------
Apple | Red Celery | Green
Orange | Orange Carrot | Orange
Pear | Green Lettuce | Green
I'd like to create a view that looks something like this:
View (Food)
Name | Colour | Type
--------|---------|---------
Apple | Red | Fruit
Orange | Orange | Fruit
Pear | Green | Fruit
Celery | Green | Vegie
Carrot | Orange | Vegie
Lettuce | Green | Vegie
This may or may not be possible... But I figured it would be worth asking. It is important that I can tell which table or 'source' the row came from in my application. There may be a better way to do it than with a view but I figured this way I can keep all of the data I get in their own tables (which have extra info specific to what the table holds) and I don't have to duplicate anything.
P.S. Very new to SQL/Sqlite so if you could add a bit of an explanation that would be awesome!
Many thanks.

Maybe this would be a simple solution
select Fruits.*, 'Fruit' as type from Fruits union select Vegies.*,
'Vegie' from vegies
Hope it helps

Related

How to select or view only the top row in airtable?

I have this table:
Name | Weight | Color
1 Cherry | 1 | Red
2 Apple | 4 | Green
3 Pear | 3 | Yellow
I need a view in which only the top row is visible
Cherry | 1 | Red
When the table changes (new record, sorting), the view changes accordingly
Example 1:
Name | Weight V| Color
1 Apple | 4 | Green
2 Pear | 3 | Yellow
3 Cherry | 1 | Red
single row view:
Apple | 4 | Green
Example 2:
Name | Weight | Color
1 Almond | 0.5 | Brown
2 Apple | 4 | Green
3 Pear | 3 | Yellow
4 Cherry | 1 | Red
single row view:
Almond | 0.5 | Brown
This doesn't seem possible. Didn't find anything in related forums.
GPT3 suggestions were selecting a record by row_id or time_of_creation fields, but this won't help with table resorting.
It also suggested using SELECT(table_name, {}, {limit: N, fields: ["field_1", "field_2"]}) - but limit does not work. Same for FIRST() which doesn't exist.
Any solution to this?
Try a record list. You can sort elements there as well as limit how many you want to list.
I guess the other would be scripting and just "limiting" what the query returns when displaying it (which doesn't sound like what you want). I am not sure there is a simple native way.
I guess in the end record list limits are the closest to what you are trying to achieve, especially since your main criterion for a top row is sorting.

Pivoting a redshift table

I think I am needing to pivot my database... or maybe there is some other function I can use to get the result I am looking for. Below is what my current dataset looks like (I actually have about 15 metrics):
+----------------------------------+---------+------------------------+----------------+
| ID | Metric 1| Metric 2 | Overall Column |
+----------------------------------+---------+------------------------+----------------+
| 1 | Red | Yellow | Red |
| 2 | Yellow | Yellow | Yellow |
| 3 | Yellow | | Yellow |
+----------------------------------+---------+------------------------+----------------+
The overall column already has logic in SQL to say 'Red' if any of the Metrics are Red (even if they are Yellow, too), and then 'Yellow' if any are Yellow. There are also cases where Two metrics can be Yellow, Red, etc. What I am looking to do is add a new column that will show specifically which metric (or metrics) caused the overall value of Red or Yellow. What I am thinking is some sort of pivot that will, for each ID, have metrics as a row value and the corresponding color also as a row value (if that makes sense), and then I can do a listagg function and then join that table back on to my original dataset based on the ID.
Pivot example, ignore col2 & col3..
+----------------------------------+---------+------------------------+----------------+
| ID | col1 | col2 | col3 |
+----------------------------------+---------+------------------------+----------------+
| 1 | Red | | |
| 1 | Yellow | | |
| 3 | Yellow | | |
+----------------------------------+---------+------------------------+----------------+
After this I can listagg that table to capture multiple colors and then join it to the original table. The only thing I am leaving out there is if there is both Red and Yellow metric for an individual ID and then I do a listagg, that would bring both Red and Yellow even though the overall value is based on the Red metric. Hoping the SQL experts can help me out here.
Redshift is currently based on Postgres 8.03 so it is missing a lot of functionality we've come to expect from Postgres over the last few years. So trying to come up with a solution involving unnest, array or lateral is out of the question (I've learned this the hard way).
So barring the availability of all those new-fangled features, you can unpivot the source table and create a set of each id and its metrics by using union all and creating a union for each metric column.
select a.id, metrics.metric
from tbl a
inner join (
select id, metric1 metric from tbl where metric1 is not null
union all select id, metric2 from tbl where metric2 is not null
union all select id, metric15 from tbl where metric15 is not null
) metrics ON metrics.id = a.id
order by a.id, metrics.metric
Results
id | item
---+--------
1 | red
1 | yellow
2 | blue
2 | green
2 | pink
3 | orange
SQL Fiddle

Sybase, show all rows but don't display column data when duplicate

Product: Sybase ASE 11/12/15/16
I am looking to update a Stored Procedure that gets called by different applications, so changing the application(s) isn't an option. What is needed is best explained in examples:
Current results:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
dog | german shepherd | James
dog | husky | Laura
cat | british blue | Mr Fluffles
cat | other | Laserchild
cat | british blue | Sleepy head
fish | goldfish | Goldie
What I need is for the First column's data to be cleared on duplicates. For example, the above data should look like:
type | breed | name
------------------------------------
dog | german shepherd | Bernie
| german shepherd | James
| husky | Laura
cat | british blue | Mr Fluffles
| other | Laserchild
| british blue | Sleepy head
fish | goldfish | Goldie
I know I can do a cursor, but there are around 10,000 records and that doesn't seem proficient. Looking for a select command, don't want to change the data in the database.
After mulling over this, I found a solution that would work and not use a cursor.
select Type,breed,name
into #DontDisplay
from #MyDataList as a1
group by breed
Having breed= (select max(name)
from #MyDataList a2
where a1.breed= a2.breed)
order by breed, name
select n.Type,d.Breed,d.Name
from #MyDataList as d
left join #DontDisplay as n
on d.Breed= n.Breed and d.Name= n.Name
order by Breed
Works great and the solution was based on another solution Sybase SQL Select Distinct Based on Multiple Columns with an ID

Select products, that have all of the parameters in column

I have a Product model and ProductParams, where I store product ids, column with some additional parameters(param) and param type.
It will look like that:
id | type | param
| |
1 | color | blue
1 | type | hard
2 | color | blue
2 | type | soft
.. | ..... | .....
How do I get only first product, when I call it with params = [blue, hard]?
For now I thought of something like Product.joins{product_params}.where{product_params.param.in params}.uniq, but I get 2nd as well, because it is blue too...
I use squeel gem, to constuct SQL queries, but answer doesn't have to be in squeel as well.
EDIT
I came up with solution, but it's a bit strange:
Product.joins{product_params}.where{product_params.param.in params}.group{id}.having{count(product_params.param) == 2}
But it would be great, if there was more graceful one.

SQL WHERE Help: Pulling Data Multiple Rows

I want to pull, say, all rows where a User has Color=blue and Color=red. I am interested in pulling these multiple rows to determine which users CHANGED their Color from blue to red, or from red to blue.
The general query i have now is this. What is wrong and how can i improve it? thank you!
Does this return Zero results because I am asking that the row's value has BOTH blue and red at the same time? (which is impossible)
my other worry, is that if I use OR instead of AND, that i will include rows for users that are color blue, or color red, but did NOT change between the two colors.
I want the results to ONLY show rows 1 and 4
SELECT *
FROM Table a
WHERE a.color='blue'
AND a.color='red'
Table Structure is below
Row | Date | Userid | Session | Color
1 | 11/1 | 001 | 24 | Blue
2 | 11/2 | 002 | 25 | Green
3 | 11/2 | 003 | 26 | Yellow
4 | 11/6 | 001 | 32 | Red
The glaring problem is:
SELECT *
FROM Table a
WHERE a.color='Blue'
OR a.color='Red'
You will either need a field with the previous color to be stored (kind of like a history) if you wish because otherwise there's not enough information in the database to properly assess what colors have been changed from.