How to hardcode row in Select statement? - sql

Select 0 AS A, 1 AS B FROM someTable
Based on the above query, I can hardcode the number of column and the data regardless of what data is in someTable, and the number of rows is depending on the number of row in someTable.
I'm wondering what can I do if I want to hardcode the number of row as well?
For example if someTable have only 10 rows, how should I modify the above query so that I can have 1000 lines of records?

You can just keep cross joining your table:
SELECT TOP 1000 0 AS A, 1 AS B
FROM someTable a
CROSS JOIN someTable b
CROSS JOIN someTable c
CROSS JOIN someTable d;
I am assuming from the fact that you have tagged with SSMS this is SQL Server, If not you may need to use LIMIT
SELECT 0 AS A, 1 AS B
FROM someTable a
CROSS JOIN someTable b
CROSS JOIN someTable c
CROSS JOIN someTable d
LIMIT 1000;
The problem here is that if SomeTable only has 1 row, it won't matter how many times you cross join it, you will still only have one row. If you don't actually care about the values in the table, and only want to use it to generate rows then you could just use a system view that you know has more rows than you need (again assuming SQL Server):
SELECT TOP 1000 0 AS A, 1 AS B
FROM sys.all_objects a;
Even on an empty database sys.all_objects will have 2083 rows. If you might need more then just CROSS JOIN the views:
SELECT TOP 1000 0 AS A, 1 AS B
FROM sys.all_objects a
CROSS JOIN sys.all_objects b;
This will give you 4,338,889 rows.

Related

PostgreSQL cross join using max returns null

I want to select the max() value of the modified_at column in each of several tables:
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a, b, c;
This works correctly as long as each of the tables has at least 1 row.
The problem is that when just one of the tables has 0 rows, null is returned for all tables:
null,null,null
What is a solution that returns the correct values for the tables that do have rows?
PostgreSQL-10
Using OUTER JOINs should do it
select
max(a.modified_at) as a_modified_at,
max(b.modified_at) as b_modified_at,
max(c.modified_at) as c_modified_at
from a outer join b outer join c;
but a possibly simpler option would be to use 3 subqueries instead:
select
(select max(modified_at) from a) as a_modified_at,
(select max(modified_at) from b) as b_modified_at,
(select max(modified_at) from c) as c_modified_at;

Query is not returning the correct count while joining two tables in SQL Server 2017

Table A has 2500 rows
Table B has 12000 rows
Common column between these two tables is Email_ID.
Note: Email ID column may have nulls in table B.
If I join these tables using this query
Select count(*)
from A
left join B on A.Email_ID = B.Email_ID
it is returning a value of more than 2500 records.
But I want only 2500 records from Table A
Please suggest how to do this.
* will return total record count regardless of duplicate or nulls :
Select count(b.Email_ID) -- if you have a duplicate email ids then use `distinct`
from A left join
B
on A.Email_ID = B.Email_ID;
However, you can also use exists, join will return multiple rows if b table has more rows matching email id available in table a. So, count will always be increased.
select count(a.Email_ID)
from a
where exists (select 1 from b where b.Email_ID = a.Email_ID);
If you want only 2,500 hundred records, then why are you joining? That is, these return 2,500:
select count(*)
from A;
select count(distinct A.Email_id)
from A left join
B
on A.Email_ID = B.Email_ID;
If you actually want one row returned from B for each A, then use outer apply rather than left join:
select . . .
from a outer apply
(select top (1) b.*
from b
where b.email_id = a.email_id
) b;
This returns an arbitrary matching row from b. Normally, the subquery would have an order by to provide more control over the matching row. For instance, order by newid() would return a random row.

Query left join without all the right rows from B table

I have 2 tables, A and B.
I need all columns from A + 1 column from B in my select.
Unfortunately, B has multiples rows(all identicals) for 1 row in A
on the join condition.
I tried but I can't isolate one row in A for one row in B with left join for example while keeping my select.
How can I do this query ? Query in ORACLE SQL
Thanks in advance.
This is a good use for outer apply. The structure of the query looks like this:
select a.*, b.col
from a outer apply
(select top 1 b.col
from b
where b.? = a.?
) b;
Normally, you would only use top 1 with order by. In this case, it doesn't seem to make a difference which row you choose.
You can group by on all columns from A, and then use an aggregate (like max or min) to pick any of the identical B values:
select a.*
, b.min_col1
from TableA a
left join
(
select a_id
, min(col1) as min_col1
from TableB
group by
a_id
) b
on b.a_id = a.id

SQL Server double left join counts are different

Code:
Select a.x,
a.y,
b.p,
c.i
from table1 a left join table2 b on a.z=b.z
left join table3 on a.z=c.z;
When I am using the above code I am not getting the correct counts:
Table1 has 30 records.
After first left join I get 30 records but after 2nd left join I am getting 33 records.
I am having hard time figuring out why I am getting different counts. According to my understanding I should be getting 30 counts even after the 2nd left join.
Can anyone help me understand this difference?
I am using sql server 2012
There are multiple rows in table3 with the same z value.
You can find them by doing:
select z, count(*)
from table3
group by z
having count(*) >= 2
order by count(*) desc;
If you want at most one match, then outer apply can be useful:
Select a.x, a.y, b.p, c.i
from table1 a outer apply
(select top 1 b.*
from table2 b
where a.z = b.z
) b outer apply
(select top 1 c.*
from table3 c
where a.z = c.z
) c;
Of course, top 1 should be used with order by, but I don't know which row you want. And, this is probably a stop-gap; you should figure out why there are duplicates.
In your table table3 contain more then 1 row per 1 row in table1. Check one value which is occured more times in both tables.
You can use group by with max function to make one to one row.

Querying a table finding if child table's matching records exist in ANSI SQL

I have two tables A and B where there is one-to-many relationship.
Now I want some records from A and with this existence field that shows if B has any matching records. I don't want to use the count function as B has too many records that delays SQL execution. Either I don't want to use proprietary keywords like rownum of Oracle like below, as I need as much compatibility as possible.
select A.*, (
select 1 from B where ref_column = A.ref_column and rownum = 1
) existence
...
You would use left join + count anyway, select statement in select list can be executed multiple times while join will be done only once.
Also you can consider EXISTS:
select A.*, case when exists (
select 1 from B where ref_column = A.ref_column and rownum = 1
) then 1 else 0 end
Use an EXISTS clause. If the foreign key in B is indexed, performance should not be an issue.
SELECT *
FROM a
WHERE EXISTS (SELECT 1 FROM b WHERE b.a_id = a.id)