I want to only return the distinct collection_time group, for only collection_time groups that contain at least 1 non null row for the blocking_session_id field. I can't get it right. Here is some example data. In the example below, the 2 rows would be returned. The last grouping of collection_time has all nulls for blocking_session_id so I would not want that group in my result set. Thanks for any tips.
2022-12-02 13:13:22.167
2022-12-02 13:13:43.873
collection_time blocking_session_id
2022-12-02 13:13:22.167 525
2022-12-02 13:13:22.167 481
2022-12-02 13:13:22.167 NULL
2022-12-02 13:13:22.167 NULL
2022-12-02 13:13:43.873 NULL
2022-12-02 13:13:43.873 NULL
2022-12-02 13:13:43.873 591
2022-12-01 20:25:12.847 NULL
2022-12-01 20:25:12.847 NULL
2022-12-01 20:25:12.847 NULL
2022-12-01 20:25:12.847 NULL
2022-12-01 20:25:12.847 NULL
You can simply select all distinct collection_times where there are no NULL blocking_session_ids
SELECT DISTINCT collection_time
FROM your_table
WHERE blocking_session_id IS NOT NULL
Related
My SQL: select * from meters order by ts;
Here's the retult from TDengine-server-3.0.2.0-Linux-x64
ts
tv
cs
th
report_time
device_no
2022-12-02 09:22:22.249
30
1
50
2022-12-02 09:22:22.249
d001
2022-12-02 09:59:54.664
27
2
20
2022-12-02 09:59:54.664
d001
2022-12-02 15:55:01.473
29
3
33
2022-12-02 15:55:01.473
d001
2022-12-02 18:25:01.494
15
4
44
2022-12-02 18:25:01.494
d001
2022-12-02 18:25:01.559
15
4
44
2022-12-02 18:25:01.559
d002
2022-12-03 14:36:37.775
15
4
44
2022-12-03 14:36:37.775
d002
but the result from TDengine-client-3.0.2.0-Windows-x64 is different.
ts
tv
cs
th
report_time
device_no
2022-12-01 17:22:22.249
30
1
50
2022-12-01 17:22:22.249
d001
2022-12-01 17:59:54.664
27
2
20
2022-12-01 17:59:54.664
d001
2022-12-01 23:55:01.473
29
3
33
2022-12-01 23:55:01.473
d001
2022-12-02 02:25:01.494
15
4
44
2022-12-02 02:25:01.494
d001
2022-12-02 02:25:01.559
15
4
44
2022-12-02 02:25:01.559
d002
2022-12-02 22:36:37.775
15
4
44
2022-12-02 22:36:37.775
d002
The problem of upgrading to the latest 3.0.2.1 version still exists.
This is init data SQL:
CREATE STABLE meters(
ts timestamp,
tv SMALLINT UNSIGNED,
cs TINYINT UNSIGNED,
th SMALLINT UNSIGNED,
report_time timestamp
) TAGS(
device_no BINARY(60)
);
INSERT INTO d001 USING meters TAGS('d001') VALUES('2022-12-02 09:22:22.249',30,1,50,'2022-12-02 09:22:22.249');
INSERT INTO d001 USING meters TAGS('d001') VALUES('2022-12-02 09:59:54.664',27,2,20,'2022-12-02 09:59:54.664');
INSERT INTO d001 USING meters TAGS('d001') VALUES('2022-12-02 15:55:01.473',29,3,33,'2022-12-02 15:55:01.473');
INSERT INTO d001 USING meters TAGS('d001') VALUES('2022-12-02 18:25:01.494',15,4,44,'2022-12-02 18:25:01.494');
INSERT INTO d002 USING meters TAGS('d002') VALUES('2022-12-02 18:25:01.559',15,4,44,'2022-12-02 18:25:01.559');
INSERT INTO d002 USING meters TAGS('d002') VALUES('2022-12-03 14:36:37.775',15,4,44,'2022-12-03 14:36:37.775');
I want to create a trigger after insert and update which calculate the sum of values in multiple rows and set the sum in [Target_Cummlative]. I try the following code but that causes this error:
Msg 156, Level 15, State 1, Procedure target_cummlative, Line 13
Incorrect syntax near the keyword 'FROM
My code:
CREATE TRIGGER [dbo].[target_cummlative]
ON [dbo].[Appointments]
AFTER insert, UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE T1
SET t1.[Target_Cummlative] = SUM(ISNULL(CAST([TARGET] AS FLOAT), 0)
FROM [Appointments] T1
INNER JOIN inserted i ON T1.[UniqueId] = i.[UniqueId]
GROUP BY CONVERT(VARCHAR(10), t1.[StartDate], 111), t1.[ResourceId]
END
Here is what i have currently, as you can see Target cummlative is null
Id StartDate Location ResourceId TARGET Target_Cummlative
1381 2019-07-22 07:00:00 41051 1 20 NULL
1382 2019-07-22 08:00:00 41051 1 20 NULL
1383 2019-07-22 09:15:00 41051 1 15 NULL
1384 2019-07-22 10:00:00 41051 1 20 NULL
1385 2019-07-22 11:00:00 41051 1 20 NULL
1386 2019-07-22 12:30:00 41051 1 8 NULL
I want set the sum the values in TARGET column and update Target cummlative as
Id StartDate Location ResourceId TARGET Target_Cummlative
1381 2019-07-22 07:00:00 41051 1 20 103
1382 2019-07-22 08:00:00 41051 1 20 103
1383 2019-07-22 09:15:00 41051 1 15 103
1384 2019-07-22 10:00:00 41051 1 20 103
1385 2019-07-22 11:00:00 41051 1 20 103
1386 2019-07-22 12:30:00 41051 1 8 103
If possible - I'd advice against storing cumulative data in the same table, if possible - better create a separate one to store aggregated information.
In any case - your issue is missing parenthesis in SET clause.
CREATE TRIGGER [dbo].[target_cumulative]
ON [dbo].[Appointments]
AFTER insert, UPDATE
AS
BEGIN
IF TRIGGER_NESTLEVEL() > 1
RETURN
UPDATE T1
SET T1.[Target_Cumulative] = SUM(ISNULL(CAST([TARGET] AS FLOAT), 0))
FROM dbo.Appointments AS T1
INNER JOIN inserted AS ION I.UniqueId = T1.UniqueId
GROUP BY CONVERT(DATE, T1.StartDate), T1.ResourceId;
END;
Oh - and there's typo in cummlative, it should be cumulative
i have one log table in that i can check how many records inserted in which table.
i want to find that in which device has 0 records inserted from complete logs.
sample data
Id DeviceID Success RecordInserted ErrorMessage LogDate
1 0000ACFFFE4D48 1 395 NULL 2018-05-11 12:54:55.713
2 0000ACFFFE4E6A 1 492 NULL 2018-05-11 12:55:10.277
3 0000ACFFFE51D9 1 247 NULL 2018-05-11 12:55:17.790
4 0000ACFFFE5585 1 399 NULL 2018-05-11 12:55:28.267
5 0000ACFFFE5B03 1 493 NULL 2018-05-11 12:55:44.313
6 0000ACFFFE56E3 1 456 NULL 2018-05-11 12:56:00.743
7 0000ACFFFE1183 1 410 NULL 2018-05-11 12:56:09.817
8 0000ACFFFE2693 1 333 NULL 2018-05-11 12:56:24.613
9 0000ACFFFE4454 1 456 NULL 2018-05-11 12:56:36.867
10 0000ACFFFE7223 1 10056 NULL 2018-05-11 13:03:04.410
11 0000ACFFFE1CBB 1 10046 NULL 2018-05-11 13:06:03.860
12 0000ACFFFE2F81 1 0 NULL 2018-05-11 13:06:06.567
13 0000ACFFFE6F29 1 0 NULL 2018-05-11 13:06:08.307
14 0000ACFFFE0B8D 1 10078 NULL 2018-05-11 13:10:28.020
15 0000ACFFFE4DF2 1 409 NULL 2018-05-11 13:10:39.950
16 0000ACFFFE4D48 1 0 NULL 2018-05-11 13:16:32.027
17 0000ACFFFE4E6A 1 1 NULL 2018-05-11 13:16:38.640
18 0000ACFFFE51D9 1 1 NULL 2018-05-11 13:16:41.997
19 0000ACFFFE5585 1 1 NULL 2018-05-11 13:16:49.473
20 0000ACFFFE5B03 1 1 NULL 2018-05-11 13:16:56.797
21 0000ACFFFE56E3 1 1 NULL 2018-05-11 13:17:03.703
22 0000ACFFFE1183 1 0 NULL 2018-05-11 13:17:10.283
23 0000ACFFFE2693 1 1 NULL 2018-05-11 13:17:14.830
24 0000ACFFFE4454 1 0 NULL 2018-05-11 13:17:21.880
25 0000ACFFFE7223 1 64 NULL 2018-05-11 14:06:32.153
26 0000ACFFFE1CBB 1 64 NULL 2018-05-11 14:09:26.907
27 0000ACFFFE2F81 1 0 NULL 2018-05-11 14:09:28.743
28 0000ACFFFE6F29 1 0 NULL 2018-05-11 14:09:30.667
29 0000ACFFFE0B8D 1 64 NULL 2018-05-11 14:11:42.227
30 0000ACFFFE4DF2 1 3 NULL 2018-05-11 14:11:48.857
Expected output
0000ACFFFE6F290B
0000ACFFFE2F818D
This 2 deviceid have 0 recordinserted
sql query
select deviceid from logs where recordinserted !=0 and deviceid in (select deviceid from logs where recordinserted= '0')
error
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Are you looking for devices with zero entries? Or devices whose entries total 0 RecordsInserted? I do not see your expected output Ids in the list and this Id '0000ACFFFE2F81' has two entries both with 0 Records Inserted.
For DeviceIds that are not listed, DeviceId needs to be a foreign key to another table, and you can find those that are not in the logs table with this query:
SELECT Id FROM DeviceTable
EXCEPT
SELECT DeviceId FROM Logs
To get those that have a sum total of 0 records in the Logs Table use this query:
SELECT DeviceId
,SUM(RecordsInserted)
FROM Logs
GROUP BY DeviceId
Having SUM(RecordsInserted) = 0
select deviceid from logs where recordinserted = 0
You can use the where clause like this when recordinserted column is integer type.
select deviceid
from logs
group by deviceid
having max(recordinserted) = 0 -- no row greater zero exists
I am trying to retrieve the most recent entry of a record in an Access report. The query I have gives me the results in SQL-Server but row_number is not compatible with Access. Its been suggested that I use the max function in Access. Can you assist me in generating this report?
SELECT cID, CName, Address, Project#, JobOwner, SubStatusID, Status, JNJobID, JNNote
FROM (
SELECT
cID, CName, Address, Project#, JobOwner, SubStatusID, Status, JNJobID, JNNote
, ROW_NUMBER() OVER (PARTITION BY JNJobID ORDER BY JNDate DESC) AS r
FROM [JobNotes]
Left JOIN Jobs ON [JobNotes].JNJobID = Jobs.JobID
Left JOIN Addresses ON Jobs.JobAddressID = Addresses.AddressID
Left JOIN Customers ON Jobs.JobCustomerID = Customers.CID
Left JOIN Status ON Jobs.JobSubStatusID = Status.StatusID
) x
WHERE r = 1 and customerID = 134 and jobsubstatusid <> 14 and jobsubstatusid <> 15 and jobsubstatusid <> 16 and jobsubstatusid <> 42 and jobsubstatusid <>38 and jobsubstatusid <>75
Jobs Table
JobID Project# JobOwner JobStatusID AddressID JobCustomerID
6972 PN1 John 1 333 222
6973 PN2 Sarah 3 444 666
6974 PN3 James 6 555 777
Address Table
AddressID Address
333 1333 Janes Ln
444 5555 Davis Blvd
555 888 Post Rd
Customer Table
CID CName
222 Builder
666 HomeOwner
777 HOA
JobNotes Table
JobNotesID JNJobID JNDate JNNote
11800 6972 2016-03-15 00:00:00.000 Example 1
11874 6972 2016-03-17 00:00:00.000 Example 2
12181 6972 2016-03-25 00:00:00.000 Example 3
12006 6973 2016-03-21 00:00:00.000 Example 4
11961 6974 2016-03-18 00:00:00.000 Example 5
11924 6974 2016-03-17 00:00:00.000 Example 6
JobNotes Table
CID CName Address Project# JobOwner SubStatusID Status JNJobID JNNote
222 Builder 1333 Janes Ln PN1 John 1 Sales 6972 Example 3
666 HomeOwner 5555 Davis Blvd PN2 Sarah 3 Design 6973 Example 4
777 HOA 888 Post Rd PN3 James 6 Construction 6974 Example 6
For example i have a Table1:
ID Specified TIN Value DateCreated
----------------------------------
1 0 tin1 45 2014-12-30
2 1 tin2 34 2013-01-05
3 0 tin3 23 2015-02-20
4 3 tin4 47 2013-06-04
5 3 tin5 12 2012-04-02
And a Table2:
ID Table1ID RegistrationDate
----------------------------------
1 1 2015-10-12
2 2 2015-07-21
3 1 2015-11-26
4 1 2015-12-04
5 2 2015-09-18
I need select all columns from Table1 with first and last RegistrationDate column in Table2. The answer should be
ID Specified TIN Value DateCreated FirstRegDate LastRegDate
---------------------------------------------------------------
1 0 tin1 45 2014-12-30 2015-10-12 2015-12-04
2 1 tin2 34 2013-01-05 2015-07-21 2015-09-18
3 0 tin3 23 2015-02-20 NULL NULL
4 3 tin4 47 2013-06-04 NULL NULL
5 3 tin5 12 2012-04-02 NULL NULL
Hi one possible solution can be something similar to pseudo query below(if you can prepare the tables I will modify to reflect actual query)
SELECT table1.*, inlineTable2.firstRegDate, inlineTable2.lastRegDate
FROM Table1
LEFT JOIN
(
SELECT
Table1ID AS id,
MIN(registrationDate) as firstRegDate,
MAX(regsitrationDate) as lastRegDate
FROM table2
GROUP BY table1ID
) AS inlineTable2
ON table1.id = inlineTable2.id
You can group by all columns in table1, and look up the minumum and maximum registration date for the group:
select ID
, Specified
, ... other columns from table1 ...
, min(RegistrationDate)
, max(RegistrationDate)
from Table1 t1
left join
Table2 t2
on t1.ID = t2.Table1ID
group by
ID
, Specified
, ... other columns from table1 ...