 |
ช่วยดู select ข้อมูล 2 เงื่อนไขโดยมี count และ group by ให้หน่อยครับ |
|
 |
|
|
 |
 |
|
 
|
 |
 |
 |
 |
Date :
2016-05-12 10:01:09 |
By :
kongbeng_36 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (SQL)
select
Customer_Type_id ,
count(Customer_id) as all_Customer,
sum(Status=1) as status1,
sum(Status=2) as status2
from ms_Customer
GROUP BY Customer_Type_id
|
 |
 |
 |
 |
Date :
2016-05-12 10:23:21 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Incorrect syntax near '='.
ขึ้นว่า error แบบนี้อ่ะครับ ผมต้องแก้ยังไงหรอครับ
|
 |
 |
 |
 |
Date :
2016-05-12 10:51:02 |
By :
kongbeng_36 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สงสัยอีกข้อครับ ผมคิดว่า SUM() ไม่น่าจะใช้ได้นะครับ ที่อยากได้คือ นับบรรทัดว่ามี satus = 1 กี่แถวกี่คน satus = 2 กี่แถวกี่คน ถ้าเกิด sum 2 สัก 3 คนค่าจะออกมาเป้น 6 ครับ รบกวนพี่ ๆ ช่วยดูให้หน่อยครับ
|
 |
 |
 |
 |
Date :
2016-05-12 11:00:34 |
By :
kongbeng_36 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อุ๊ป ขอโทษ mssql
sum( case when Status=1 then 1 else 0 end)
แก้อันอื่นด้วยครับ
|
 |
 |
 |
 |
Date :
2016-05-12 11:17:13 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วครับ ขอบคุณมากค้าบบบบบบบบบบ
Code (SQL)
select Customer_Type_id
,count(case when ms_Customer.Status = 1 then 1 else null end) as status1
,count(case when ms_Customer.Status = 2 then 2 else null end) as status2
from ms_Customer
GROUP BY Customer_Type_id
|
 |
 |
 |
 |
Date :
2016-05-12 11:52:11 |
By :
kongbeng_36 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 4 เขียนโดย : kongbeng_36 เมื่อวันที่ 2016-05-12 11:00:34
รายละเอียดของการตอบ ::
ได้ทดสอบใช้งานหรือยังครับ
sum(Status=2) ไม่ใช่ เป็นการกำหนดค่า 2 ให้ กับ status ครับ
แต่เป็นการเปรียบเทียบ อยู่ในส่วนของการคำนวณ ไม่ได้อยู่ในส่วน ของคำสั่ง set
ถ้าเป็นจริงจะได้ค่า 1 แต่เป็นของ mysql ใช้กับ mssql ไม่ได้
ส่วนข้างล่างเป็นการทดสอบคำสั่ง sum ครับ ใช้ count ก็ได้ครับ เพราะมีค่า null เลยไม่ได้ถูกนับ
ก็อยู่ที่วิธีการเขียนแหล่ะครับ ใช้คำสั่งได้ถูกต้อง อะไรก็ใช้ได้
Code (SQL)
select A,
SUM(case when b=1 then 1 else 0 end) as b1,
SUM(case when b=2 then 1 else 0 end) as b2
from (
select '1' as A, 1 as b union all
select '1' , 2 union all
select '2' , 2 union all
select '2' , 1 union all
select '1' , 2 union all
select '1' , 2 union all
select '2' , 2 union all
select '2' , 1 union all
select '2' , 2
) as tmp group by A
|
 |
 |
 |
 |
Date :
2016-05-12 19:34:58 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|