Using custom expression when relating tables in SSAS - sql-server

In SSAS (Tabular model), I have 2 tables.
Student Grade
+-----------+-------+ +----------+--------+---------+
| StudentId | Score | | Title | LowEnd | HighEnd |
+-----------+-------+ +----------+--------+---------+
| 1 | 70 | | Intermed | 60 | 74 |
| 2 | 80 | | Good | 75 | 89 |
| 3 | 92 | | Excelent | 90 | 100 |
+-----------+-------+ +----------+--------+---------+
I want to relate Student table to Grade table, but there is no direct relation (FK), because I want to related tables using the LowEnd and HighEnd value in Grade table
In query representation, my desired join will be this:
select St.StudentId, Gr.Title
from Student St
inner join Grade Gr on St.Score between Gr.LowEnd and Gr.HighEnd
The question is: Is there any way to make such a relation in SSAS?

Related

Pivoting Data in SQL and Updting it into Destination Table

I am working on a Store Procedure where I need to update records from from table 1 into table 2.
Table1: contains the following 4 columns
----------------------------------------------
| ID | TableName | ColumnName | Value |
----------------------------------------------
| 1 | [UserDetails] | [FinalScore] | 92 |
| 2 | [UserDetails] | [FinalScore] | 89 |
| 3 | [UserDetails] | [FinalScore] | 65 |
| 4 | [UserDetails] | [FinalScore] | 91 |
| 5 | [UserDetails] | [FinalScore] | 76 |
| 1 |[EmployeeDetail]| [ScoreData] | 0.91 |
----------------------------------------------
UserDetails table
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 0 |
| 2 | Leonard J | +14581232342 | 0 |
| 3 | Max R | +17123992342 | 0 |
| 4 | Robert H | +15688992122 | 0 |
| 5 | Jimmy L | +1328996782 | 0 |
-----------------------------------------------------------
Need to load all the data from table1 into the corresponding destination table for large amount of data (30,000 to 60,000) records.
Table1 contains the ID (from ID Column in table1) and FinalScore (from ColumnName Column in table1) in the destination table (from TableName Column in table1) where the value (from Value Column in table1) needs to be loaded.
End Result of UserDetails table after SP execution :
-----------------------------------------------------------
| UserID | UserName | ContactNo | FinalScore |
-----------------------------------------------------------
| 1 | John G | +13288992342 | 92 |
| 2 | Leonard J | +14581232342 | 89 |
| 3 | Max R | +17123992342 | 65 |
| 4 | Robert H | +15688992122 | 91 |
| 5 | Jimmy L | +1328996782 | 76 |
-----------------------------------------------------------
I am not sure how to load the data from "table1" into destination table (userdetails table) for bulk data as update query in a loop is taking very long time to complete.
It's not pivot what you're after but a simple join:
select
a.userid, a.contactno, b.finalscore
from UserDetails a
join table1 b on a.id = b.id
alternatively, if you want to update the old table without creating a new one, you can update join
SQL update query using joins

TSQL count vs sum distinct values

I have a slightly confusing conundrum and I have been stuck all day.
I have the following types of data ...
For each customer record I have order numbers and for each order, I have a series of package numbers and for each package number, I have a possibility of zones... Normally the math would be relatively simple if there was 1 package with 1 or more zones we just select distinct amount of seats for example.
+-----------+-------+-----+------+-------+
| customer | order | pkg | zone | seats |
+-----------+-------+-----+------+-------+
| 1 | 1 | 11 | 7 | 2 |
| 1 | 1 | 12 | 7 | 2 |
+-----------+-------+-----+------+-------+
We know customer 1 has 2 seats per package.
Here is where it gets tricky
+----------+-------+-----+------+-------+
| customer | order | pkg | zone | seats |
+----------+-------+-----+------+-------+
| 2 | 3 | 8 | 5 | 2 |
| 2 | 3 | 9 | 5 | 2 |
| 2 | 3 | 10 | 5 | 2 |
-- In the above case we know a given customer has one order #3, with three packages in the same zone each package has two seats.
| 2 | 3 | 9 | 6 | 1 |
| 2 | 3 | 9 | 8 | 1 |
| 2 | 3 | 10 | 7 | 2 |
+----------+-------+-----+------+-------+
-- Here things are confusing because the same customer, has a single order #3 (and its possible
-- both scenarios occur in one single order) with two packages 9 and 10, package 9 has two zones
-- 1 and 1 and package 10 has one zones with two seats. how do we distinguish when we simply count
-- the seats like in the first/second occurrence or when we sum the seats like in the last example.
To reiterate a single customer would have a single order each order can have many packages in it with distinct package numbers each package can have 1 or more zones and each zone can have 1 or more seats.
When the zones are the same for a single package we simply count distinct. when a single order+package has more than one zone we sum we don't count.
I can't figure out how to code the logic. Please help.
My columns are customer_no, order_no, pkg_no, zone_no and pkg_seats.
Here is a real example
+----------+-------+-----+-------+------+
| customer | order | pkg | seats | zone |
+----------+-------+-----+-------+------+
| 374 | 876 | 68 | 2 | 26 |
| 374 | 876 | 68 | 1 | 32 |
| 374 | 876 | 68 | 1 | 56 |
| 374 | 876 | 71 | 2 | 56 |
| 374 | 876 | 71 | 2 | 79 |
| 862 | 538 | 71 | 2 | 33 |
| 862 | 538 | 71 | 1 | 81 |
| 862 | 538 | 71 | 1 | 82 |
-- In the below case we simply count 2. in the above we sum.
| 575 | 994 | 68 | 2 | 34 |
| 575 | 994 | 68 | 2 | 79 |
+----------+-------+-----+-------+------+
I should add one super confusing piece. We have a series of packages that are part of other packages. For example package 68, 70 and 71 are all together and the parent package is 68.
I can't figure out the grouping.
with data as (
select *,
min(zone_no) over
(partition by customer_no, order_no, pkg_no) as min_zone_no1,
min(zone_no) over
(partition by customer_no, order_no, pkg_no, pkg_seats) as min_zone_no2
from T
)
select
customer_no, order_no,
sum(case when zone_no = min_zone_no1 then pkg_seats end) as seat_total1,
sum(case when zone_no = min_zone_no2 then pkg_seats end) as seat_total2
from data
group by customer_no, order_no
order by customer_no, order_no;
I've poured over your description a few times and I'm still not certain I'm on the right track. You seem to have a problem of double-counting: essentially you want a sum, but some of the rows shouldn't be included. (To "count distinct seats" is likely the wrong nomenclature here.)
My approach above is to try and identify sets of rows that involve "duplicates" and some data that will assist in counting only one of them. I'm not sure what to make of order 876 which has different numbers of seats across the three zones.

Group By with Multiple Unique Constraints Condition

I need to fetch data from below mentioned table Sizes where data will be unique by Code,ArtId and Size OR unique By Code,ArtId and SizeIndex (i.e There are two unique constraints).How I can get unique records in single select statement using group by .
ArtId | SizeIndex | Size | Description | Code
001 | 000000000001111 | X | TEST | 01
002 | 000000000001111 | XL | NULL | 02
003 | 000000000001111 | L | NULL | 03
004 | 000000000009999 | SL | TEST2 | 04
005 | 000000000009999 | ML | LIGHT | 05
006 | 000000000009999 | M | Filter element,Air | 06
Your help will be highly appreciated
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE FROM
(SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROM TABLE GROUP BY CODE,ARTID,SIZE
UNION
SELECT ART_ID,SIZEiNDEX,SIZE, Description ,CODE
FROMM TABLE GROUP BY CODE,ARTID,SIZEINDEX)A

SQL Server. Join two tables n a view, take rows from one and turn into columns [duplicate]

This question already has answers here:
Efficiently convert rows to columns in sql server
(5 answers)
Closed 8 years ago.
I'm pretty new to SQL Server so don't really know what I'm doing with this. I have two tables, which might look like this:
table 1
| ID | customer | Date |
| 1 | company1 | 01/08/2014 |
| 2 | company2 | 10/08/2014 |
| 3 | company3 | 25/08/2014 |
table 2
| ID | Status | Days |
| 1 | New | 6 |
| 1 | In Work | 25 |
| 2 | New | 17 |
| 3 | New | 14 |
| 3 | In Work | 72 |
| 3 | Complete | 25 |
What I need to do is join based on the ID, and create new columns to show how long each ID has been in each status. Every time an order goes to a new status, a new line is added and the number of days is counted as in the 2nd table above. What I need to create from this, should look like this:
| ID | customer | Date | New | In Work | Complete |
| 1 | company1 | 01/08/2014 | 6 | 25 | |
| 2 | company2 | 10/08/2014 | 17 | | |
| 3 | company3 | 25/08/2014 | 14 | 72 | 25 |
So what do I need to to to create this?
Thanks for any help, as I say I'm pretty new to this.
I would suggest that AHiggins' link is a better candidate to mark this as a dupe rather than the one that's actually been selected because his link involves a join.
WITH [TimeTable] AS (
SELECT
T1.ID,
T1.[Date],
T2.[Status] AS [Status],
T2.[Days]
FROM
dbo.Table1 T1
INNER JOIN dbo.Table2 T2
ON T2.ID = T1.ID
)
SELECT *
FROM
[TimeTable]
PIVOT (MAX([Days]) FOR [Status] IN ([New], [Complete], [In Work])) TT
;

vb.net Comparing 2 Databases

I have Database1 and Database2, i add each of them to a datatable object.
now Database1 is changed through Access, Records maybe added removed or row data might change.
so the database Looks like
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductA | 453 | 55 |
| 2 | ProductB | 43 | 156 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
so if i Delete the row with Product ID=1 i want it to be deleted in the second Database2
if i add new product with ID = 4 i want it to be Added to Database2, also if i change Price or Sales i want it to change in Database2 for that record.
now the problem is the Database records are not in the same order in both databases so looping might take alot of time..for example if ID=4000 was removed than i've to loop through the whole Database to find this out.
so are thre anyother solutions than alot of loops?
It is possible to refer to another database in SQL run against an MS Access connection:
SELECT t1.*, t2.* FROM Table1 t1
INNER JOIN [;DATABASE=Z:\Docs\Test.accdb].Table1 t2
ON t1.ID=t2.ID
From here, it is easy enough to create queries to find missing or changed records.
If ID is the identifier for the row, then I do not think you can safely do what you are asking.
Given the example above for Table 1
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductA | 453 | 55 |
| 2 | ProductB | 43 | 156 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
And simulating the out of order condition for Table 2
+----+----------+-------+-------+
| ID | Name | Price | Sales |
+----+----------+-------+-------+
| 1 | ProductB | 43 | 156 |
| 2 | ProductA | 453 | 55 |
| 3 | ProductC | 22 | 161 |
+----+----------+-------+-------+
If you delete ID 2 from table 2, you will end up deleting Product B from Table 1 (not product A). I don't think this is what you want.
Are you sure that ID is the primary key for these values?

Resources