שאלה

DarkVera

New member
שאלה

רשמתי את השאילתא הזאת
select sum(amount)/12
from Table_1
where type = 1
group by DATEPART ( month , date )
MINUS
select max(amount)
from Table_1
where type = 1
group by DATEPART ( month , date )
minus
select min(amount)
from Table_1
where type = 1
group by DATEPART ( month , date )​

ומופיע לי הודעת שגיאה Incorrect syntax near 'MINUS' .
לא הצלחתי להבין למה מופיע השגיאה
אשמח לעזרה
 

i t a i b

New member
תנסה ככה

select sum(amount)/12
from Table_1
where type = 1
group by DATEPART ( month , date ) as MINUS

UNION
select max(amount)
from Table_1
where type = 1
group by DATEPART ( month , date ) as minus
UNION
select min(amount)
from Table_1
where type = 1
group by DATEPART ( month , date ) as c
 

גרי רשף

New member
באיזה כלי אתה משתמש?

האופרטור Minus קיים באוראקל,
אך ב-SQL Server משתמשים ב-Except.
בכלים אחרים אינני יודע.
 

DarkVera

New member
עזרה קטנה

עכשיו הוא רץ אך לא מוריד לי את המקסימלי והמינימלי
אשמח לעזרה

select sum(amount)/12
from Table_1
where type = 1
EXCEPT
select max(amount)
from Table_1
where type = 1
EXCEPT
select min(amount)
from Table_1
where type = 1
 

DarkVera

New member
עזרה

חילקתי ב12 בשביל ממוצע חודשי הוא בכלל לא מתייחס לי לכל ה min ו max גם שאני מוחק את זה ומשאיר רק חלק ראשון עדין מופיע לי אותוט תשובה
 

DarkVera

New member
עוד תרגיל

אני צריך להוציא את כל הרשומות שהסכום שלהם גדול מ 500 .
select *
from Table_1
where 500<

(select sum(amount)
from Table_1
where type = 1
group by DATEPART ( month , date ))

הוא רושם שיש שגיאה Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
 

גרי רשף

New member
נסי כך

Select *
From (Select *,
Sum(amount) Over(Partition By DATEPART (Month ,date )) SumAmount
From Table_1
Where type = 1) T
Where SumAmount>500;
 

DarkVera

New member
תודה עובד . בקשר לשאלה ששאלתי מקודם

ניסיתי לרשום שאילתה שמחשבת ממוצע חודשי ללא הסכום הנמוך והגבוה אך משום מה הוא לא מתייחס לחלק של מציאת מינימום ומקסימום . מצרף את השאילתא שרשמתי

select sum(amount)/12
from Table_1
where type = 1
EXCEPT
select min(amount)
from Table_1
where type = 1
group by DATEPART ( month , date )
EXCEPT
select max(amount)
from Table_1
where type = 1
group by DATEPART ( month , date )​
 

גרי רשף

New member
איך הנתונים נראים?

יש לך 12 שורות, אחת לכל חודש, ומהן צריך להשמיט את המקסימום והמינימום ולהישארמ עם 10 שורות?
יש הרבה שורות (למשל- 10 שורות לכל חודש ובסה"כ 120), וצריך להשמיט מהן את אלו של שני החודשים - זה עם המינימום וזה עם המקסימום (בסה"כ 20 שורות להשמיט)?
 

DarkVera

New member
תשובה

לכל חודש יש 2 שורות ואני צריך לחבר אותם ואז להשמיט את הקטן והגדול ולעשות ממוצע חודשי
 

גרי רשף

New member
נסה כך

With T1 As
(select DATEPART ( month , date ) M,
sum(amount) SAmount
from Table_1
where type = 1
group by DATEPART ( month , date )),
T2 As
(Select *,
Min(SAmount) Over() MnSAmount,
Max(SAmount) Over() MxSAmount
From T1)
Select Avg(SAmount) AvgSAmount
From T2
Where SAmount>MnSAmount
And SAmount<MxSAmount;
ב-T1 מחשבים את הממוצעים החודשיים,
ב-T2 את הממוצע המינימלי והמקסימלי,
ולבסוף את הממוצע של כל אלו שגדולים מהמינימום וקטנים מהמקסימום.
 

DarkVera

New member
תודה. ואשמח לעוד משהו קטן

עשיתי הכנסה לטבלה והוא מכניס לי מספר פעמים כך ששורות חוזרות על עצמם. איך אני פותר את זה.
מצרף את השאילתה שלי
insert into accounts(account_id,name,addr)
select accounts_dail.account_id,accounts_dail.name,accounts_dail.addr
from accounts_dail,accounts
where
accounts.account_id != accounts_dail.account_id
and
accounts.addr != accounts_dail.addr
and
accounts.name != accounts_dail.name​
 

i t a i b

New member
distinct

נסך כך:


insert into accounts(account_id,name,addr)
select distinct accounts_dail.account_id,accounts_dail.name,accounts_dail.addr
from accounts_dail,accounts
where
accounts.account_id != accounts_dail.account_id
and
accounts.addr != accounts_dail.addr
and
accounts.name != accounts_dail.name
&nbsp;
 
למעלה