Left joining right aligned field with left aligned field in Crystal - crystal-reports

I have a report where I need to join two tables but the fields are defined differently between them and I cannot change the table schemas because its JDEdwards. So I have one field as 30 characters left aligned and another another as 12 characters right aligned. The values will always be under 12 characters. So the issue is not the difference in size but the alignment/padding. I have to do a left join though. Right now the report is just comparing in the data selection and causing an inner join. But I need to change it to left join to stop losing rows. Is there any way to do it?
What happens is when I create the link to do the left join in Database Expert, I end up getting the data from the joined table all blank due to it not finding any rows because of the padding difference.

Since you only need one value from the table to be joined you can instead create a single SQL Expression to get the value for you without the need to add any tables to the report itself via Database Expert. You'll be able to use any valid SQL so you can do pretty much whatever you want without being constrained by Crystal.
For example, say your report consists of Table_A with a foreign key that is 30-characters left-aligned and you're trying to join to Table_B to get at some field, but that key is 12 characters right-aligned. A simple example in Oracle would be something like this:
case when "Table_A"."ForeignKey" is null then null
else
(select Table_B.SomeFieldYouWant
from Table_B
where rpad(Table_B.PrimaryKey,30,'0')="Table_A"."ForeignKey"
end
It's important to note that in a SQL Expression, any fields referenced with double quotes means that it's referring back to tables/fields in your report instead of new tables in the subquery; this is how you relate the subquery back to your report's data.

Related

Snowflake invalid identifier when performin a join

I have been trying to do an outer join across two different tables in two different schemas. I am trying to filter out before from the table variants the character that are smaller than 4 and bigger than 5 digits. The join was not working with a simply where clause in the end, hence this decision.
The problem is if I do not put the quotes, Snowflake will say that I put invalid identifiers. However, when I run this with the quotes, it works but I get as values in the fields of the column raw.stitch_heroku.spree_variants.SKU only named as the column name, all across the table!
SELECT
analytics.dbt_lcasucci.product_category.product_description,
'raw.stitch_heroku.spree_variants.SKU'
FROM analytics.dbt_lcasucci.product_category
LEFT JOIN (
SELECT * FROM raw.stitch_heroku.spree_variants
WHERE LENGTH('raw.stitch_heroku.spree_variants.SKU')<=5
and LENGTH('raw.stitch_heroku.spree_variants.SKU')>=4
) ON 'analytics.dbt_lcasucci.product_category.product_id'
= 'raw.stitch_heroku.spree_variants.SKU'
Is there a way to work this around? I am confused and have not found this issue on forums yet!
thx in advance
firstly single quote define a string literal 'this is text' where as double quotes are table/column names "this_is_a_table_name"
add alias's to the tables makes the SQL more readable, and the duplicate length command can be reduced with a between, thus this should work better:
SELECT pc.product_description,
sp.SKU
FROM analytics.dbt_lcasucci.product_category AS PC
LEFT JOIN (
SELECT SKU
FROM raw.stitch_heroku.spree_variants
WHERE LENGTH(SKU) BETWEEN 4 AND 5
) AS sp
ON pc.product_id = sp.SKU;
So I reduced the sub-selects results as you only used sku from sp but given you are comparing product_id to sku as your example exists you don't need to join to sp.
the invalid indentifiers implies to me something is named incorrectly, the first step there is to check the tables exist and the columns are named as you expect and the type of the columns are the same for the JOIN x ON y clause via:
describe table analytics.dbt_lcasucci.product_category;
describe table raw.stitch_heroku.spree_variants;

postgres: left join by unknown table - column value

I know this is a stupid question, but I'm gotta ask away.
I'm in a situation where the db I'm working on doesn't have a "backwards" relation to another table.
Let's say I have three tables - let's call them type tables, rt, wt, and ret.
Each of those tables has a group_id.
Each group table has type which may contain rt, wt, or ret.
The normal procedure for selection is select from a type table and then left join group on group_id.
I'm in a situation where I need to select in the reverse order, but I don't know which table I'm to be selecting from at run time. All I have is the type.
Is there such a thing as selecting a table by a column value? such as
SELECT * FROM group LEFT JOIN `group.type` ON `group.type`.group_id = group.id
In my case, I just did multiple left joins, of course only one of them stuck.

differing column names in self-outer-joins

When writing a self-join in tSQL I can avoid duplicate column names thus:
SELECT FirstEvent.Title AS FirstTitle, SecondEvent.Title AS FirstTitle
FROM ContiguatedEvents AS FirstEvent
LEFT OUTER JOIN ContiguatedEvents AS SecondEvent
ON FirstEvent.logID = SecondEvent.logID
Suppose I want to select all the columns from the self-join, for example into a view. How do I then differentiate the column names without writing each one out in the join statement. I.e. is there anything I can write like this (ish)
SELECT FirstEvent.* AS ???, SecondEvent.* AS ???
FROM ContiguatedEvents AS FirstEvent
LEFT OUTER JOIN ContiguatedEvents AS SecondEvent
ON FirstEvent.logID = SecondEvent.logID
There's no way to automatically introduce aliases for multiple columns, you just have to do it by hand.
One handy hint for quickly getting all of the column names into your query (in management studio) is to drag the Columns folder from the Object Explorer into a query window. It gives you all of the column names.

T-SQL LEFT JOIN on bigint id return only ids lower than 101 on right table

I have two tables on a Sql Server 2008.
ownership with 3 fields and case with another 3 fields I need to join both on the ID field (bigint).
For testing purposes I'm only using one field from each table. This field is bigint and has values from 1 to 170 (for now).
My query is:
SELECT DISTINCT
ownership.fCase,
case.id
FROM
ownership LEFT JOIN case ON (case.id=ownership.fCase)
WHERE
ownership.dUser='demo'
This was expected to return 4 rows with the same values on both columns. Problem is that the last row of the right table comes as null for the fCase = 140. This is the only value above 100.
If I run the query without the WHERE clause it show all rows on the left table but the values on the right only apear if below 101 otherwise shows null.
Can someone help me, am I doing something wrong or is this a limitation or a bug?
Case is also a verb so it may be getting confused. Try your table and column names in []. E.G. [case].[id] = [ownership].[fCase]. Are you like double check sure that [case].[id] and [ownership].[fCase] are both bigint. If your current values are 1-170 then why bigint (9,223,372,036,854,775,807)? Does that column accept nulls?

sql query to retrieve DISTINCT rows on left join

I am developing a t-sql query to return left join of two tables, but when I just select records from Table A, it gives me only 2 records. The problem though is when I left join it Table B, it gives me 4 records. How can I reduce this to just 2 records?
One problem though is that I am only aware of one PK/FK to link these two tables.
The field you are using for the join must exist more than once in table B - this is why multiple rows are being returned in the join. In order to reduce the row count you will have to either add further fields to the join, or add a where clause to filter out rows not required.
Alternatively you could use a GROUP BY statement to group the rows up, but this may not be what you need.
Remember that the left join brings you null fields from joined table.
Also you can use select(distinct), but i can't see well you issue. Can you give us more details?