Calculated Column Count in SAP HANA - data-modeling

I have a table with fields ID, Status and Changed Date like below:
Table
How to create a Calculated Column wherein I should the count like:
Open = 1
In progress = 1
Completed = 1
i.e., we should get the final status count of the ID.
Instead I'm getting the count like:
Open = 3
Inprogress = 2
Completed = 1

You may want to look at the answer for SQL query to fetch OrderID, transactionID, Status based on transaction status which is Char which is basically your requirement.

Related

How to force Query engine to return data from right partition without .$Partition function ?

We partitioned a table based on IsActive(bit) flag. Almost %70 out of 500k rows are current and we wanted to write old and new rows in 2 different partitions. However when I want to return data from the table I see difference in execution time and I/O cost when I run these two queries.
SELECT [columns] FROM TableX
WHERE $PARTITION.partition_function_name(IsActive) = 2 -- partition has active records.
SELECT [columns] FROM TableX
WHERE IsActive = 1 -- Returns active rows
Is there a way to avoid using $PARTITION.partition_function_name(IsActive) = 2
statement. I was assuming since table partitioned by IsActive column it should automatically read from right partition and return me with less cost.
Execution Plan Details
With $PARTITION.partition_function_name(IsActive) = 2
with IsActive = 1

Getting Expired Purchases And Associated Features

I am updating our in-house purchase system. While doing this, I had to move the service expiration date from the client table to the purchase table so each system could expire separately. I am trying to update the SQL statement to get systems that need to be billed, but am running into issues. Below is a simplified example of what I need:
Purchase Table
ID Product ClientID SystemID IsSystem ExpireDate
1 System1 12 1 1 9/22/2015
2 Feature1 12 1 0 NULL
3 Feature2 12 1 0 9/22/2016
4 System2 12 2 1 9/22/2016
5 Feature1 12 2 0 9/22/2015
6 Feature2 12 2 0 NULL
In the example above, I would need to get lines 1, 2 and 3. What I need to do is:
Look at each system to see if the ExpireDate is less than or equal to a given date (say today)
If it is, take all rows with a matching ClientID and SystemID reguardless of ExpireDate
Although line 5 would be expired, it would not get included because the system it is a part of has not expired.
This is getting inserted into a much larger SQL statement spanning several tables, but I should be able to extrapolate from an example with this table.
This is probably best accomplished using a common table expression that gets the expired client and systemIDs and then checks every row in your table against that list:
WITH expired AS
(
SELECT DISTINCT
ClientID,
SystemID
FROM purchase
WHERE
CONVERT(DATE, ExpireDate) <= CONVERT(DATE, GETDATE())
AND isSystem = 1
)
SELECT
*
FROM purchase p
WHERE
EXISTS (SELECT * FROM expired WHERE p.ClientId = ClientID AND p.SystemId = SystemId)

SQL scheduled jobs Failed Notification

I need idea or a way to check if my schedule jobs work and insert the right data. I Have a function that call a stored procedure to count inserted data from today and insert into specific field.
like this
SET #male = (select count(idUser) from (select idUser from #tmpLog2 where sex = 1 AND CAST(catchTime as date) = #DATE group by idUser)u);
SET #female = (select count(idUser) from (select idUser from #tmpLog2 where sex = 0 AND CAST(catchTime as date) = #DATE group by idUser)u);
INSERT INTO CatchLog
(
[male],
[female]
)
VALUES
(
#male,
#female
)
the stored procedure works OK, but sometime when today have a lot of data, the result inserted 0 for male / female.
It's possible to have 0 data inserted when today data really no male / female.
but sometime it inserted 0 but there are male n female data. Anyone can help me to check, how to check if the data inserted true and give some report or insert into error Log if data inserted not true?
sorry for my bad english
I dont know what column are you using into Count() Function. But if you are using Nullable column into it then it may gives 0 even if record were added as column value is null.
Consider to use Primary key column or not Nullable column in count() argument.

Inserting one table's complete column data to a particular column in another table in SQL Server

Inserting one table's complete column data to a particular column in another table in SQL SERVER
I have two tables i.e AuditCalendar, ScheduleAudit
Audit Calendar has two columns Taskid, TaskTypeId
Schedule Audit has two columns Scheduleid, Taskid
Audit Calendar looks like this
Taskid (Auto increment) TaskTypeId
-------------------------------------
1 1
2 2
3 3
4 1
5 1
But I want Taskid column data from Audit Calendar table based on TaskTypeId .Columns
After completion of query, the ScheduleAudit table should look like this
Scheuleid (AutoIncrement) Taskid
-------------------------------------
1 1
2 1
3 1
I have to run this query seems to look like a error
Subquery returns more than one value
Query is:
INSERT INTO ScheduleAudit(TaskId)
VALUES ((SELECT TaskId FROM AuditCalendar Where TaskTypeId = 1))
Please can you suggest how I can do this approach I am new to SQL Server but someone says that use cursors.... I am really confused last 1 week on words. And also search google but not get it now...Please can you give me any one valuable suggestions.
insert ... values is supposed to insert a single row. So what you have in the parentheses is supposed to produce a single row, or else it would fail.
There's no need to use insert ... values, when you can use insert ... select:
INSERT INTO ScheduleAudit(TaskId)
SELECT TaskId FROM AuditCalendar Where TaskTypeId=1
...however, that would produce
1 1
2 4
3 5
I'm not sure I understand the logic behind producing your example output.

Conditional Count and group by with realtime data

I was able to find similar and close-match questions regarding my query. But I want to know if there are better ways of doing the same.
I have a database table which is being populated by call to a web-service end-point. The end-point is called when certain event occurs and a new record is inserted to this table. The events take place in real-time and there is no set frequency or pattern of occurrence of the events.
The table looks like:
CREATE TABLE MyTbl(id int, type nvarchar(10), timestamp datetime, category nvarchar(50));
I am fetching data from the table as:
SELECT category,
COUNT(CASE WHEN type = 'sent' THEN 1 END) sent,
COUNT(CASE WHEN type = 'received' THEN 1 END) received,
COUNT(CASE WHEN type = 'blocked' THEN 1 END) blocked,
COUNT(CASE WHEN type = 'opened' THEN 1 END) opened
FROM MyTbl
WHERE timestamp >= '2013-01-01 00:00:00' AND timestamp < '2013-02-01 00:00:00'
GROUP BY category
The details about the database schema, sample data and select-query for the report is available here.
Given that the:
data is fed into the table in real-time
table will store huge volume of data approx. 10,00,000+ records
structure of the report-query will not change
data would be filtered on category, date-from and date-to (all are optional parameters)
time is not relevant, only date part is
Will it be a good idea to run a scheduled task that would run periodically and update a new table with values from MyTbl? The new table would look similar to the report query:
Date | Category | Sent | Received | Blocked | Opened
and this table would be queried by applying category and date filters.
Negatives to this approach:
We still have to maintain data on daily basis
We still have to apply GROUP BY and SUM
There may be more database operations required than the original approach
We will not get all the data that comes in real-time.
Positives to this approach:
May speed-up the records fetching from the database which can speed-up the process of display, sorting and paging
Is this a viable approach?
Are there any other ways to speed up the process? Please help!
Yes, this is a viable approach.
You may also want to consider reconfiguring your query as a PIVOT.
select *
from
(select [date],category, [type]
from yourtable
where timestamp between '2013-01-01' and '2013-02-01') d
pivot
(count (type) for [type] in (sent,blocked,received,opened)) p

Resources