Before Start
I Have View 'PageViewModSession'
its code is
SELECT CONVERT(datetime, CONVERT(varchar(14), VisitStartDateTime) + ':00:00') AS DateValue, MAX(dbo.PageLogGroupByDateTimeFull(VisitStartDateTime)) AS PageLogCount,
(CASE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) WHEN 0 THEN 1 ELSE MAX(dbo.SessionGroupByDateTimeFull(VisitStartDateTime)) END) AS SessionLogCount, SiteInfoID
FROM dbo.PageLog
GROUP BY CONVERT(varchar(14), VisitStartDateTime), SiteInfoID
and When select this views my result is
and when select in ef with this syntaxt
var obj = db.PageViewModSessions.AsQueryable();
result is
repeat row one in every row on result
i catch created Sql query query in profiler
SELECT
[Extent1].[DateValue] AS [DateValue],
[Extent1].[PageLogCount] AS [PageLogCount],
[Extent1].[SessionLogCount] AS [SessionLogCount],
[Extent1].[SiteInfoID] AS [SiteInfoID]
FROM (SELECT
[PageViewModSession].[DateValue] AS [DateValue],
[PageViewModSession].[PageLogCount] AS [PageLogCount],
[PageViewModSession].[SessionLogCount] AS [SessionLogCount],
[PageViewModSession].[SiteInfoID] AS [SiteInfoID]
FROM [dbo].[PageViewModSession] AS [PageViewModSession]) AS [Extent1]
and result is
2015-11-03 01:00:00.000 19 9 2
2015-11-03 02:00:00.000 19 4 2
2015-11-03 03:00:00.000 4 1 2
2015-11-03 11:00:00.000 7 5 2
2015-11-03 12:00:00.000 9 2 2
2015-11-04 01:00:00.000 1 1 2
2015-11-04 02:00:00.000 12 1 2
2015-11-04 03:00:00.000 5 1 2
2015-11-04 05:00:00.000 1 1 2
2015-11-04 06:00:00.000 4 1 2
2015-11-04 10:00:00.000 20 2 2
2015-11-04 11:00:00.000 19 4 2
2015-11-04 12:00:00.000 23 18 2
2015-11-05 02:00:00.000 1 1 2
2015-11-05 03:00:00.000 5 1 2
2015-11-05 04:00:00.000 25 2 2
2015-11-05 10:00:00.000 2 1 2
2015-11-05 11:00:00.000 3 1 2
why ?!!
and what have to do fix this problem
I handle This
by Setting Two Key for model
DateValueand SiteInfoId
Related
I have a Table 'Meter1' in my MSSQL database DB_TEST. Table structure is like this,
MeterID
Timestamp
Value
7
2022-09-16 11:00:00.000
1800
7
2022-09-16 12:00:00.000
1805
7
2022-09-16 13:00:00.000
1820
7
2022-09-16 14:00:00.000
1860
7
2022-09-16 15:00:00.000
1875
I need to calculate the hourly consumption by substracting the current value - previous value.
I have achieved this by using LAG function,
SELECT [MeterID]
,[Timestamp]
,[Value]
,VALUE - LAG (VALUE)
OVER (ORDER BY TIMESTAMP) AS Consumption
FROM [DB_TEST].[dbo].[Meter1]
& the results like this
MeterID
Timestamp
Value
Consumption
7
2022-09-16 11:00:00.000
1800
NULL
7
2022-09-16 12:00:00.000
1805
5
7
2022-09-16 13:00:00.000
1820
15
7
2022-09-16 14:00:00.000
1860
40
7
2022-09-16 15:00:00.000
1875
15
But how can I update or Insert the same results to my existing table with another column "Consumption".
I have a table that looks like this:
Timestamp CPID Con Context Type Value
2018-01-01 03:11 1 2 6 8 0
2018-01-01 03:11 1 2 3 8 0
2018-01-01 03:11 1 2 3 3 100
2018-01-01 03:15 2 1 6 8 16
2018-01-01 03:15 2 1 3 8 15
2018-01-01 03:15 2 1 3 3 200
I want to add a column called new_column, and populate it with 1s when Value=0 when Context=6. I want to consider Timestamp, CPID and Con as a group, so that when for a given group has Context=6, the other rows in that group are also assigned 1 in new_column. The result would look like this:
Timestamp CPID Con Context Type Value new_column
2018-01-01 03:11 1 2 6 8 0 1
2018-01-01 03:11 1 2 3 8 0 1
2018-01-01 03:11 1 2 3 3 100 1
2018-01-01 03:15 2 1 6 8 16 0
2018-01-01 03:15 2 1 3 8 15 0
2018-01-01 03:15 2 1 3 3 200 0
Notes: the row orders are not always the same, so I can't just fill down 2 rows every time; I also cannot directly ALTER Table because it is read only.
I'm still new to SQL so struggling with this one.
You can create a view by using exists :
select t.*,
(case when exists (select 1
from table t1
where t1.Timestamp = t.Timestamp and t1.CPID = t.CPID and
t1.Con = t.Con and (t1.Context = 6 or t1.Value = 0)
)
then 1 else 0
end) as new_column
from table t;
I am trying to to extend the valid_till date for a month of tenants who have reference id more than two times.
refid, referrer_id, referrer_bonus_amount, referral_valid, valid_from, valid_till
1 2 2500 1 2015-07-05 2015-09-05
2 3 2500 1 2015-07-05 2015-09-05
3 5 1000 0 2015-12-13 2016-02-13
4 6 2500 0 2016-04-25 2016-06-24
5 10 1000 1 2015-07-01 2015-09-01
6 12 2500 1 2015-05-12 2015-07-12
7 13 2500 0 2015-08-05 2015-10-05
8 20 1000 1 2016-02-05 2016-04-05
9 2 2500 0 2015-08-12 2015-09-12
10 5 91000 1 2016-02-18 2016-04-18
11 20 1500 1 2016-06-19 2016-08-19
12 9 2500 0 2015-11-15 2016-01-15
13 13 91000 1 2016-02-01 2016-04-01
14 5 1000 1 2016-04-25 2016-06-24
To update the table (t) to add 1 month to the valid_till date for those refid that appear in referrer_id more than two times using exists() with having count(*) > 2:
update t
set valid_till = dateadd(month,1,valid_till)
output inserted.*
from t
where exists (
select 1
from t as i
where i.referrer_id = t.refid
group by referrer_id
having count(*) > 2
)
rextester demo: http://rextester.com/WXZC31875
output:
+-------+-------------+-----------------------+----------------+------------+------------+
| refid | referrer_id | referrer_bonus_amount | referral_valid | valid_from | valid_till |
+-------+-------------+-----------------------+----------------+------------+------------+
| 5 | 10 | 1000 | 1 | 2015-07-01 | 2015-10-01 |
+-------+-------------+-----------------------+----------------+------------+------------+
SELECT
t0.brandID,
t0.brandName,
t0.cdt,
t0.udt,
t0.brandstatus,
(DATEPART(MINUTE, t0.cdt)) as tempt
FROM brands t0
WHERE
CONVERT(VARCHAR(10),t0.cdt,110) BETWEEN
CONVERT(VARCHAR(10),'01-11-2013',110) and CONVERT(VARCHAR(10),'11-11-2014',110)
The above query giving me values between the dates. I want to get values between these dates for every 10 minutes.
brandID brandName cdt udt brandstatus tempt
1 khasim 2013-11-01 19:14:18.123 2013-11-15 19:14:18.123 1 14
3 khasim 2013-11-02 19:17:57.700 2013-11-15 19:17:57.700 1 17
4 tanveer 2013-11-03 19:18:05.947 2013-11-15 19:18:05.947 1 18
5 abcdef 2013-11-04 20:50:06.783 2013-11-15 20:50:06.787 1 50
8 budwieser 2014-02-12 19:26:43.913 2014-02-12 19:26:43.913 1 26
expected result: (when selecting date between 15-11-2013 and 16-11-2013)
brandid brandname cdt udt
1 khasim 2013-11-15 (00:00:01)---first record created on that date
2 somethi 2013-11-15 (00:00:05)---second record
...
...
final record at (00:10:00) minutes...
need to calculate number of 10 minute intervals between selected date and for every 10 min interval need to get records.
I've following table:
Id CreationDate FromEntryNo ToEntryNo
1 2013-01-01 1 4
2 2013-01-03 5 8
3 2013-01-05 9 11
...
I want to split this into multiple rows to have a list with all consecutive EntryNo, something like this:
Id CreationDate FromEntryNo ToEntryNo EntryNo
1 2013-01-01 1 4 1
1 2013-01-01 1 4 2
1 2013-01-01 1 4 3
1 2013-01-01 1 4 4
2 2013-01-03 5 8 5
2 2013-01-03 5 8 6
2 2013-01-03 5 8 7
2 2013-01-03 5 8 8
3 2013-01-05 9 11 9
3 2013-01-05 9 11 10
3 2013-01-05 9 11 11
...
My first attempt is CTE with recursion, but it doesn't work:
with cte as
(select gr.Id, gr.CreationDate, gr.FromEntryNo, gr.ToEntryNo, gr.FromEntryNo as [EntryNo]
from dbo.[Register] gr
union all
select No, CreationDate, FromEntryNo, ToEntryNo, EntryNo + 1 from cte where EntryNo <= ToEntryNo
)
select Id, CreationDate, FromEntryNo, ToEntryNo, EntryNo from cte
<
Any idea how to do this using one SQL query?
with cte as
(select gr.Id, gr.CreationDate, gr.FromEntryNo, gr.ToEntryNo,
gr.FromEntryNo as [EntryNo]
from dbo.[Register] gr
union all
select Id, CreationDate, FromEntryNo,
ToEntryNo, EntryNo + 1
from cte where EntryNo < ToEntryNo
)
select Id, CreationDate, FromEntryNo, ToEntryNo, EntryNo
from cte
ORDER BY Id,EntryNo