Extracting data from two tables - join

I have two tables "CAR" -it's fields are- (car_id,name), the second table "CARSFORSALE" contains cars for sale (id,car_id,....)
How i can get 10 rows from "CARSFORSALE" table for each car_id in one query
that mean if i have 7 car_id my result will be <=70

select 1,car_id
union all
select 2,car_id
union all
select 3,car_id
union all
select 4,car_id
union all
select 5,car_id
union all
select 6,car_id
union all
select 7,car_id
union all
select 8,car_id
union all
select 9,car_id
union all
select 10,car_id

Related

How to skip defined Combinations in sql?

--A=100,B=50,C=200
DECLARE #T1 TABLE (CAT1 VARCHAR(1),CAT2 VARCHAR(1),MinVal int)
INSERT INTO #T1
SELECT 'A','A',100
UNION ALL
SELECT 'A','B',50
UNION ALL
SELECT 'A','C',100
UNION ALL
SELECT 'B','A',50
UNION ALL
SELECT 'B','B',50
UNION ALL
SELECT 'B','C',50
Union all
SELECT 'C','A',100
UNION ALL
SELECT 'C','B',100
UNION ALL
SELECT 'C','C',200
select * from #T1
I have to calculate sum of MinValue
which should include only (AB,AC,BC) as whatever min of AB=BA so need to take one only.
so in result i want to get 3 rows out of 9.
Cat1|Cat2|MinVal
A|B|50
A|C|100
B|C|50
Any help will be highly appreciated.
Earlier i tried the things below but it did not work.
Select * from #T1 where Cat1<>Cat2 and ?
(what condition i need to write to avoid undesired combination.)
Here's a link

Run 2 select statement in DB2 depending of the result of one of them

I'm trying to do an SP in DB2 with 2 select statements. If the first select returns null, perform the second one.
For example
Select a, b, c from table A where...
--If first select returns null
Select a, from table B where...
I tried a lot of ideas but none of them worked.
Thanks
You can use this general pattern, of course you will have to adapt your two result sets to match
WITH first AS
(
SELECT ..result1.. FROM table1
WHERE ..clause1..
)
SELECT ..result1.. FROM first
UNION
SELECT ..result2.. FROM table2
WHERE 0=(SELECT COUNT(1) FROM first)
AND
..clause2..
Here is a simple way to write that
Select a, from table B where...
and not exists (select * from table a where...)
union
select a,.. from table A)

SQL Join based on three keys

Database is Teradata
I have two table which I am trying to join. Following are the table structures. When I join these table I expect to get two rows as output but getting 4 rows.what is reason for this behavior. Join based on three keys should uniquely identify a row but still getting 4 rows as output. Any help is appreciated.
TableA
Weekkey|segment|type|users
201501|1|A|100
201501|1|B|100
TableB
Weekkey|segment|type|revenue
201501|1|A|200
201501|1|B|200
when I join these two table using the following query i get the following result
select a.* ,b.user
from tablea a left join tableb b on a.weekkey=b.weekkey
and a.segment=b.segment
and a.type=b.type
Weekkey|segment|type|revenue|users
201501|1|A|200|100
201501|1|B|200|100
201501|1|A|200|100
201501|1|B|200|100
Using sql server, here is ddl and sample data along with the query you posted. The output you state you are getting doesn't happen here.
create table #tablea
(
Weekkey int
, segment int
, type char(1)
, users int
)
insert #tablea
select 201501, 1, 'A', 100 union all
select 201501, 1, 'B', 100
create table #TableB
(
Weekkey int
, segment int
, type char(1)
, revenue int
)
insert #TableB
select 201501, 1, 'A', 200 union all
select 201501, 1, 'B', 200
select a.*
, b.revenue
from #tablea a
left join #tableb b on a.weekkey = b.weekkey
and a.segment = b.segment
and a.type = b.type
drop table #tablea
drop table #TableB

Sqlite where clause with multiple values in select statement

I am trying a sqlite select query statement as below:
SELECT IndicatorText
FROM Table
where IndicatorID in('13','25','64','52','13','25','328')
AND RubricID in('1','1','1','1','1','1','6')
This gives an output but the duplicate values are not displayed. I want to display all the values of IndicatorText even though it is duplicate.
Please help me with this query.
The two IN conditions are evaluated individually.
To check both values at once, you could concatenate them so that you have a single string to compare:
SELECT IndicatorText
FROM MyTable
WHERE IndicatorID || ',' || RubricID IN (
'13,1', '25,1', '64,1', '52,1', '13,1', '25,1', '328,6')
However, doing this operation on the column values prevents the query optimizer from using indexes, so this query will be slow if the table is big.
To allow optimizations, create a temporary table with the desired values, and join that with the original table:
SELECT IndicatorText
FROM MyTable
NATURAL JOIN (SELECT 13 AS IndicatorID, 1 AS RubricID UNION ALL
SELECT 25, 1 UNION ALL
SELECT 64, 1 UNION ALL
SELECT 52, 1 UNION ALL
SELECT 13, 1 UNION ALL
SELECT 25, 1 UNION ALL
SELECT 328, 6)

Postgres Rank As Column

I have the following query:
SELECT name, rank() OVER (PARTITION BY user_id ORDER BY love_count DESC) AS position FROM items
And I'd now like to do a where clause on the rank() function:
SELECT name, rank() OVER (PARTITION BY user_id ORDER BY love_count DESC) AS position FROM items WHERE position = 1
That is, I want to query the most loved item for each user. However, this results in:
PGError: ERROR: column "position" does not exist
Also, I'm using Rails AREL to do this and would like to enable chaining. This is the Ruby code that creates the query:
Item.select("name, rank() OVER (PARTITION BY user_id ORDER BY love_count DESC) AS position").where("position = 1")
Any ideas?
You need to "wrap" it into a derived table:
SELECT *
FROM (
SELECT name,
rank() OVER (PARTITION BY user_id ORDER BY love_count DESC) AS position
FROM items
) t
WHERE position = 1
My first thought was, "Use a common table expression", like this untested one.
WITH badly_named_cte AS (
SELECT name,
rank() OVER (PARTITION BY user_id
ORDER BY love_count DESC) AS position
FROM items
)
SELECT * FROM badly_named_cte WHERE position = 1;
The problem you're seeing has to do with the logical order of evaluation required by SQL standards. SQL has to act as if column aliases (arguments to the AS operator) don't exist until after the WHERE clause is evaluated.

Resources