I want to do a select in a table named ALBARANES that it's going to show me all
the columns of the table. This select has inputs values to do the where clauses.
Inputs: #serie, #client
What I want is: If #serie's value is null then show me all the columns regardles of the column value of ALBARANES.SERIE, but if #serie has a value not null then do a clause using it.
SELECT * FROM ALBARANES AC
WHERE
AC.SERIE = #serie/*Code when #serie has value not null*/
AC.FECHA < (SELECT CAST ('Now' as date) from RDB$DATABASE)
And the same with the input #client. How can I implement this select?
You want:
WHERE (AC.SERIE = #serie or #serie is null) and
ac.fecha < date(now())
Your question is tagged MySQL. However, your date logic is not MySQL, so I changed it. If your question is mistagged, then you may want your original logic.
Related
I used a CTE (temp table) to compute data through semesters. With my query, I want to compare if my data (already aggregated) are equal to my sum computed in my CTE. However, when using WHERE data_already_aggregated <> data_computed I got a result where my results in those columns are equal. I tried to use =! as well, and also =. Last option worked, but not the way I want.
Here my code:
WITH trimestriel AS(
SELECT "donnéestrim".annee,
"donnéestrim".trimestre,
"donnéestrim".codescpi,
scpi.scpi,
"donnéestrim"."rd_t",
SUM("donnéestrim".rd_t) OVER (PARTITION BY "donnéestrim".annee, "donnéestrim".codescpi) AS "trim_sum_totalYear_rd_t",
row_number() over (partition BY scpi.codescpi, annee) AS "row_number"
FROM "donnéestrim"
LEFT JOIN scpi ON scpi.codescpi = "donnéestrim".codescpi
ORDER BY "donnéestrim".annee)
SELECT "annuel".codescpi,
scpi.scpi ,
"annuel".annee,
trimestriel."trimestre",
"annuel".revdisavpl AS "annuel_revdisavpl",
"trim_sum_totalYear_rd_t",
"rd_t" AS "trim_rd_t"
FROM "annuel"
LEFT JOIN scpi ON scpi.codescpi = "annuel".codescpi
LEFT JOIN trimestriel ON trimestriel.codescpi = "annuel".codescpi AND trimestriel.annee=annuel.annee
WHERE "annuel".annee = '2018' and scpi.codescpi = '129' and "annuel".revdisavpl != "trim_sum_totalYear_rd_t"
result
As #JNevill suggested, it's a data type problem. A double precision type only display the data with its specific parameters, but can store more information if casted. I cast it to real, and I can saw the gaps between my data.
My first table (actually a view) is:
SELECT * FROM VW_MAIN_INFO
My second table is:
SELECT * FROM TBL_POINTS_AND_CYCLES
In a query, I combine both like this:
SELECT TP.TYPE,VMI.*
FROM VW_MAIN_INFO VMI,
TBL_POINTS_AND_CYCLES TP
WHERE VMI.START_INLET_TEMP=TP.TEMP1
AND VMI.START_OUTLET_TEMP=TP.TEMP2
AND VMI.TIME_FORMATTED=CONVERT(DATETIME, TP.DATE, 101)
What you can tell, what really matters for me in the second table (TBL_POINTS_AND_CYCLES) is the field "TYPE".
What do I need help with:
I need to return everything from VW_MAIN_INFO and TYPE (from TBL_POINTS_AND_CYCLES).
However, if I cannot find a type in TBL_POINTS_AND_CYCLES, I should return a specific value (for example, "EMPTY" or null).
How can I achieve? Is the best path to use "minus" like this?
Finally, my problem with minus is that I don't have the same structure in both tables.
Any help? Ideas?
Thank you.
SELECT TP.TYPE ,
VMI.*
FROM VW_MAIN_INFO VMI
LEFT JOIN TBL_POINTS_AND_CYCLES TP ON VMI.START_INLET_TEMP = TP.TEMP1
AND VMI.START_OUTLET_TEMP = TP.TEMP2
AND VMI.TIME_FORMATTED = CONVERT(DATETIME, TP.DATE, 101);
I have some data
I need result of query like this:
With this query :
select
early.sensor,
early.event,
late.value - early.value as value
from data_table as early
inner join data_table as late on
early.sensor=late.sensor and early.event=late.event
where late.event_date > early.event_date;
I get this result:
Assuming that the input table is called "data_table" and the fourth column in it is called event_date, then something like this should do it:
select
early.sensor,
early.event,
late.value - early.value as value
from
data_table as early
inner join data_table as late on
early.sensor=late.sensor
and early.event=late.event
where
late.event_date > early.event_date;
But if there are more than two values (as #Thorston Kettner asks), then you will need some more complex logic.
I need to determine if a certain field value in a database table occurs before a certain date, but not after that date.
I can determine the values that occur before the cutoff date with a simple select, but there may be records after that date.
The field values that I am using are the 'entereddate' and the value I am looking for (in this case a carriercode).
Thanks for your help!
This is the best I can do without seeing the data structure.
SELECT *
FROM BillTBL a
INNER JOIN carriertbl b ON a.carrier_key = b.carrier_key
WHERE a.billentereddate < '2009-09-01'
AND NOT EXISTS (SELECT 1
FROM BillTBL
WHERE whatever_the_key_is = a.whatever_the_key_is
AND billentereddate > '2009-09-01')
select a.carriercode
from carriertbl as a
inner join BillTBL as b ON b.carrier_key = a.carrier_key and b.enteredate < '2009-09-01'
Maybe you have to ajust some column name...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the record of a table who contains the maximum value?
I've got an aggregate query like the following:
SELECT TrainingID, Max(CompletedDate) as CompletedDate, Max(Notes) as Notes --This will only return the longest notes entry
FROM HR_EmployeeTrainings ET
WHERE (ET.AvantiRecID IS NULL OR ET.AvantiRecID = #avantiRecID)
GROUP BY AvantiRecID, TrainingID
Which is working, and returns correct data most of the time, but I noticed a problem. The Notes field which gets returned will not necessarily match the record that the max(completedDate) is from. Instead it will be the one with the longest string? Or the one with the highest ASCII value? What does SQL Server do in the event of a tie between two records? I'm not even sure. What I want to get is the notes field from the max(completedDate) record. How should I got about doing this?
You can use a subquery. The subquery will get the Max(CompletedDate). You then take this value and join on your table again to retrieve the note associate with that date:
select ET1.TrainingID,
ET1.CompletedDate,
ET1.Notes
from HR_EmployeeTrainings ET1
inner join
(
select Max(CompletedDate) CompletedDate, TrainingID
from HR_EmployeeTrainings
--where AvantiRecID IS NULL OR AvantiRecID = #avantiRecID
group by TrainingID
) ET2
on ET1.TrainingID = ET2.TrainingID
and ET1.CompletedDate = ET2.CompletedDate
where ET1.AvantiRecID IS NULL OR ET1.AvantiRecID = #avantiRecID
Ah yes, that is how it is intended in SQL. You get the Max of every column seperately. It seems like you want to return values from the row with the max date, so you have to select the row with the max date. I prefer to do this with a subselect, as the queries keep compact easy to read.
SELECT TrainingID, CompletedDate, Notes
FROM HR_EmployeeTrainings ET
WHERE (ET.AvantiRecID IS NULL OR ET.AvantiRecID = #avantiRecID)
AND CompletedDate in
(Select Max(CompletedDate) from HR_EmployeeTrainings B
where B.TrainingID = ET.TrainingID)
If you also want to match by AntiRecID you should include that in the subselect as well.
Each MAX function is evaluated individually. So MAX(CompletedDate) will return the value of the latest CompletedDate column and MAX(Notes) will return the maximum (i.e. alphabeticaly highest) value.
You need to structure your query differently to get what you want. This question had actually already been asked and answered several times, so I won't repeat it:
How to find the record in a table that contains the maximum value?
Finding the record with maximum value in SQL
There's no easy way to do this, but something like this will work:
SELECT ET.TrainingID,
ET.CompletedDate,
ET.Notes
FROM
HR_EmployeeTrainings ET
inner join
(
select TrainingID, Max(CompletedDate) as CompletedDate
FROM HR_EmployeeTrainings
WHERE (ET.AvantiRecID IS NULL OR ET.AvantiRecID = #avantiRecID)
GROUP BY AvantiRecID, TrainingID
) ET2
on ET.TrainingID = ET2.TrainingID
and ET.CompletedDate = ET2.CompletedDate