PDA

View Full Version : ادغام دو رکورد



m.khani_121
یک شنبه 22 بهمن 1391, 12:15 عصر
سلام دوستان . طبق این عکس باید چه شرطی بگذارم یا چه دستوری بنویسم که این 2تا رکورد ، تبدیل به یه رکورد بشه ؟؟؟

99865

محمد سلیم آبادی
یک شنبه 22 بهمن 1391, 12:23 عصر
شما دقیقا میخواهید چه خروجی داشته باشید؟
با group by میشه سطرهای خروجی را ادغام کرد ولی باید دید دقیقا خروجی مورد نظر شما چیه تا بشه یک کد صحیح پیشنهاد داد.

m.khani_121
یک شنبه 22 بهمن 1391, 12:32 عصر
ببینید دوست عزیز توی عکس من 2 تا رکورد دارم . میخوام این 2تا یکی بشند . یعنی اطلاعات 2تاشون تو یه رکورد باشه ...

محمد سلیم آبادی
یک شنبه 22 بهمن 1391, 14:00 عصر
یعنی چیزی شبیه به این؟
99868

m.khani_121
یک شنبه 22 بهمن 1391, 14:26 عصر
آره خودشه . چه دستوری باید بنویسم ؟؟؟ ممنونم .

محمد سلیم آبادی
یک شنبه 22 بهمن 1391, 16:25 عصر
فقط به جای table_name نام جدولتان را قرار دهید:
select Perno, EnterDate,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistFate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistFate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
(
select *, row_number() over(order by EnterTime) rnk
from table_name
where Perno = 4507431
and EnterDate = '1391/03/01'
)t
group by Perno, EnterDate

m.khani_121
یک شنبه 22 بهمن 1391, 16:56 عصر
دوست عزیز من این دستور رو نوشتم :


select Perno, EnterDate,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistFate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistFate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
(
select *, row_number() over(order by EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate = '1391/03/01'
)
group by Perno, EnterDate

این خطا رو میده :


Incorrect syntax near the keyword 'group'.

محمد سلیم آبادی
یک شنبه 22 بهمن 1391, 17:59 عصر
فکر کردین اون t که بعد از پرانتز اومده اضافه و اشتباهیه که حذفش کردین؟!
بعد از پرانتز باید یک حرف نوشته شود. در واقع نام مستعار باید برای جدول مشتق شده تعیین گردد.

m.khani_121
یک شنبه 22 بهمن 1391, 18:05 عصر
خب اول کار با t گذاشتم :


select Perno, EnterDate,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistFate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistFate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
(
select *, row_number() over(order by EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate = '1391/03/01'
)t
group by Perno, EnterDate

این خطا رو داد :
Incorrect syntax near 't'.
بازم خطا میده !!!!

محمد سلیم آبادی
یک شنبه 22 بهمن 1391, 18:10 عصر
اوه کلمه from جا افتاده.
select Perno, EnterDate,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistFate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistFate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
from
(
select *, row_number() over(order by EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate = '1391/03/01'
)t
group by Perno, EnterDate

m.khani_121
یک شنبه 22 بهمن 1391, 18:22 عصر
درست شد مهندس .

فقط یه چیزی . وقتی تاریخ رو بازه بهش میدم بهم میریزه :


select Perno, EnterDate,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistFate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistFate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
from
(
select *, row_number() over(order by EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate between '1391/03/01' and '1391/03/02'
)t
group by Perno, EnterDate

m.khani_121
یک شنبه 22 بهمن 1391, 21:37 عصر
99886

اینم عکسش . تاریخ ها و زمان ها رو null برمیگردونه .
http://barnamenevis.org/images/misc/pencil.png

محمد سلیم آبادی
دوشنبه 23 بهمن 1391, 06:17 صبح
این را امتحان کنید:
select Perno,
--first repeating group
min(EnterDate) as EnterDate1,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistDate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
--second repeating group
max(EnterDate) as EnterDate2
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistDate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2
from
(
select *, row_number() over(partition by Perno order by EnterDate,EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate between '1391/03/01' and '1391/03/02'
)t
group by Perno;

m.khani_121
دوشنبه 23 بهمن 1391, 21:25 عصر
دوست عزیز من دیگه تاریخ ها رو نمیخوام ادغام کنم . هر روزی تو یه رکورد !

محمد سلیم آبادی
چهارشنبه 25 بهمن 1391, 13:07 عصر
من نمیدانم خروجی مورد نظر شما چیست. لطفا در صورت حل نشدن مشکلتون، خروجی مورد نظر خود را بر اساس داده های نمونه در قالب جدول (سطر و ستون) ارائه دهید.

m.khani_121
چهارشنبه 25 بهمن 1391, 19:47 عصر
شرمنده مهندس . این مثلا برای 4 روزه . حالا من میخوام برای یک ماه کوئری بگیرم . 2تا ورود و 2 تا خروج


100024

محمد سلیم آبادی
چهارشنبه 25 بهمن 1391, 20:04 عصر
با تغییر query پست شماره 13 میشه به این نتیجه دست یافت. کافیه rnk =3 و rnk = 4 هم لحاظ کنیم.

m.khani_121
چهارشنبه 25 بهمن 1391, 20:32 عصر
من تغییرش دادم اینجوری شد :


select Perno,
--first repeating group
min(EnterDate) as EnterDate1,
max(case when rnk = 1 then EnterTime else null end) as EnterTime1,
max(case when rnk = 1 then ExitDate else null end) as ExistDate1,
max(case when rnk = 1 then ExitTime else null end) as ExitTime1,
--second repeating group
max(EnterDate) as EnterDate2,
max(case when rnk = 2 then EnterTime else null end) as EnterTime2,
max(case when rnk = 2 then ExitDate else null end) as ExistDate2,
max(case when rnk = 2 then ExitTime else null end) as ExitTime2,
max(EnterDate) as EnterDate3,
max(case when rnk = 3 then EnterTime else null end) as EnterTime3,
max(case when rnk = 3 then ExitDate else null end) as ExistDate3,
max(case when rnk = 3 then ExitTime else null end) as ExitTime3,
max(EnterDate) as EnterDate4,
max(case when rnk = 4 then EnterTime else null end) as EnterTime4,
max(case when rnk = 4 then ExitDate else null end) as ExistDate4,
max(case when rnk = 4 then ExitTime else null end) as ExitTime4
from
(
select *, row_number() over(partition by Perno order by EnterDate,EnterTime) rnk
from IOINFO
where Perno = 4507431
and EnterDate between '1391/03/01' and '1391/03/02'
)t
group by Perno

100029
http://barnamenevis.org/images/misc/pencil.png

m.khani_121
چهارشنبه 25 بهمن 1391, 20:32 عصر
من میخوام مثل عکس پست 16 برای 1 ماه داشته باشم . برای هر روز یک ردیف !!! ممنونم واقعا . . .

حمیدرضاصادقیان
پنج شنبه 26 بهمن 1391, 23:53 عصر
سلام
دوست عزیز حداقل خودتون هم مقداری تلاش کنید.
نمونه کد هست خودتون فکر کنید و گسترشش بدید.
اینجا درخواست کد ممنوع هست.