Power BI DAX group by , min max for TrxID - sql-server

I'm having trouble trying to obtain summary transaction header level data from my line details data.
I have sales item data with a DateTime for each row added to the table. I want to pull the min and max values of this datetime column BY EACH transaction ID.
Hoping to do this in DAX as a virtual table or measure without going to SQL server again.
Thanks for your help
Sample data.
Desired Result

I you need a new table created by DAX, use this code:
NewTable = ADDCOLUMNS(
SUMMARIZE('Table','Table'[TrxID]),
"MinDateTime", CALCULATE(min('Table'[LineDateTime])),
"MaxDateTime", CALCULATE(max('Table'[LineDateTime]))
)

Related

How to get row count of VIEW table along with Metadata information in Snowflake?

I would like to get row count of VIEW table in Snowflake along with metadata information such as TABLE_SCHEMA,TABLE_NAME,CREATED,LAST_ALTERED columns from INFORMATION_SCHEMA for the specific VIEW table.Let say my VIEW table name is "V_TEST"
I can write SELECT COUNT(*) FROM DB.SCHEMA.V_TEST and get a row count for VIEW table but not sure how to merge in the table with metadata table ?
I can get TABLE_SCHEMA,TABLE_NAME,CREATED,LAST_CREATED using below code
SELECT TABLE_SCHEMA,TABLE_NAME,CREATED,LAST_CREATED FROM DB.Information_SCHEMA.VIEWS
WHERE TABLE_NAME='V_TEST'
Thanks in advance for your help and efforts!

Updating local Sql Server Database from Cloud Snowflake Server fastest way to get new data

I have a local SQL Server DB table with about 5 million records.
I snowflake server that has a similar table that is updated daily.
I need to update my local table with the new records that are added on the Snowflake table.
This code works but it takes about an hour to retrieve about 200,000 records. I insert the records into a local temp table and then insert them into my Sql server db.
Is there a faster way to retrieve the records from Snowflake and get them into SQL Server?
TIA
JohnB
SELECT A.*
into #Sale2020New
FROM OPENQUERY(SNOW, 'SELECT * FROM "DATA"."PUBLIC"."Sales" where "Sales"."Date" >= ''1/1/2020'' and "Sales"."Date" <= ''12/31/2020'' ') A
Left JOIN [SnowFlake].[dbo].Sale2020 B
ON B.PrimaryKey = A.PrimaryKey
WHERE
b.PrimaryKey IS NULL;
Does it take 1 hour just retrieving data from Snowflake or the whole process?
To speed up data retrieval from Snowflake, implement clustering on DATE column in snowflake table. This would prune micropartitions and avoid full table scan. You can get more information on clustering here
As for delta load, instead of a join you can apply filter on DATE column to current date and this will avoid a costly join operation and filter data at the start.
SELECT * FROM "SALES"
where "Sales"."Date" = '2020-04-07'

SQL Server: Get the last row entries (more than one) with sql query

Scenario: a user will copy and paste data (multiple rows) from an Excel sheet onto my webpage and press submit. When this occurs, the data will be saved into a SQL Server table. The current date will also be saved next to each row.
Now, in another gridview, I would like to view only these multiple rows that have been pasted /saved to DB that certain day.
So I was thinking about using TOP / MAX(date) but Top returns specified rows only, and MAX only 1 row.
Anyone out there that has done this before or can help get a working query?
Use TOP WITH TIES in order to get all last entries:
SELECT TOP(1) WITH TIES
...
ORDER BY submit_date DESC;
Is "that certain day" based on a specific day or a 24 hour interval?
You can make the gridview query the data where the date field is higher than or equal to dateadd(dd, -1, getdate())
Or if you mean the current day as in the current date, where the date is equal to the date of getdate.

indexing datetime column on a very wide table

I have table with about 400 columns and 4 million rows in SQL Server 2012.
the only purpose of this table to be used by a reporting tool. this table is refreshed(dropped and recreated) every night via scheduled Job. so no update/insert/delete.
there is a Date column with Datetime as datatype. I have created a clustered index on this date column but it only seemed to help a little.(there wont be any other conditions on where clause so I haven't included any other columns in the index)
the query send by reporting tool is like
select *(all columns listed)
from mytable
where date>='01/01/2010' and date <='12/01/2010'
it takes about 10 mins to retrieve all that falls under above date range which is about a million rows.
I need to get this under a minute if I can or the best I can.
if I can get some idea that might help me to achieve this . I would greatly appreciate it.
I have tried following but no significant performance gain.
-change datatype to 'Date'/'varchar'/'int' from 'Datetime'
-create nonclustered index on same column
-create clustered/nonclustered index including other columns to make it unique
100 million rows may just plain be a data volume thing.
Try:
select count(*) from mytable where date>='01/01/2010' and date <='12/01/2010'
If that is fast then it is not an index issue

SQL Server indexed calculated column that sums another table

I'd like to effectively add a calculated column, which sums a column from selected rows in another table. I need to to quickly retrieve and search for values in the calculated column without re-computing the sum.
The calculated column I'd like to add would look like this in Dream-SQL:
ALTER TABLE Invoices ADD Balance
AS SUM(Transactions.Amount) WHERE Transactions.InvoiceId = Invoices.Id
Of course, this doesn't work. My understanding is that you can't add a calculated column that references another table. However, it appears that an indexed view can contain such a column.
The project is based on Entity Framework Code First. The application needs to quickly find non-zero balances.
Assuming an indexed view is the way to go, what is the best approach to integrating it with the Invoices and Transactions tables to make it easy use with LINQ to Entities? Should the indexed view contain all the columns in the Invoices table or just the Balance (what gets persisted)? A code snippet of the SQL to create the recommended view and index would be helpful.
An indexed view won't work because it would only index expressions in the GROUP BY clause, which means it can't index the sum. A computed column won't work because the sum can't be persisted or indexed.
A trigger works, however:
CREATE TRIGGER UpdateInvoiceBalance ON Transactions AFTER INSERT, UPDATE AS
IF UPDATE(Amount) BEGIN
SET NOCOUNT ON;
WITH InvoiceBalances AS (
SELECT Transactions.InvoiceId, SUM(Transactions.Amount) AS Balance
FROM Transactions
JOIN inserted ON Transactions.InvoiceId = inserted.InvoiceId
GROUP BY Transactions.InvoiceId)
UPDATE Invoices
SET Balance = InvoiceBalances.Balance
FROM InvoiceBalances
WHERE Invoices.Id = InvoiceBalances.InvoiceId
END
It also helps to provide a default value of 0 for the Balance column since when you mark it as DatabaseGeneratedOption.Computed, EF won't provide any value for it when adding an Invoice row.

Resources