How can I filter a crosstab query in Access? - crosstab

Folks,
I have this crosstab query, It shows all the budgets I have in my database, What I need to do its to have a filter so I can just see the Id Budget I choice.
THis is my sql code:
Prespuesto=Budget
TRANSFORM Sum(qGrafpv.Importe_CD) AS SumaDeImporte_CD
SELECT qGrafpv.[Id Presupuesto], qGrafpv.[Orden Partida], qGrafpv.[Nombre Partida], Sum(qGrafpv.Importe_CD) AS [Total de Importe_CD]
FROM qGrafpv
GROUP BY qGrafpv.[Id Presupuesto], qGrafpv.[Orden Partida], qGrafpv.[Nombre Partida]
PIVOT Format([MínDeComienzo],"yyyy-mm");

Related

How to keep one record in specific column and make other record value 0 in group by clause in PostgreSQL?

I have a set of data like this
The Result should look Like this
My Query
SELECT max(pi.pi_serial) AS proforma_invoice_id,
max(mo.manufacturing_order_master_id) AS manufacturing_order_master_id,
max(pi.amount_in_local_currency) AS sales_value,
FROM proforma_invoice pi
JOIN schema_order_map som ON pi.pi_serial = som.pi_id
LEFT JOIN manufacturing_order_master mo ON som.mo_id = mo.manufacturing_order_master_id
WHERE to_date(pi.proforma_invoice_date, 'DD/MM/YYYY') BETWEEN to_date('01/03/2021', 'DD/MM/YYYY') AND to_date('19/04/2021', 'DD/MM/YYYY')
AND pi.pi_serial in (9221,
9299)
GROUP BY mo.manufacturing_order_master_id,
pi.pi_serial
ORDER BY pi.pi_serial
Option 1: Create a "Running Total" field in Crystal Reports to sum up only one "sales_value" per "proforma_invoice_id".
Option 2: Add a helper column to your Postgresql query like so:
case
when row_number()
over (partition by proforma_invoice_id
order by manufacturing_order_master_id)
= 1
then sales_value
else 0
end
as sales_value
I prepared this SQLFiddle with an example for you (and would of course like to encourage you to do the same for your next db query related question on SO, too :-)

What does filter mean?

select driverid, count(*)
from f1db.results
where position is null
group by driverid order by driverid
My thought: first find out all the records that position is null then do the aggregate function.
select driverid, count(*) filter (where position is null) as outs
from f1db.results
group by driverid order by driverid
First time, I meet with filter clause, not sure what does it mean?
Two code block results are different.
Already googled, seems don't have many tutorials about the FILTER. Kindly share some links.
A more helpful term to search for is aggregate filters, since FILTER (WHERE) is only used with aggregates.
Filter clauses are an additional filter that is applied only to that aggregate and nowhere else. That means it doesn't affect the rest of the columns!
You can use it to get a subset of the data, like for example, to get the percentage of cats in a pet shop:
SELECT shop_id,
CAST(COUNT(*) FILTER (WHERE species = 'cat')
AS DOUBLE PRECISION) / COUNT(*) as "percentage"
FROM animals
GROUP BY shop_id
For more information, see the docs
FILTER ehm... filters the records which should be aggregated - in your case, your aggregation counts all positions that are NULL.
E.g. you have 10 records:
SELECT COUNT(*)
would return 10.
If 2 of the records have position = NULL
than
SELECT COUNT(*) FILTER (WHERE position IS NULL)
would return 2.
What you want to achieve is to count all non-NULL records:
SELECT COUNT(*) FILTER (WHERE position IS NOT NULL)
In the example, it returns 8.

How to create two virtual columns by different conditions from repeating row data using SQL?

My database table has three columns: [Date, Client, Value][1].
I want a new table with only three clients in it - "Technics", "Metal Inc", "Sunny Day" and two virtual total columns - "Price" / August, and "Price" / 2017.
What I tried so far and what I get: Click to see
Why does the "For August" total SUM goes down the next row, but not in a new column?
After all, my code says: SELECT SUM(Value) AS 'For August'.
Any ideas?
Union will give you rows instead of columns, for this case maybe you can use subqueries, try something like this
SELECT
t1.Client, SUM(t1.Value) AS 'For 2017', (SELECT SUM(t2.Value)FROM Test GROUP
BY t2.Client)AS 'For August'
FROM Test AS t1
JOIN Test AS t2
ON t1.Client = t2.Client
WHERE
t2.Date LIKE '%2017-08%'AND
(t2.CLient LIKE '%Technics%' OR t2.CLient LIKE
'%Metal Inc%' OR t2.CLient LIKE '%Sunny Day%')
AND
(t1.CLient LIKE '%Technics%'
OR t1.CLient LIKE '%Metal Inc%'
OR t1.CLient LIKE '%Sunny Day%')
GROUP BY t1.Client

how to achieve Tableau from SQL statement

Please can anyone help out on how to achieve sql statement in tableau. I have a table in Tableau that contains ID name smkoes SmokeCessRef ward etc as the dimension in tableau. In SQL i query the table as follows:
SELECT
count(Distinct ID) as TotSmokeExcl
FROM IP
WHERE
(Smokes in ('Yes') and SmokeCessRef in ('No'))
OR
(Smokes in ('Yes') and SmokeCessRef in ('No') and [Disch/LastWard] in ('Camp','House'))
How can i do this in tableau. or what process do i need to do to achieve this in Tableau
Create a calculated field with similar logic to that which you have in SQL.
([Smokes] = 'Yes' AND [SmokeCessRef] = 'No') OR
([Smokes] = 'Yes' AND [SmokeCessRef] = 'No' AND ([Disch/LastWard] = 'Camp' OR [Disch/LastWard] = 'House'))
Then put the calculated field in the filter card and set it to 'True'.
As an aside, your logic doesn't completely make sense. For instance, you are going to return all (Smokes in ('Yes') and SmokeCessRef in ('No')), so the second part (Smokes in ('Yes') and SmokeCessRef in ('No') and [Disch/LastWard] in ('Camp','House')) is useless.

How to write date filter in MDX where clause?

I am new to MDX. Could please suggest how to write below T-SQL query in MDX Query language.
T-SQL:
SELECT wp.date,Sum(wp.bbls_oil)
AS BBLSOIL_TOTAL,Sum(wp.bbls_water)
AS BBLSWATER_TOTAL,Sum(wp.mcf_prod)
AS MCF_PROD_TOTAL,Sum(wp.vent_flare)
AS VENT_FLARE_TOTAL
FROM well_prod_bst_horiz_og_2_yrs wp, well_index wi
WHERE wp.fileno = wi.fileno
AND wp.date <= :startDate
AND wp.date >= :endDate
AND wi.apino IN (:wellids)
GROUP BY wp.date ORDER BY wp.date ASC";
In the above query, Start and End date values are supplied dynamically.
Assuming you have measures named BBLSOIL, BBLSWATER, MCF_PROD, and VENT_FLARE_TOTAL and your date attribute is named [Date].[Date], and your :startDate contains [Date].[Date].&[20120101] and your :endDate contains [Date].[Date].&[20141231], and your cube is named Name of your Cube you would write
SELECT {
Measures.[BBLSOIL],
Measures.[BBLSWATER],
Measures.[MCF_PROD],
Measures.[VENT_FLARE_TOTAL]
}
ON COLUMNS,
[Date].[Date].&[20120101] : [Date].[Date].&[20141231]
ON ROWS
FROM [Name of your Cube]
i. e. you put an MDX set containing the list of required measures on the columns axis and you put a range (specified by :) on the rows axis. Aggregations like Sum and GROUP BY are not necessary inn MDX, these are handled by the cube definition.