473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

query group by anno multi colonna

Ciao ho una tabella in sql server formata da questi campi

società, valorefattura, datafattura

Vorrei estrarre i dati in questo modo

anno anno -1
anno -2
Società somma(valorefattura) somma(valorefattura)
somma(valorefattura)
Potete aiutarmi a costruire la query?

Ciao grz by Logan bye bye
Jul 20 '05 #1
4 5722
Logan wrote:
Ciao ho una tabella in sql server formata da questi campi

società, valorefattura, datafattura

Vorrei estrarre i dati in questo modo

anno anno -1
anno -2
Società somma(valorefattura) somma(valorefattura)
somma(valorefattura)
Potete aiutarmi a costruire la query?
....la query può essere risolta con

==================================
SELECT Società,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) THEN valorefattura
ELSE 0
END,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) -1 THEN
valorefattura
ELSE 0
END,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) -2 THEN
valorefattura
ELSE 0
END
FROM MyTable
GROUP BY Società
==================================
Ciao grz by Logan bye bye


Ciao

--
Luca Bianchi
Microsoft MVP - SQL Server
http://mvp.support.microsoft.com
http://italy.mvps.org
Jul 20 '05 #2

"Luca Bianchi" <ri******************@hotmail.com> ha scritto nel messaggio
news:bm************@ID-201973.news.uni-berlin.de...
Logan wrote:
Ciao ho una tabella in sql server formata da questi campi

società, valorefattura, datafattura

Vorrei estrarre i dati in questo modo

anno anno -1
anno -2
Società somma(valorefattura) somma(valorefattura)
somma(valorefattura)
Potete aiutarmi a costruire la query?
...la query può essere risolta con

==================================
SELECT Società,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) THEN

valorefattura ELSE 0
END,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) -1 THEN
valorefattura
ELSE 0
END,
SUM(CASE
WHEN YEAR(datafattura) = YEAR(GETDATE()) -2 THEN
valorefattura
ELSE 0
END
FROM MyTable
GROUP BY Società
==================================
Ciao grz by Logan bye bye


Ciao

--
Luca Bianchi
Microsoft MVP - SQL Server
http://mvp.support.microsoft.com
http://italy.mvps.org


Grazie 1k è proprio quello che mi serviva

Ciao by Logan bye bye
Jul 20 '05 #3
Hi Logan

This is an English language news group and I don't speak Itallian, so I hope
your post isn't in Spanish!!!

Check out for a crosstab that will transform your rows to columns:

http://support.microsoft.com/default...;EN-US;q175574

You will also need to use a derived query to group the data into years

SELECT società, YEAR(datafattura), SUM(valorefattura)

FROM MyTable

GROUP BY società, YEAR(datafattura)

This is what Altavista says!

Ciò è un gruppo di notizie di lingua inglese e non parlo italiano, in modo
da spero il vostro alberino non sono nello Spagnolo!!!
Verific un crosstab che trasformerà le vostre file alle colonne:
http://support.microsoft.com/default...;EN-US;q175574
Inoltre dovrete usare una domanda derivata per raggruppare i dati negli anni
SELECT società, YEAR(datafattura), SUM(valorefattura)
FROM MyTable

GROUP BY società, YEAR(datafattura)

John

"Logan" <lo*******@hotmail.com> wrote in message
news:bm************@ID-141292.news.uni-berlin.de...
Ciao ho una tabella in sql server formata da questi campi

società, valorefattura, datafattura

Vorrei estrarre i dati in questo modo

anno anno -1
anno -2
Società somma(valorefattura) somma(valorefattura)
somma(valorefattura)
Potete aiutarmi a costruire la query?

Ciao grz by Logan bye bye

Jul 20 '05 #4
Logan wrote:
Grazie 1k è proprio quello che mi serviva

Ciao by Logan bye bye


....per il futuro ricordati che il cross-posting non è gradito o perlomeno
dovresti indicarlo nell'oggetto...

Ciao

--
Luca Bianchi
Microsoft MVP - SQL Server
http://mvp.support.microsoft.com
http://italy.mvps.org
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
1
by: Jillian Cee | last post by:
I have a multiselect list box (simple). I need to find out how to get Access to recognise my multiple selections firstly (I believe) then I want these selections to go into a query so that I can...
2
by: Zlatko Matiæ | last post by:
Hello. How to reference selected values from a multi-select list box, as a criteria in a query ? Is it possible at all? Regards, Zlatko
5
by: Dave Smithz | last post by:
Hi there, Been working on an evolving DB program for a while now. Suddenly I have come across a situation where I need to update a table based on a group by query. For example, I have a table...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
14
by: Tina | last post by:
My employer tracks productivity/performance of clinicians (how much they bill) each week, its averages for the month, and the 6 months. These averages are compared to their expected productivity....
4
by: Andy_Khosravi | last post by:
I'm trying to build a search utility for users to find 'inquiries' in my database that involves several tables. This is normally easy to do with the query builder, but I have a unique situation...
0
by: fitzini | last post by:
Hi all, I need to update multiple rows using a sub query. I've pasted sample code below to hopefully help recreate what I'm trying to do. drop table dummy_test create table dummy_test (cmr...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.