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 |
+-------+-------------+-----------------------+----------------+------------+------------+
There are two tables:
SatisDetay
Depo SatisID SatisSira UrunID Satici KdvKod KDVYuzde Miktar
AZ01 20001 1 3788898 999 AZ 18.00 1
AZ01 20002 1 3788898 999 AZ 18.00 1
AZ01 20003 1 3876390 999 AZ 18.00 1
AZ01 20003 2 3793202 999 AZ 18.00 1
AZ01 20003 3 4046508 999 AZ 18.00 1
AZ01 20003 4 3843387 999 AZ 18.00 1
AZ01 20003 5 3850608 999 AZ 18.00 1
And DepoSevkDetay
BaslikID Sira UrunID Miktar Fiyat Depo
20001 1 3792703 1 1 AZ01
20002 1 4067131 1 1 AZ01
20003 1 3251881 1 1 AZ01
20003 2 3251883 1 1 AZ01
20003 3 3788887 1 1 AZ01
20003 4 3788890 1 1 AZ01
20004 1 3761260 2 1 AZ01
There is a task to take sum of Miktar by each UrunID and spread it to UrunID in the second table. But the amount of UrunID records may not match. But it is not the only problem. I tried a simple query as below but it executes endlessly
UPDATE
Table_A
SET
Table_A.[Miktar] = Table_B.[Miktar]
FROM
[Retail].[dbo].[tb_DepoSevkDetay] AS Table_A
INNER JOIN (
SELECT [Miktar], [UrunID] FROM [Retail].[dbo].[tb_SatisDetay]
GROUP BY [UrunID], [Miktar] ) AS Table_B
ON Table_A.[UrunID] = Table_B.[UrunID]
Please help to find out why
I have the below table format and wanted to compute the last column based on other two columns:
BASE_VERSION JOURNEY_NO LINE_NO DIRECTION OP_DEP_NO SEQ_NO BLOCK_NO DEP_TIME TRAV_TIME ARRV_TIME
20,160,603 33,263 176 1 2 1 21,760,010 5:25:00 ? 5:25:00
20,160,603 33,263 176 1 2 2 21,760,010 5:25:00 0:00:45 5:25:45
20,160,603 33,263 176 1 2 3 21,760,010 5:25:00 0:00:43 5:26:28
20,160,603 33,263 176 1 2 4 21,760,010 5:25:00 0:00:47 5:27:15
20,160,603 33,263 176 1 2 5 21,760,010 5:25:00 0:00:59 5:28:14
20,160,603 33,263 176 1 2 6 21,760,010 5:25:00 0:01:31 5:29:45
20,160,603 33,263 176 1 2 7 21,760,010 5:25:00 0:01:08 5:30:53
20,160,603 33,263 176 1 2 8 21,760,010 5:25:00 0:01:37 5:32:30
20,160,603 33,263 176 1 2 9 21,760,010 5:25:00 0:00:48 5:33:18
20,160,603 33,263 176 1 2 10 21,760,010 5:25:00 0:01:02 5:34:20
20,160,603 33,263 176 1 2 11 21,760,010 5:25:00 0:00:38 5:34:58
20,160,603 33,263 176 1 2 12 21,760,010 5:25:00 0:01:18 5:36:16
20,160,603 33,263 176 1 2 13 21,760,010 5:25:00 0:00:58 5:37:14
20,160,603 33,263 176 1 2 14 21,760,010 5:25:00 0:00:47 5:38:01
Last Column ARRV_TIME for first row is (DEP_TIME + TRAV_TIME) . from second row ARRV_TIME is calculated as prev value + current row TRAV_TIME.
eg: 1 row for ARRV_TIME is (5:25:00 +?) = 5:25:00. from 2 row to rest is calculated as prev result which is 5:25:00 + 0:00:45 = 5:25:45 then next row is 5:25:45 + 0:00:43 = 5:26:28 and so on for each LINE_NO and DIRECTION.
You describe a Cumulative Sum, one of the basic tasks for an Analytic Function:
SUM(column TRAV_TIME is based on)
OVER (PARTITION BY LINE_NO, DIRECTION
ORDER BY SEQ_NO
ROWS UNBOUNDED PRECEDING)
Add this to the column DEP_TIME is based on and then apply the interval calculation
result * INTERVAL '00:00:01' HOUR TO SECOND
This results in an Interval, if you need a Time datatype:
TIME '00:00:00' + (result * INTERVAL '00:00:01' HOUR TO SECOND)
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
When I run a SQL query on a single table and here is the data (this is just a sample, error column might be more than 10)
time total Error
00:16 6 10000(E)
00:20 4 10000(E)
00:46 2 10000(E)
01:01 2 10000(E)
01:40 2 10000(E)
02:07 2 10000(E)
02:52 1 10000(E)
04:27 2 10000(E)
04:29 6 10000(E)
04:32 4 10000(E)
04:49 2 10000(E)
04:50 2 10000(E)
06:18 2 10000(E)
09:04 1 10000(E)
10:57 4 10000(E)
10:58 4 10000(E)
00:36 1 9401(E)
00:37 1 9401(E)
00:57 1 9401(E)
00:58 1 9401(E)
01:32 1 9401(E)
01:33 1 9401(E)
02:36 2 9401(E)
03:05 1 9401(E)
03:06 1 9401(E)
09:53 2 9401(E)
12:11 2 9401(E)
12:12 4 9401(E)
12:41 1 9401(E)
I want to write a SQL query so that I want to get the above data like this
time 10000(E) 9401(E)
---------------------------
00:16 6 0
00:20 4 0
00:36 0 1
00:37 0 1
00:46 2 0
00:57 0 1
00:58 0 1
01:01 2 0
01:32 0 1
01:33 0 1
01:40 2 0
02:07 2 0
02:36 0 2
02:52 1 0
03:05 0 1
03:06 0 1
04:27 2 0
04:29 6 0
04:32 4 0
04:49 2 0
04:50 2 0
06:18 2 0
09:04 1 0
09:53 0 1
10:57 4 0
10:58 4 0
12:11 0 2
12:12 0 4
12:41 0 1
is this possible??
Does this meet your requirement?
select e.time
, e.[10000(E)]
, e.[9401(E)]
from (
select time
, SUM(case when Error LIKE N'10000(E)' then Total else NULL end) as [10000(E)]
, null as [9401(E)]
from MyTable
where Error LIKE N'10000(E)'
group by time
union
select time
, null as [10000(E)]
, SUM(case when Error LIKE N'9401' then Total else NULL end) as [9401(E)]
from MyTable
where Error LIKE N'9401(E)'
group by time
) e
order by e.time
If no, please tell me about the result so that I can bring the righteous corrections.
The SUM function only comes to group the number of occurences of a same error into one given time, which seems to be what you have in your table, actually. So, it shouldn't modify any data. On the other hand, if you had two different records of the same error by the same time, then they should be grouped by this time and the total of occurences of this error will be additioned.
For your given in- and output it could be as simple as this.
SELECT *
FROM (
SELECT time
, [10000(E)] = Total
, [9401(E)] = 0
FROM YourTable
WHERE Error = '10000(E)'
UNION ALL
SELECT time
, [10000(E)] = 0
, [9401(E)] = Total
FROM YourTable
WHERE Error = '9401(E)'
) q
ORDER BY
time