Error loading multiple tables in Qlik Sense - concatenation

I'm pulling in three tables into Qlik Sense, of which two are basically same tables with one filter difference (more than 4 counts, less than 4 counts) that I load with adding new filter field, as such:
Cohort1:
Load
*
, 'LessThan4' as Cohort;
Cohort2:
Load
*
, 'MoreThan4' as Cohort;
I do so because I need Cohort to be my filter for the entire app.
Then I have the third table, that is separate data that I need to outer join. All three tables have same id field that should tie each other, but I can't figure out right way to join them. I thought all three tables having same id field (named same) should be auto-concatenated, but it didn't. I've only done concatenating two tables, but to force concatenate all three tables, what is the right code?

Qlik will automatically link the tables having a common fields (named the same). This link will act as join and the result will be multiple tables. Concatenate will result in multiple tables being append to one another and the final result will be one table.
If you want to have one table will all the records in it have a look into Concatenating tables documentation
The code below will show you how to perform force contatenation:
Table1:
Field1,
Field2
From my-data.qvd;
Concatenate(Table1)
Table2:
Field1,
Field3
From my-data1.qvd;
The result table will have 3 fields: Field1, Field2, Field3
Side note: Qlik will auto-concatenate tables having exactly the same set of fields. To avoid this have a look at the NoConcatenate statement

Related

Access tables multiple join issue

Can I use two joins between two tables in Access database?
I have a customer database, and my customer names appear in two different fields and I want to convert customer names into the short names and return that in a query in one single field.
In attempt to solve that I have created a second table with all the customer names and their abbreviations then linked "CustName" field with the "Customer_Name" field in my main table, in my query I am returning the short names of my customers. The struggle is that some customer names e.g Toyota appear in "customer_Plant" field instead of "customer_Name" field (please see picture). I want to use different Toyota shortnames by each plant location. Another difficulty is that the "Customer_Plant" field in my original table is not always populated, except for Toyota.
Is there any way I can use multiple relations between two different tables? so that access query can return short names, not just by "customer_Name" but also by "Customer_Plant" at the same time.
Access does not allow me to join "Customer_Plant" with "custPlant" if one join is present between the tables. Is there any other way I can achieve this?
Tbl_claimdata & tbl_custShortName:
Join between the tables:
Current Output:
If Plant name is not provided in either or both tables, consider:
Query1: Claims_ADJ
SELECT tblCustClaimData.Customer_Name,
tblCustClaimData.Customer_Plant, Nz([Customer_Plant],[Customer_Name])
AS LinkNameClaim FROM tblCustClaimData;
Query2: Short_ADJ
SELECT tblShortCustName.CustName, tblShortCustName.PlantName,
Nz([PlantName],[CustName]) AS LinkNameShort,
tblShortCustName.ShortName FROM tblShortCustName;
Query3:
SELECT Customer_Name, Customer_Plant, ShortName FROM Short_ADJ RIGHT
JOIN Claims_ADJ ON Short_ADJ.LinkNameShort = Claims_ADJ.LinkNameClaim;
Query3 is not updatable so probably useful only for a report.
So alternative is DLookup in query (queries 2 and 3 not needed) or textbox:
DLookUp("ShortName","tblShortCustName","Nz([PlantName],[CustName])='"
& Nz([Customer_Plant],[Customer_Name]) & "'")

SQL equivalent query in Qlikview?

In SQL we can write query like:
Select field1,field2,field3,field4,field5,field6,field7
from table1 t1,table2 t2,table3 t3
where t1.field1 = t3.field3 and
t2.field2 = 'USD'
In Qlikview, I have created QVD's for 6 tables, now I want to create a single QVD of these 6 QVD's. Unfortunately these tables don't contain primary keys. So I cant use join. I have tried like this also:
fact:
load *
from
[D:\path\fact*.qvd](qvd);
//To store all qvd's into one qvd.
store fact into [D:\path\facttable.qvd];
This query creates a facttable but only with 2 columns, these columns are of first fact table. Diagram shows it much clear:
As it internally gives the name of all the facts table with fact, fact-1, fact-2 and so on and I have written the query store fact into [D:\path\facttable.qvd]; and in this diagram fact table contains only two columns so it creates the fact table with two columns only.
Please let me know the solution that how can we write this query in Qlikview or how can we create a fact table using all the QVDS?
Thanks in advance.
Since every qvd contains different field names it will create several tables with synthetic keys when you load *.
You can use Concatenate Load to stack each qvd onto one fact table. One simple example would be to first create a Fact table by:
Fact:
Load * INLINE [
dummyField
];
Now you can concatenate the qvd's onto that Fact table:
concatenate(Fact)
load *
from
[D:\path\fact*.qvd](qvd);
//To store all qvd's into one qvd.
store Fact into [D:\path\facttable.qvd];
//drop the dummy field.
drop field dummyField;

Relational database design tables with common base

I have a lot trouble finding the best design solution for this situation. I have two tables with a common base. Currently I have designed it like this: I have an order table (the common base):
[order_table]
order_id
order_type
company
created
I have another table with reference to the order table:
[product_order]
order_id fk
product_id
quantity
price
I have second table with reference to the order table:
[special_order]
order_id fk
description
price_estimate
color
size
Both tables share the same order_id which i like. I often have to do large queries on order_table using the information available in that table lets say 'company = 200'. But for each result I also need its data from product_order or special_order depending on which type it is. So the only optimal solution I see is to left joining the query with both tables on order_id and filter the information afterwards. The only other option I see is to add the common columns to each table, but then I would have a lot of reorganizing afterwards to get them in correct order.
Is there a better way to organize the data?
So those extra tables are extra attributes to a specific order-id (1:1)?
I'd consider adding all the fields to the common tables, or at least the fields from the most used sub-table.
If not appropriate, you may want to add "Type" to the common table and let a trigger manage insert/delete of related records to avoid the fuzz with orphans etc.
Use views with your left joins (wouldn't inner be better?) to fetch the different types.

SQL Server Select Query

I have to write a query to get the following data as result.
I have four columns in my database. ID is not null, all others can have null values.
EMP_ID EMP_FIRST_NAME EMP_LAST_NAME EMP_PHONE
1 John Williams +123456789
2 Rodney +124568937
3 Jackson +124578963
4 Joyce Nancy
Now I have to write a query which returns the columns which are not null.
I do not want to specify the column name in my query.
I mean, I want to use SELECT * FROM TABLE WHERE - and add the filter, but I do not want to specify the column name after the WHERE clause.
This question may be foolish but correct me wherever necessary. I'm new to SQL and working on a project with c# and sql.
Why I do not want to use the column name because, I have more than 250 columns and 1500 rows. Now if I select any row, at least one column will have null value. I want to select the row, but the column which has null values for that particular row should not appear in the result.
Please advice. Thank you in advance.
Regards,
Vinay S
Every row returned from a SQL query must contain exactly the same columns as the other rows in the set. There is no way to select only those columns which do not return null unless all of the results in the set have the same null columns and you specify that in your select clause (not your where clause).
To Anders Abels's comment on your question, you could avoid a good deal of the query complexity by separating your data into tables which serve common purposes (called normalizing).
For example, you could put names in one table (Employee_ID, First_Name, Last_Name, Middle_Name, Title), places in another (Address_ID, Address_Name, Street, City, State), relationships in another, then tiny 2-4 column tables which link them all together. Structuring your data this way avoids duplication of individual facts, like, "who is John Williams's supervisor and how do I contact that person."
Your question reads:
I want to get all the columns that don't have a null value.
And at the same time:
But I don't want to specify column names in the WHERE clause.
These are conflicting goals. Your only option is to use the sys.tables and sys.columns DMVs to build a series of dynamic SQL statements. In the end, this is going to be more work that just writing one query by hand the first time.
You can do this with a dynamic PIVOT / UNPIVOT approach, assuming your version of SQL Server supports it (you'll need SQL Server 2005 or better), which would be based on the concepts found in these links:
Dynamic Pivot
PIVOT / UNPIVOT
Effectively, you'll select a row, transform your columns into rows in a pivot table, filter out the NULL entries, and then unpivot it back into a single row. It's going to be ugly and complex code, though.

Merge data object table with associated attributes table in a view

Here's the setup: I have several tables that hold information for data objects which have the potential to have various and sundry bits of data associated with them. Each of these tables has an associated attributes table, which holds 3 bits of information:
the id (integer) of the row the attribute is associated with
a short attribute name ( < 50 chars )
a value (varchar)
The object table will have any number of columns of varying data types, but will always have an integer primary key. If possible, I would like to set up a view that will allow me to select a row from the object table, and all of its associated attributes at one go.
****EDIT****
Ideally, the form I'd like this to take is having columns in the view with the names of the matched attribute from the attributes table, and the value as the value of the attribute.
So for example, if I have table Foo with columns 'Bar', 'Bat', and 'Baz' the view would have those columns, and additionally, columns for any attributes that a row might have.
****END EDIT****
Now, I know (or think I do) that SQL doesn't allow using variables as an alias for a column name. Is there a clean, practical way of doing what I want, or am I chasing a pipe dream?
The obvious solution is to handle all of this in the application code, but I'm curious if it can be done in SQL.
The answer depends on what you are actually seeking. Will the output of the view have one row per attribute per object or one column per attribute per object? If the former, then I'm not sure why you need a view:
Select ...
From ObjectTable
Join AttributeTable
On AttributeTable.Id = ObjectTable.Id
However, I suspect what you want is the later or something like:
Select ...
, ... As Attribute1
, ... As Attribute2
, ... As Attribute3
...
From ObjectTable
In this scenario, the columns that would be generated are not known at execution because the attribute names are dynamic. This is commonly known as a dynamic crosstab. In general, the SQL language is not designed for dynamic column generation. The only way to do this in T-SQL is to use some fugly dynamic SQL. Thus, it is better done in a reporting tool or in middle-tier code.
It sounds like you want a view for each of your 'object' tables as well as its 'attributes' table. Correct me if I am wrong in my reading. It's not clear what your intentions are with 'using variables as an alias for a column name'. Were you hoping to merge ALL your objects, with their different columns, into one view?
Suggest create one view per entity table, and join to its relevant 'attributes' table.
Question though - why is there one matching attributes table for each entity table? Why are they split out? Perhaps you've made the question simpler or obfuscated, so perhaps my question is rhetorical.
CREATE VIEW Foo AS
SELECT O.ID
,O.EverythingElse
,A.ShortName
,A.SomeVarcharValue
FROM
ObjectTable AS O --customer, invoice, whathaveyou
INNER JOIN
ObjectAttribute AS A ON A.ObjectID = O.ID
To consume from this, you could:
SELECT * FROM Foo WHERE ID = 4 OR
SELECT * FROM Foo WHERE ShortName = 'Ender'

Resources