select a.*, b.haydung
from CTMAUBOCVATTU a, vattu b
where a.mavt = b.mavt
This are two sample screenshots from this query:
This result is Ok .
This second result is not OK.
I want to get the list row same name: MADDK have condition exit 1 or column have value haydung=1
SELECT
a.MADDK, a.MAVT, a.NOIDUNG, a.SOLUONG, a.GIAVT, a.TIENVT,
a.GIANC, a.TIENNC, b.haydung
FROM
CTMAUBOCVATTU a
JOIN
vattu b ON a.mavt = b.mavt
WHERE
a.MADDK IN (SELECT MADDK
FROM CTMAUBOCVATTU
WHERE haydung = 0)
AND b.haydung = 1
GROUP BY
a.MADDK, a.MAVT, a.NOIDUNG, a.SOLUONG, a.GIAVT, a.TIENVT,
a.GIANC, a.TIENNC, b.haydung
HAVING
COUNT(*) = 0;
This is my query but it doesn't work.
CREATE TABLE [dbo].[CTMAUBOCVATTU]
(
[MADDK] [varchar] (11) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[MAVT] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[NOIDUNG] [nvarchar] (3000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SOLUONG] [decimal] (18, 2) NULL,
[GIAVT] [decimal] (18, 0) NULL,
[TIENVT] [decimal] (18, 0) NULL,
[GIANC] [decimal] (18, 0) NULL,
[TIENNC] [decimal] (18, 0) NULL,
[ISCTYDTU] [bit] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CTMAUBOCVATTU]
ADD CONSTRAINT [PK_MAUBOCVATTU]
PRIMARY KEY CLUSTERED ([MADDK], [MAVT]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CTMAUBOCVATTU]
ADD CONSTRAINT [FK_CTMAUBOCVATTU_MAUBOCVATTU]
FOREIGN KEY ([MADDK]) REFERENCES [dbo].[MAUBOCVATTU] ([MADDK])
GO
ALTER TABLE [dbo].[CTMAUBOCVATTU]
ADD CONSTRAINT [FK_MAUBOCVATTU_VATTU]
FOREIGN KEY ([MAVT]) REFERENCES [dbo].[VATTU] ([MAVT])
CREATE TABLE [dbo].[VATTU]
(
[MAVT] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[MAHIEU] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[TENVT] [nvarchar] (500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DVT] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[GIAVT] [decimal] (19, 0) NULL,
[GIANC] [decimal] (19, 0) NULL,
[NGAYAD] [smalldatetime] NULL,
[MANHOM] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[HAYDUNG] [bit] NULL,
[MANVN] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[THUEVAT] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[VATTU]
ADD CONSTRAINT [PK_VATTU]
PRIMARY KEY CLUSTERED ([MAVT]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[VATTU]
ADD CONSTRAINT [FK_VATTU_DVT]
FOREIGN KEY ([DVT]) REFERENCES [dbo].[DVT] ([DVT])
GO
ALTER TABLE [dbo].[VATTU]
ADD CONSTRAINT [FK_VATTU_NHANVIEN]
FOREIGN KEY ([MANVN]) REFERENCES [dbo].[NHANVIEN] ([MANV])
GO
ALTER TABLE [dbo].[VATTU]
ADD CONSTRAINT [FK_VATTU_NHOMVATTU]
FOREIGN KEY ([MANHOM]) REFERENCES [dbo].[NHOMVATTU] ([MANHOM])
GO
1条答案
按热度按时间ac1kyiln1#
As I understand your question, you want to retrieve all rows having a MADDK value where at least one row with the same MADDK value has haydung = 1.
In your first data set, the second row has haydung = 1, so all rows with MADDK = 150 should be selected. In your second data set, no rows have haydung = 1, so rows with MADDK = 50DTD100 should not be selected.
How about:
There may be better ways of writing the above using Common Table Expressions (CTEs) or window functions, but I believe the above should accomplish the task without getting overly complicated.