Filtrare due campi in una vista

mercoledì 09 febbraio 2011 - 08.58
Tag Elenco Tags  VB.NET  |  Windows XP  |  Visual Studio 2008  |  SQL Server 2008

Zagor60 Profilo | Junior Member

Salve,

come si può fare per filtrare i dati del campo( tipo (della seguente tabella) .
Mi spiego meglio, il campo Tipo= NOLO e/o Stallie e inoltre un campo [totalenolo] mi deve sommare tutti i record con tipo= Nolo e un campo [totalestallie] mi deve sommare tutti i record con tipo=stallie .
Avevo provato a fare una sottoquery ma senza risultato.
la tabella è la seguente:
USE [resaDB]
GO

/****** Object: Table [dbo].[TabFattBroker] Script Date: 02/09/2011 08:49:05 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TabFattBroker](
[idbroker] [int] NOT NULL,
[n_vg] [varchar](10) NOT NULL,
[nave] [varchar](50) NOT NULL,
[databl] [datetime] NULL,
[viaggio] [varchar](100) NULL,
[noleggiatore] [varchar](50) NULL,
[n_fattura] [varchar](50) NULL,
[desc_fattura] [varchar](150) NULL,
[importo] [decimal](13, 2) NULL,
[cambio] [decimal](13, 4) NULL,
[tipovaluta] [varchar](50) NULL,
[dataFattura] [datetime] NULL,
[percentuale] [decimal](13, 4) NULL,
[tipo] [varchar](50) NULL,
[commissione] [decimal](13, 4) NULL,
[broker] [varchar](150) NULL,
[totale] [decimal](18, 2) NULL,
[modifica] [bit] NULL,
CONSTRAINT [PK_TabFattBroker] PRIMARY KEY CLUSTERED
(
[idbroker] 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

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[TabFattBroker] WITH CHECK ADD CONSTRAINT [FK_TabFattBroker_TabViaggi] FOREIGN KEY([n_vg], [nave])
REFERENCES [dbo].[TabViaggi] ([n_vg], [nave])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[TabFattBroker] CHECK CONSTRAINT [FK_TabFattBroker_TabViaggi]
GO

ALTER TABLE [dbo].[TabFattBroker] ADD CONSTRAINT [DF_TabFattBroker_importo] DEFAULT ((0)) FOR [importo]
GO

ALTER TABLE [dbo].[TabFattBroker] ADD CONSTRAINT [DF_TabFattBroker_cambio] DEFAULT ((1)) FOR [cambio]
GO

ALTER TABLE [dbo].[TabFattBroker] ADD CONSTRAINT [DF_TabFattBroker_percentuale] DEFAULT ((0)) FOR [percentuale]
GO


Avete qualche idea?
saluti

micto27 Profilo | Senior Member

Prova qualcosa del genere.
Non l'ho provata ma dovrebbe funzionare.

Ciao, Michele

select sum(case Tipo when 'NOLO' then totale else 0 end) as totalenolo, sum(case Tipo when 'Stallie' then totale else 0 end) as totalestallie from [dbo].[TabFattBroker]

Zagor60 Profilo | Junior Member

Grazie, funziona perfettamente.
vedendo la tabella noti qualcosa di sbagliato circa gli indci ecc.
Slt

micto27 Profilo | Senior Member

circa gli indici.... dipende dalle queries che vengono fatte sulla tabella.
Così, al buio, vedo che viene definita la foreign key FK_TabFattBroker_TabViaggi,
di istinto mi verrebbe da definire un indice con le colonne costituenti tale foreign key.

Ciao, Michele

Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5