Exclude Combination of Data Items From One Table From Another - tsql

I have a view, A, with 20 columns which forms my primary data. I have a table B which lists some of the columns from A and contains data I want to exclude from A.
For example table B will have 6 columns 2 of which are 'customer' and 'country' and contain the data 'HP' and 'America'. These columns exist in A. But I want to write a query that brings back data from A except where any rows that have a combination HP and America.
There are 6 columns and table B can have any combination of rows. Anywhere between 1 and all 6 rows could be filled in or there could be a row which has 5 columns filled in. Also another row with a different 5 columns filled in and so on.
I want to be prepared for any possible combination of the 6 rows and the query to search A for the combination and exclude any rows with that data from B.
I have tried this
SELECT *
FROM A T1
WHERE not EXISTS
(SELECT * FROM [dbo].[ExcludedItems] T2
WHere ReportNumber=1
AND
(
T1.job=ISNULL(T2.job,T1.job) and T1.CustomerName=ISNULL(T2.CustomerName,T1.CustomerName) and
T1.COUNTRY= ISNULL(T2.COUNTRY,T1.COUNTRY) and T1.CONTINENT=ISNULL(T2.CONTINENT,T1.CONTINENT) AND
T1.continer= ISNULL(T2.ContainerName, T1.continer) and T1.UnscheduledJob= ISNULL(T2.unscheduledJob, T1.UnscheduledJob) and
T1.[Price]= ISNULL(T2.Price, T1.Price) and
T1.[Haulage]= ISNULL(T2.[Haulage], T1.[Haulage]) and
T1.SiteAdress= ISNULL(T2.SiteAddress, T1.SiteAdress) and T1.Delta=ISNULL(T2.Delta, T1.Delta) and
T1.Cost= ISNULL(T2.Cost, T1.Cost)
)
)
The problem is the result set is not correct. I have tried with a smaller column sample and able to exclude the correct combination of Customer and Country but when I introduce a 3rd or 4th column combination I can eyeball the result set and immediately see its incorrect. Not sure if I have to use multiple NOT EXISTS for each possible combination, was hoping not to.
A constraint is A has to be a view not a table. Otherwise I would have used variables in some manner and wrapped the whole thing in a stored procedure.
Appreciate any help, fall back is to manually add to the code each time an item combination is supplied in B!

Related

Dynamically render multiple columns based on a drop-down list

I have a table in tableau with say 4 columns (Say Employee ID, Name, Address, Email).
I want to provide the users an option to select single/multiple columns from a drop down list (assume in the drop down list we have columns headers, Customfield1, Customfield2.....,Customfield10) which would be added to the table as a separate columns.
Say for Instance if the user selects CustomField1, CustomField2, CustomField3 from the drop-down list then the table should have in total of 7 columns (4 Emp fields + 3 Custom Fields)
Though I stated a simple requirement, my idea was to extrapolate that to something complex.
I think some version of this could be done, albeit in a hacky way. What you're looking for is 'Dynamic Parameters.' They don't technically officially exist in the product, but can be created with the combination of parameter and calculated field.
Here is a link to a method on such creation.
The idea here would be to re-order columns in a custom manner. Each calculated field would have to have its own logic for which dimension it was to display when a user selected a parameter. (ie: With parameter selection A Column 1 goes to [Name] and Column 2 goes to [Address], and with parameter selection B Column 1 goes to [Address] and Column 2 goes to [Name].) The caveat here is that each potential layout would need to have the same number of columns - there is not a way to add columns dynamically. Consider that each column is represented here with a calculated field.
Good luck on it.

Displaying only 1 row for duplicated data in tableau table

I have a table that displays 8 columns. Due to the way the data source is structured (I cannot change this) it sometimes means one or more of the columns will be the same across two rows.
I need to filter to show only one row of data. This is based on "email', so if an email has more than 1 row, I want one of the rows. If the other columns are the same or not doesn't matter.
I don't need to combine anything, and I don't care which row is displayed, I just need to remove one of the duplicated rows.
Replace this:
Place orderid email item name date
a 1 a#a.com b c 1/1/11
a 1 a#a.com d c 1/1/11
With this:
Place orderid email item name date
a 1 a#a.com b c 1/1/11
Or this:
Place orderid email item name date
a 1 a#a.com d c 1/1/11
Any help would be much appreciated! I had a go with LOD calculations and I couldn't make that do what I wanted (that may well be me not understanding how to use them properly, though).
Given your example, it appears the difference is within the [item] field. You can create an index calculation on it and filter for value of 1.
index()
Set the index to discrete and compute using the [item] and filter for only 1. You can hide the field if you don't want it to appear by deselecting Show Header.
If you want to show the first row per email, regardless of what's in the other fields, try the following:
Create an [Index] calc field using INDEX().
Add [Index] to rows shelf and change to discrete.
Edit the table calc for [Index], select 'Specific Dimensions' and restarting every email (dropdown menu). All of the checkboxes should be ticked by default.
Notice how the index is not restarting for each email as expected. To correct this, drag email checkbox to the top position.
From here, filter [Index] = 1 and hide index column from view.

Insert multiple records into fact table based on fields in single record

I'm working in Pentaho 4.4.1-GA (Kettle / PDI). The database is Postgres.
I need to be able to insert multiple records into a fact table based on the fields that come from a single record. The single record contains fields:
productcode1, price1
productcode2, price2
productcode3, price3
...
productcode10,price10
So if there was a value for each of the 10 productcode / prices then I'd need to insert a total of 10 records into the fact table. If there were values for 4 of the combinations, then I'd need to insert 4 records into the fact table, etcetera. All field values for the fact records would be identical except for the PK (generated by sequence), product codes, and prices.
I figure that I need some type of looping construct which would let me check whether or not a value was present for each productx field, and if so, do an insert/update step on the fact table with the desired field values. I'm just not sure how to do this in Pentaho.
Any ideas? All suggestions are welcome :)
Thank You,
Rakesh
Could you give a sample input and output for your scenario??
From your example data I can infer that if there are 10 different product codes and only 4 product prices you want to have 4 records inserted into your table. Is that so?
Well for a start you can add a constant value of 1 to those records by filtering for NOT NULL and then use an Group BY Step to count the number of 1's. This would give you the count. BTW it would be helpful if you could provide more details on what columns you would be loading as there are ways to make a PDI transformation execute multiple times

Showing 4 records in a portal from same table

I have a table that contains students' results. These results are generally broken into four types: term1, term2, term3 and term4. So over a year, a student may have up to four records in that table containing his results.
I want to create a layout that contain a portal that will show all the 4 records in a single portal row. Is there any way to do this? Or any workaround?
The reason why I do not want to display the records as four rows in the portal is because there are different subjects and will not be right if each subject occupy four rows and there are many subjects a student may take.
I can think of two ways to approach this, both of which would require a relationship from your Results table occurrence to another table occurrence based on Results, let's call it Results~SameStudentID. (The matching field would the foreign key to the Student table, FK_StudentID = FK_StudentID.)
Create 4 calculation fields in your Results table: Result_1, Result_2, Result_n, etc. The formula to use for each of the calculation (starting from the context of the Results table occurrence) would be:
GetNthRecord ( Results~SameStudentID::Result ; n )
Then, simply include the 4 "Result_n" fields in your portal
Create just one field, Results_1_4, with the following formula:
Substitute ( List ( Results~SameStudentID::Result ) ; ΒΆ ; " " )

I need to know if it is possible to create a SSRS report as defined

I have a query with 3 parameters that a user should be able to define:
AND (ORION_SCHED.TRIP.DATE_TIME = '09/11/2012')
AND (ORION.CUST.COUNTRY = 'BE')
AND (ORION_SCHED.TRIP.ID_SHIFT ='1')
DATE_TIME should be a datepicker
COUNTRY dropdown with defined values
ID_SHIFT dropdown with defined values
I guess this isn't an issue?
The result of this query will return the dataset
My actual question: I want the result to be presented in this way
So always 3 blocks of data next to each other, the data of every block is the result of the query filtered down on the column TRUCK_ID (a sub-select of the returned dataset)
The empty cells per block are a nice to have, these are non-unique values per TRUCK_ID that I don't wish to be repeated on every line, but like I said a nice to have.
I would add a new column to the query that returned 1,2 or 3 (for each different TRUCK_ID. Then I would use 3 Tablix (one per column on your report) and filter the data by the new column you added on the query.
Then you just group on the TruckID on each tablix and play around with the format. It should work.