View Full Version : کمک در انجام یک select
etbani
جمعه 06 بهمن 1391, 21:27 عصر
99000
این جدولمه و می خوام خروجی این بشه
99001
c1 ,c2,c3مقادیر count(count بر حسب فیلد count است , ،فیلد price،sum(price) است
با تشکر
محمد سلیم آبادی
جمعه 06 بهمن 1391, 21:43 عصر
تاریخ 22 از کجا اضافه شده؟
اینو امتحان کنین:
select insdate,
count(case when count = 1 then 1 else null end) as c1,
count(case when count = 2 then 1 else null end) as c2,
count(case when count = 3 then 1 else null end) as c3,
sum(price) as price
from table_name
group by insdate;
etbani
جمعه 06 بهمن 1391, 22:27 عصر
مرسی همینه:لبخندساده:.
من می خوام این خروجی رو تو gridview بذارم برای تاریخ 22 که هیپی درج نشده ستون گریدو چه کار کنم البته میدونم تو این تالار جای سوالش نیست:خجالت:
محمد سلیم آبادی
شنبه 07 بهمن 1391, 06:12 صبح
ارتباطی به grid view نداره، باید توسط query تاریخ 22 بدست بیاد، برای این منظور بایستی یه calendar table درست کنید که حاوی روزهای مختلف سال باشد. سپس روزهایی که بین کوچکترین و بزرگترین تاریخ هست و در جدول وجود ندارد استخراج بشه.
پس اول ایجاد این جدول:
CREATE TABLE [Calendar]
(
[CalendarDate] DATETIME
)
DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = '2011-03-01'
SET @EndDate = DATEADD(d, 365*2, @StartDate)
WHILE @StartDate <= @EndDate
BEGIN
INSERT INTO [Calendar]
(
CalendarDate
)
SELECT
@StartDate
SET @StartDate = DATEADD(dd, 1, @StartDate)
END
سپس گرفتن این query:
select c.CalendarDate,
isnull(t.c1, 0),
isnull(t.c2, 0),
isnull(t.c3, 0),
isnull(t.price, 0)
from
(
select insdate,
count(case when count = 1 then 1 else null end) as c1,
count(case when count = 2 then 1 else null end) as c2,
count(case when count = 3 then 1 else null end) as c3,
sum(price) as price,
min(insdate) over() as mi,
max(insdate) over() as mx
from table_name
group by insdate
)t
right outer join [Calendar] c
on c.CalendarDate between mi and mx
and t.insdate = CalendarDate;
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.