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
Related
i would like to add a sequence nr from a set value.
is this output possible?
p_id date days OUTPUT(what i want)
3385 2012-02-02 556 0
3385 2012-02-03 1 1
3385 2012-09-24 234 0
3385 2012-09-25 1 1
3385 2013-11-12 413 0
3385 2013-11-13 1 1
3385 2013-11-14 1 2
3385 2013-11-15 1 3
3385 2014-09-09 298 0
3385 2014-09-10 1 1
3385 2014-09-11 1 2
3385 2015-11-11 426 0
3385 2015-11-12 1 1
3385 2015-11-13 1 2
3385 2015-11-14 1 3
3385 2015-11-15 1 4
3385 2015-11-16 1 5
3385 2015-11-17 1 6
3385 2015-11-18 1 7
3385 2015-11-19 1 8
3385 2015-11-20 1 9
The days column counts days from the row abow it.
if the days value is greater the 5 then start a new "0" and then a new sequence number.
Try this:
SELECT p_id, [date], days,
ROW_NUMBER() OVER (PARTITION BY p_id, grp
ORDER BY [date]) - 1 AS [OUTPUT]
FROM (
SELECT p_id, [date], days,
SUM(IIF(days > 5, 1, 0)) OVER (PARTITION BY p_id
ORDER BY [date]) AS grp
FROM mytable ) AS t
Explanation:
The inner query uses SUM() OVER() to produce the following output:
p_id date days grp
===============================
3385 2012-02-02 556 1
3385 2012-02-03 1 1
3385 2012-09-24 234 2
3385 2012-09-25 1 2
3385 2013-11-12 413 3
3385 2013-11-13 1 3
3385 2013-11-14 1 3
3385 2013-11-15 1 3
3385 2014-09-09 298 4
3385 2014-09-10 1 4
3385 2014-09-11 1 4
3385 2015-11-11 426 5
3385 2015-11-12 1 5
3385 2015-11-13 1 5
3385 2015-11-14 1 5
3385 2015-11-15 1 5
3385 2015-11-16 1 5
3385 2015-11-17 1 5
3385 2015-11-18 1 5
3385 2015-11-19 1 5
3385 2015-11-20 1 5
grp field is essentially the running total of 'greater than 5' occurrences. Using this field in an outer query we can easily produce the required enumeration with ROW_NUMBER window function.
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
With Sql Server 2014:
I have two tables - Events and Locations, that share a time column and I need to merge them into one table order by time. In the Events table there is an Event column that I need to place in all the Locations row following that event (time wise), here is an example:
Events:
time event
------------
09:00 2
09:10 3
10:15 1
10:17 2
10:30 3
Locations:
time X Y
-------------
09:01 1 3
09:02 2 3
09:05 4 1
09:09 6 4
09:10 7 8
09:11 8 8
09:12 9 7
10:17 1 2
10:19 5 4
10:20 4 3
10:25 5 4
10:28 3 5
Merged Table:
time X Y event
--------------------
09:00 0 0 2
09:01 1 3 2 <
09:02 2 3 2 <
09:05 4 1 2 <
09:09 6 4 2 <
09:10 0 0 3
09:10 7 8 3 <
09:11 8 8 3 <
09:12 9 7 3 <
10:15 0 0 1
10:17 0 0 2
10:17 1 2 2 <
10:19 5 4 2 <
10:20 4 3 2 <
10:25 5 4 2 <
10:28 3 5 2 <
10:30 0 0 3
The elements that mark with '<' are the inserted Events.
Any ideas and help on how to perform this task is welcome.
You can use UNION ALL and APPLY:
SQL Fiddle
SELECT
[Time], X = 0, Y = 0, [Event]
FROM [Events]
UNION ALL
SELECT l.*, x.Event
FROM Locations l
CROSS APPLY(
SELECT TOP 1 *
FROM [Events]
WHERE [Time] <= l.[Time]
ORDER BY [Time] DESC
)x
ORDER BY [Time]
set row pro1 pro2 Dup
1 1 AB2 AB2
1 1 AB6 AB4
1 1 AB2 NB3
2 1 AN2 QW3
2 2 BH1 BH1
3 1 AJ1 AJ1
4 1 HU3 HU3
4 1 BH2 BH2
I want to populate dup = 'Case1' where for a given set, pro1 = pro2 ; it is a requirement that there is more than 1 record in that set.
Expected result:
set row pro1 pro2 Dup
1 1 AB2 AB2 CASE1
1 1 AB6 AB4 NULL
1 1 AB2 NB3 NULL
2 1 AN2 QW3 NULL
2 2 BH1 BH1 CASE1
3 1 AJ1 AJ1 NULL (even though it matches, but its just one record)
4 1 HU3 HU3 CASE1
4 1 BH2 BH2 CASE1
with C as
(
select pro1,
pro2,
dup,
count(*) over(partition by [set]) as setcount
from YourTable
)
update C
set dup = 'CASE!'
where pro1 = pro2 and
setcount > 1
Working sample on SE-Data
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