Jump to content

Codigo SQL para criar uma tabela resumo dinâmica


Tesla
 Share

Recommended Posts

Boas.

Estou a tentar criar uma tabela resumo dinâmica.

Ela deverá ter na primeira coluna os locais de venda, e nas colunas deverão ser as vendas por dia, onde o titulo de cada uma dessas colunas será o dia a que as vendas se reportam.

Uma coisa deste género:

[table]

[/table]

De notar que onde temos os dias da semana, deveriam aparecer os dias do mês, de forma dinâmica.

Obrigado pela vossa ajuda.  👍

Assinatura editada pelo staff, por favor consulte as regras.

Link to comment
Share on other sites

"Dinâmica"? Mas queres alterar o esquema da tabela diariamente? Isso não é recomendável, imho.

Faz uma tabela com a coluna POS e as colunas para cada dia do mês, e depois no programa que trabalhar com a BD, pedes só as colunas de dia 1 até ao dia actual.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Boas.

Por dinâmica quero dizer que, onde diz Segunda Feira, coloque a data no formato DD-MM-AAAA.

A ideia é ter uma tabela semanal de vendas. Se que a correr ela vai ver todas as vendas de todos os sites feitos entre a segunda feira dessa semana e o dia em que estamos dessa semana também.

O meu problema está em conseguir com que o SQL crie as colunas dos dias e coloque lá os somatórios correctos.

Obrigado, desde já, pelo interesse em ajudar! 🙂

Assinatura editada pelo staff, por favor consulte as regras.

Link to comment
Share on other sites

Hmm, mesmo nesse caso, também não me parece que seja essa a melhor solução.

Porque não fazes uma tabela:  POS | Data | Vendas

E depois o programa lê os dias que quer para a POS que quiser.

Depois fazes um select entre duas datas:

SELECT Vendas WHERE POS = Lisboa AND Data between ('2004-12-03') and ('2004-12-12')

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Ah, pensei que estavas a fazer um programa de raiz. Sendo assim não te consigo ajudar 🙂

Se compraste uma solução de um distribuidor da PHC, devias enviar-lhes um email a expor o teu problema. Eles têm a obrigação de te suportar as adaptações que tu necessites para o teu negócio.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Não precisas de nenhuma tabela dinamica. Precisas sim de um codigo que coloque o total do dia em coluna/semana/mes/ano

vê o seguinte codigo sobre uma BD PHC e adapta-o às tuas necessidades (este codigo é para procurar compras por mês  para o presente ano). Se não quiseres fazer isto em SQL deves então usar o VFP do lado do client.

PS: sempre podes pedir ajudar na comunidade da phc, pode ser que te safes .... 🙂

TSQL

...
DECLARE  @ref_1     CHAR(18)
DECLARE c_1 Cursor For
Select Distinct top 1 
	Ref 
From St (Nolock)
OPEN c_1              
FETCH NEXT FROM c_1 INTO @ref_1
WHILE @@Fetch_status = 0  
BEGIN 
BEGIN TRANSACTION 
	If (select count(*) From fn (Nolock) inner join fo (Nolock) on fo.fostamp=fn.fostamp  inner join cm1 (Nolock) on cm1.cm=fo.doccode  where Fn.ref=@Ref_1)>0
	Begin
		Insert Into #1
		select Distinct
			st.REF,
			st.Design,
			st.unidade,
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=1  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) Janeiro, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=2  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) fevereiro, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=3  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) Março, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=4  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) abril, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=5  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) maio, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=6  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) junho, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=7  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) julho, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=8  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) julho, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=9  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) agosto, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=10  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) setembro, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=11  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) outubro, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())		And Month(datalc)=12  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) novembro, 
			Isnull((Select sum(qtt) from Sl (Nolock) Where year(datalc)=Year(getdate())							  and ref=@ref_1 and cm in (1,2,5,9,13,14) ),0) dezembro
		from St (Nolock)
		where 
			st.ref=@Ref_1
		group by 
			st.REF,
			st.design,
			st.unidade
		order by 
			st.REF
	End
FETCH NEXT FROM c_1 INTO @ref_1
COMMIT TRANSACTION 
END
CLOSE      c_1 
DEALLOCATE c_1 


...
Link to comment
Share on other sites

Criar ou alterar uma tabela "on the fly" é fácil, o problema aqui é que me parece que estás a ter um erro de lógica e o que  queres é uma linha e não uma nova coluna na tabela, tal como o IceBrain já referiu.

Estás a construir um "mini data warehouse" ou algo assim? Isso que estás a referir soa muito ao cruzamento de uma tabela de factos com uma tabela de dimensão...

10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.