PDA

View Full Version : نوشتن یک کوئری در sql server



alimooghashang
شنبه 28 مرداد 1391, 09:12 صبح
سلام
من یه سری داده دارم به این صورت
میخوام که اگه بشه با sql این داده ها رو دسته بندی کنم و بشمارم
شرطی که باید بذارم اینه، که تعداد داده هایی که مقدارش کمتر از 10 هست رو بهم برگردونه!
ولی یه شرط دیگه هم داره که باید بصورت دسته ای برگردونه
یعنی چی؟
یعنی خروجی برای داده های زیر بشه این

داده ها:

5.9264
4.3522
1.14824
2.8706
5.87084
8.48216
0.6482
2.38908
15.4642
6.81536
1.59272
5.14856
0.75932
7.408
11.538
14.3345
40.9848
28.1689
5.24116
5.85232
19.1497
9.87116
15.3531

خروجی:

8
5
2
1

اصلن میشه یه همچین کاری با sql کرد با group by ؟
یا باید داده ها رو پردازش کنم و به این نتیجه برسم؟
ممنون

lastmory
یک شنبه 29 مرداد 1391, 11:11 صبح
سلام ، اگه داده هات توی یه جدوله ، این راهو برو

Select * From
(
Select Cast(Number As Int)Number From Tbl_1
)T
GROUP BY T.Number

alimooghashang
یک شنبه 29 مرداد 1391, 14:07 عصر
ممنون
ولی شما متوجه منظور من نشدید!
من تعداد رکوردهایی که کمتر از 10 هست رو میخوام
و باید بصورت گروهی باشه!
مثلا 8 تای اول و 5 تای دوم و 2 تا ی سوم و 1 ی چهارم

baktash.n81@gmail.com
سه شنبه 31 مرداد 1391, 09:19 صبح
وقتی شما می گی تعداد رکوردهایی که کمتر از 10 هست ... یعنی مقدارشون کوچکتر از 10 هست ؟! خوب اگه اعداد رو با نقطه از هم جدا کردین ... فقط یه دونه 5 داریم ... ؟!؟ 8 تای اول و 5 تای دوم 2 تای سوم و .... مفهومشون چیه ؟!

alimooghashang
سه شنبه 31 مرداد 1391, 15:09 عصر
رکورد هایی که مقدارشون کمتر از 10 هست نه تعداد رکوردها!

Reza_Yarahmadi
سه شنبه 31 مرداد 1391, 19:17 عصر
اولین راه حلی که به ذهنم رسید این بود.
Declare @MyTable Table(ID int Identity, Number float)

Insert Into @MyTable Values (5.9264)
Insert Into @MyTable Values(4.3522)
Insert Into @MyTable Values(1.14824)
Insert Into @MyTable Values(2.8706)
Insert Into @MyTable Values(5.87084)
Insert Into @MyTable Values(8.48216)
Insert Into @MyTable Values(0.6482)
Insert Into @MyTable Values(2.38908)
Insert Into @MyTable Values(15.4642)
Insert Into @MyTable Values(6.81536)
Insert Into @MyTable Values(1.59272)
Insert Into @MyTable Values(5.14856)
Insert Into @MyTable Values(0.75932)
Insert Into @MyTable Values(7.408)
Insert Into @MyTable Values(11.538)
Insert Into @MyTable Values(14.3345)
Insert Into @MyTable Values(40.9848)
Insert Into @MyTable Values(28.1689)
Insert Into @MyTable Values(5.24116)
Insert Into @MyTable Values(5.85232)
Insert Into @MyTable Values(19.1497)
Insert Into @MyTable Values(9.87116)
Insert Into @MyTable Values(15.3531)

;With tbl (ID, Cnt) as(
Select TOP 1 ID, Cnt = Case When Number < 10 Then 1 Else 0 End From @MyTable
Union All
Select
ID = Case When Number < 10 Then T.ID Else MT.ID + 1 End,
Cnt = Case When Number < 10 Then Cnt + 1 Else 0 End
From
@MyTable MT INNER JOIN Tbl T
ON
MT.ID = T.ID + T.cnt)
Select MAX(Cnt)
From Tbl
Group By ID
Having MAX(Cnt) <> 0

alimooghashang
پنج شنبه 02 شهریور 1391, 18:48 عصر
راستشا بخواین اصل داده های من این شکلیه


datetime value
2012-01-01 16:21:52 6
2012-01-01 16:22:02 5
2012-01-01 16:22:12 2
2012-01-01 16:22:22 3
2012-01-01 16:22:32 6
2012-01-01 16:22:42 9
2012-01-01 16:22:52 1
2012-01-01 16:23:02 3
2012-01-01 16:23:12 16
2012-01-01 16:24:02 7
2012-01-01 16:24:12 2
2012-01-01 16:24:22 6
2012-01-01 16:24:32 1
2012-01-01 16:24:42 8
2012-01-01 16:24:52 12
2012-01-01 16:25:02 15
2012-01-01 16:25:12 41
2012-01-01 16:25:22 29
2012-01-01 16:25:32 6
2012-01-01 16:25:42 6
2012-01-01 16:25:52 20
2012-01-01 16:26:02 10
2012-01-01 16:26:12 16
2012-01-01 16:26:22 14
2012-01-01 16:26:32 10
2012-01-01 16:26:42 6
2012-01-01 16:26:52 9
2012-01-01 16:27:02 7
2012-01-01 16:27:12 7
2012-01-01 16:27:22 17

و من این خروجی رو نیاز دارم


from to count
2012-01-01 16:21:52 2012-01-01 16:23:02 8
2012-01-01 16:24:02 2012-01-01 16:24:42 5
2012-01-01 16:25:32 2012-01-01 16:25:42 2
2012-01-01 16:26:42 2012-01-01 16:27:12 4


ممنون

rahgozare_abi
جمعه 03 شهریور 1391, 03:54 صبح
Declare @DataTable Table(_Date DateTime , _Value float)

Insert Into @DataTable Values ('2012-01-01 16:21:52',6)
Insert Into @DataTable Values ('2012-01-01 16:22:02',5)
Insert Into @DataTable Values ('2012-01-01 16:22:12',2)
Insert Into @DataTable Values ('2012-01-01 16:22:22',3)
Insert Into @DataTable Values ('2012-01-01 16:22:32',6)
Insert Into @DataTable Values ('2012-01-01 16:22:42',9)
Insert Into @DataTable Values ('2012-01-01 16:22:52',1)
Insert Into @DataTable Values ('2012-01-01 16:23:02',3)
Insert Into @DataTable Values ('2012-01-01 16:23:12',16)
Insert Into @DataTable Values ('2012-01-01 16:24:02',7)
Insert Into @DataTable Values ('2012-01-01 16:24:12',2)
Insert Into @DataTable Values ('2012-01-01 16:24:22',6)
Insert Into @DataTable Values ('2012-01-01 16:24:32',1)
Insert Into @DataTable Values ('2012-01-01 16:24:42',8)
Insert Into @DataTable Values ('2012-01-01 16:24:52',12)
Insert Into @DataTable Values ('2012-01-01 16:25:02',15)
Insert Into @DataTable Values ('2012-01-01 16:25:12',41)
Insert Into @DataTable Values ('2012-01-01 16:25:22',29)
Insert Into @DataTable Values ('2012-01-01 16:25:32',6)
Insert Into @DataTable Values ('2012-01-01 16:25:42',6)
Insert Into @DataTable Values ('2012-01-01 16:25:52',20)
Insert Into @DataTable Values ('2012-01-01 16:26:02',10)
Insert Into @DataTable Values ('2012-01-01 16:26:12',16)
Insert Into @DataTable Values ('2012-01-01 16:26:22',14)
Insert Into @DataTable Values ('2012-01-01 16:26:32',10)
Insert Into @DataTable Values ('2012-01-01 16:26:42',6)
Insert Into @DataTable Values ('2012-01-01 16:26:52',9)
Insert Into @DataTable Values ('2012-01-01 16:27:02',7)
Insert Into @DataTable Values ('2012-01-01 16:27:12',7)
Insert Into @DataTable Values ('2012-01-01 16:27:22',17)


Declare @ResultTable Table(_lowerBound DateTime ,_UpperBound DateTime,_boundInsideContentCount int)

Declare Cur_DataTable Cursor Local For
Select _Date
,_Value
From @DataTable
Order By _Date

Open Cur_DataTable

Declare @Date DateTime
,@Value float
,@flag bit
,@lowerBound DateTime
,@upperBound DateTime
,@constComparisonValue float
,@boundInsideContentCount int

Set @boundInsideContentCount = 0
Set @constComparisonValue = 10
Set @flag = 0
Fetch Next From Cur_DataTable Into @date,@value
while(@@FETCH_STATUS = 0)
Begin
IF @Value < @constComparisonValue And @flag = 1
Set @boundInsideContentCount = @boundInsideContentCount + 1

IF @Value < @constComparisonValue And @flag = 0
Begin
Set @lowerBound = @Date
Set @upperBound = @lowerBound
Set @boundInsideContentCount = 1
Set @flag = 1
End

IF @Value >= @constComparisonValue And @flag = 1
Begin
Insert Into @ResultTable Values(@lowerBound,@upperBound,@boundInsideContent Count)
Set @flag = 0
End
Set @upperBound = @date
Fetch Next From Cur_DataTable Into @date,@value
End

Close Cur_DataTable
Deallocate Cur_DataTable

Select _lowerBound
,_UpperBound
,_boundInsideContentCount
From @ResultTable

alimooghashang
جمعه 03 شهریور 1391, 12:03 عصر
ممنون
ولی من میخواستم یه کوئری بنویسم نه while و این چیزا!
اگه بخوام اینجا while بنویسم خب چه کاریه ، توی برنامه while مینویسم و پردازش میکنم
بازم ممنون

Reza_Yarahmadi
شنبه 04 شهریور 1391, 14:46 عصر
در حال حاضر دسترسی به SQL Server ندارم ولی با کد زیر شاید کارتون راه بیوفته
;With tbl (ID, Cnt, DT) as(
Select TOP 1 ID, Cnt = Case When Number < 10 Then 1 Else 0 End, DT From @MyTable
Union All
Select
ID = Case When Number < 10 Then T.ID Else MT.ID + 1 End,
Cnt = Case When Number < 10 Then Cnt + 1 Else 0 End,
MT.DT
From
@MyTable MT INNER JOIN Tbl T
ON
MT.ID = T.ID + T.cnt)
Select MAX(Cnt) as [Count], MIN(DT) as [From], MAX(DT) as [To]
From Tbl
Group By ID
Having MAX(Cnt) <> 0