|
|
|
สอบถามปัญหาการเขียนคิวรี่หลายๆ เงื่อนไข 3 ตัวนี้มาเขียนในอันเดียวมันจะได้มั้ยครับ เเละต้องเขียนยังไงครับ |
|
|
|
|
|
|
|
ยังงงๆ อยู่ กลัวตอบผิด
|
|
|
|
|
Date :
2011-02-15 20:48:38 |
By :
หางอึ่ง |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
รวม query ที่ 1 และ 2 ทำได้ค่ะ ส่วนอันที่ 3 ก้อแค่เติม field update ต่อๆกันไป
ไม่อธิบายนะคะ เพราะคุณก้อให้แค่ข้อมูลสั้นๆ ไม่มีอะไรให้นอกจากที่เห็น
ทั้งหมดนี้ run ใน sql server management studio ค่ะ ไปแปลงเอาด้วยละกันค่ะ
Code (C#)
-- GENERATE SECTION
/****** Object: Table [dbo].[house] Script Date: 02/15/2011 22:36:15 ******/
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[house]') AND type in (N'U'))
DROP TABLE [dbo].[house]
GO
USE [test] -- Change to your database name
GO
/****** Object: Table [dbo].[house] Script Date: 02/15/2011 22:36:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[house](
[id] [int] NOT NULL,
[parcel_code] [int] NULL,
[building_width] [real] NULL,
[building_height] [real] NULL,
CONSTRAINT [PK_house] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF
, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON
, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
USE [test]
GO
/****** Object: Table [dbo].[land] Script Date: 02/15/2011 22:36:34 ******/
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[land]') AND type in (N'U'))
DROP TABLE [dbo].[land]
GO
USE [test]
GO
/****** Object: Table [dbo].[land] Script Date: 02/15/2011 22:36:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[land](
[landid] [int] NOT NULL,
[parcel_code] [int] NULL,
[land_useid] [nvarchar](50) NULL,
[land_rentid] [nvarchar](50) NULL,
[land_value] [real] NULL,
[minus_rai] [real] NULL,
[total_rai] [real] NULL,
[evaluate_tax] [real] NULL ,
[taxed_rai] [real] NULL,
[taxed_ngan] [real] NULL,
[taxed_wa] [real] NULL,
CONSTRAINT [PK_land] PRIMARY KEY CLUSTERED
(
[landid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF
, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON
, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- INSERT SECTION
INSERT INTO [dbo].[land]
([landid]
,[parcel_code]
,[land_useid]
,[land_rentid]
,[land_value]
,[minus_rai]
,[total_rai]
,[evaluate_tax]
,[taxed_rai]
,[taxed_ngan]
,[taxed_wa])
VALUES
(1 ,1 ,'02' ,'00' ,1000 ,0 ,500 ,7 ,4.5 ,3 ,2 ) ,
(2 ,2 ,'02' ,'00' ,300000 ,0 ,320 ,7 ,4.5 ,3 ,2 ) ,
(3 ,3 ,'02' ,'00' ,320000 ,0 ,200 ,7 ,4.5 ,3 ,2 ) ,
(4 ,3 ,'03' ,'00' ,5000 ,0 ,800 ,7 ,4.5 ,3 ,2 ) ,
(5 ,3 ,'01' ,'01' ,56100 ,0 ,2900 ,7 ,4.5 ,3 ,2 ) ,
(6 ,3 ,'04' ,'01' ,9000 ,0 ,800 ,7 ,4.5 ,3 ,2 )
;
GO
INSERT INTO [dbo].[house]
([id]
,[parcel_code]
,[building_width]
,[building_height])
VALUES
(1 ,1 ,20 ,12 ) ,
(2 ,1 ,10 ,5 ) ,
(3 ,1 ,7.4 ,5 ) ,
(4 ,2 ,20 ,5 ) ,
(5 ,2 ,6 ,4 ) ,
(6 ,3 ,10 ,5 ) ,
(7 ,3 ,10 ,40 )
;
GO
WITH [calc_house]([parcel_code] ,[all_building_area]) AS
(
SELECT [parcel_code]
,sum([building_width] * [building_height]) As [all_building_area]
FROM [house]
GROUP BY [parcel_code]
)
UPDATE [land]
SET [land].[minus_rai] =
CASE
WHEN [land].[land_useid] = '02'
THEN [all_building_area]
WHEN (([land].[land_useid]='01') OR ([land].[land_useid] = '03')
OR ([land].[land_useid] = '04') OR ([land].[land_useid] = '05'))
AND ( [land].[land_rentid] = '01') AND ([land].[land_value] > 30000)
THEN 0.25
ELSE 0
END
FROM [land] INNER JOIN [calc_house]
ON [land].[parcel_code] = [calc_house].[parcel_code]
;
SELECT * FROM [land] ;
-- CLEAR ALLv SECTION
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[house]') AND type in (N'U'))
DROP TABLE [dbo].[house]
GO
IF EXISTS (SELECT * FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[land]') AND type in (N'U'))
DROP TABLE [dbo].[land]
GO
|
|
|
|
|
Date :
2011-02-15 23:10:50 |
By :
สาวเอ๋อ (ก้อคนมานเอ๋อ) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
น้องเอ๋อ อ่าน sql เล่มไหนครับ
|
|
|
|
|
Date :
2011-02-15 23:49:30 |
By :
PlaKriM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่กี่เล่มหรอกค่ะ ทะมัยหรอคะพี่ปลาการิม
|
|
|
|
|
Date :
2011-02-16 00:02:03 |
By :
blurEyes |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อยากเก่งมั่ง จะเอามาหนุนนอนให้มันออสโมซิสเข้ากบาล
pm มาเลย เผื่อเจอจะได้หามาหนุนแทนหมอน
|
|
|
|
|
Date :
2011-02-16 00:16:08 |
By :
PlaKriM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|