Use SUMMARIZECOLUMNS on table union in DAX query - dax

I am trying to write a DAX query that runs the SUMMARIZECOLUMNS function on a table variable. The table variable is the union of two tables that have the same columns in the same order.
When I try to run the query, I get a Cannot find table error. Here is the query I am trying to run:
EVALUATE
VAR u = UNION(Table1, Table2)
RETURN SUMMARIZECOLUMNS(u[CreationYear], u)
How can I run this query on the union of the two tables?

It's not very elegant, but in response to your comment on Marco's solution, you can do a count like this:
EVALUATE
VAR u = UNION(Table1, Table1)
RETURN SUMMARIZE(u, [CreationYear],
"Count",
COUNTX(
FILTER(u,
[CreationYear] = EARLIER([CreationYear])
),
[Id]
)
)

Try using SUMMARIZE in stead of SUMMARIZECOLUMNS. Like this:
EVALUATE
VAR u = UNION ( Table1, Table2 ) RETURN SUMMARIZE ( u, [CreationYear] )

Related

How to do union of multiple tables with where clause and order by in GORM (golang)

I'm trying to do union of multiple tables with selected columns and run where clause and order by clause on the resultset. How do I write this in GORM (Golang)
I tried the following snippet, but didn't run the where clause and order by clause in the DB query:
var union []map[string]interface{}
database.CONNECTION.Raw("? UNION ?",
database.CONNECTION.Select(ContentAttributes).Model(&model1{}),
database.CONNECTION.Select(ContentAttributes).Model(&model2{}),
).Where("id > ?", 1).Order("Name").Scan(&union)
N.B. ContentAttributes is a slice of string which contains the attributes I want to select.
It's running the following query:
SELECT "id","name","created_at","updated_at" FROM "model1" WHERE "model1"."deleted_at" IS NULL UNION SELECT "id","name","created_at","updated_at" FROM "model2" WHERE "model2"."deleted_at" IS NULL
I expected this to run the where condition and the order by clause on the union resultset. But it just did union and collected the results in the union variable. Please suggest a way to do this.
Not sure if this is the cleanest way to do it but it works.
var db = database.CONNECTION
var union []map[string]interface{}
var raw = "SELECT * (? UNION ?) union WHERE union.id > ? ORDER BY union.name"
db.Raw(raw,
db.Select("*").Model(&model1{}),
db.Select("*").Model(&model2{}),
1
).Scan(&union)

Using same table in subquery

I am failing to convert next SQL code into laravel eloquent:
SELECT t1.template, t1.created_at
FROM sent_emails t1
where created_at = (
select max(created_at) from sent_emails t2 where t2.template = t1.template
)
group by t1.created_at, t1.template
or:
SELECT t1.template, t1.created_at
FROM sent_emails t1
JOIN
(
SELECT Max(created_at) date, template
FROM sent_emails
GROUP BY template
) AS t2
ON t1.template = t2.template
AND t1.created_at = t2.date
group by t1.created_at, t1.template
Both queries return same data set. Creating subquery in separate variable is not an option as I need multiple values to be returned from it.
I also don't know how can I set alias name if I create table using models (and not using DB::), so this is my unsuccessful try:
$sent_emails = SentEmail::where('created_at', function($query) {
SentEmail::where('template', 't1.template')->orderBy('created_at', 'desc');
})->groupBy('template', 'created_at')->get(['template', 'created_at']);
You query should be something like this (I'm not at a computer to test this, so it may require further editing)
$sent_emails = SentEmail::where('created_at', function($query) {
$query->where('created_at', SentEmail::->whereColumn('column_name', 'table.column_name')->orderBy('created_at', 'desc')->max('created_at'));
})->groupBy('template', 'created_at')->get(['template', 'created_at']);

Mutiple Where subqueries in Hive doesn't work

I have a Query like below:
SELECT T.MTH_END_DT, T.SRC_SYS_CD, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
WHERE T.SRC_SYS_CD='SL'
AND T.MTH_END_DT in (SELECT(MAX(MTH_END_DT)) FROM PROD_RCRR.BAL_CNTRL_LOG)
AND T.BTCH_ID in (SELECT(MAX(BTCH_ID )) FROM PROD_RCRR.BAL_CNTRL_LOG)
A error message shows Hive only can support one "in" clause. Anyone can give me a solution?
You can replace the whole thing with Join ON clause
SELECT
T.MTH_END_DT
, T.SRC_SYS_CD
, T.BTCH_ID
FROM PROD_RCRR.BAL_CNTRL_LOG T
JOIN ( SELECT
MAX(MTH_END_DT) ENDT
, MAX(BTCH_ID ) BTCH
FROM PROD_RCRR.BAL_CNTRL_LOG ) X
ON T.SRC_SYS_CD='SL'
AND T.MTH_END_DT = X.ENDT
AND T.BTCH_ID = X.BTCH

How do I return a table in LINQ that relies on a join/subquery?

I need the fields in 1 table contingent on 1 property matching rows in another table.
I can write this query in SQL with a subquery as such:
SELECT *
FROM Table1
WHERE Property1 IN
(
SELECT Property1
FROM Table2
WHERE Property0 = 1
)
But I read here that it's less complicated and just as easy to write with a join, which I did. However, so far I'm unable to return just Table1 as I'd like since I'm using a join, which if I'm not mistaken, requires me to create this anonymous type, as below. What I did here works (I created another object with the same properties of Table1 I need), but I can't help thinking there's a better way to do this.
Table1.Join(Table2, t1 => t1.Property1, t2 => t2.Property1, (t1, t2) => new
{
t1.Property1,
t1.Property2,
t1.Property3
})
.Select(ob => new UnnecessaryObject
{
Property1 = ob.Property1,
Property2 = ob.Property2,
Property3 = ob.Property3
}
I also tried just creating a Table1 in the .Select part, but I got an error about explicit construction not being allowed.
Just to clarify, I'd like to be able to return the IQueryable of type Table1, which it seems like I ought to be able to do without having to create UnnecessaryObject...but I'm still pretty new to LINQ, so I'd appreciate any help you can offer. Thanks in advance.
You could just do:
from t1 in table1
join t2 in table2 on t1.property1 equals t2.property1
select t1;
That would return a collection of table1 objects. This assumes from your example table1 is a collection of table1 objects and table2 is a collection of table2 objects.
The best translation of your original query I can come up with is:
from item in context.Table1
where context.Table2
.Where(x => x.Property0 == 0)
.Any(x => x.Property1 == item.Property1)
select item
This selects all items from Table1, where there's an item with matching Property1 and Property0 == 0 from Table2
It can also be solved with a join indeed. To get an efficient join, you need to have a relation between the two tables. Then you can do something like assuming the relation is called RelatedItems:
from item in context.Table1
join relatedItem in item.RelatedItems
on item.Property1 equals relatedItem.Property
where relatedItem.Property0 == 0
select item
This is equivalent to the SQL:
SELECT *
FROM Table1
JOIN Table2 ON Table1.Property1 = Table2.Property1
WHERE Table2.Property0 = 0

nested subquery in linq to entities

Hi i want to know how to create nested subquery in linq to entities.
e.g. this is my sql subquery which i want to translate in linq.
SELECT
#planned = COUNT(ID)
FROM Task_Detail
WHERE task_id IN
(
SELECT
task_id
FROM Story_Task
WHERE is_testcase = 'true' AND
story_id IN
(
SELECT str_id FROM dbo.story
WHERE prjId = #pro_id AND
str_id IN
(
SELECT str_id FROM dbo.Sprint_StoryMapping WHERE sprint_id = #sprintid
) AND is_testcase = 'true'
)
) AND status = 1 AND
DATEPART(dd,workDate) = DATEPART(dd,#stdt)
Gut feel is that if you just turn this into a single select with the tables joined together, the whole query is going to run a lot faster and be easier to work with. Especially since your after an aggregate result.

Resources