To_char ,To_date - sql

how do you become conform with this code, only dates of 2019 appear to me? You are not changing To_char.
SELECT GUEST.GNO, GUEST.GLASTNAME, GUEST.GFIRSTNAME, BOOKING.gNO, BOOKING.GINDATE
FROM GUEST, BOOKING
WHERE GUEST.GNO = BOOKING.gNO AND BOOKING.GINDATE = TO_CHAR (GINDATE,'DD/MM/YYYY');

I am interpreting your query as you want bookings in 2019. If so, then query should look more like this:
SELECT g.GNO, g.GLASTNAME, g.GFIRSTNAME, b.gNO, b.GINDATE
FROM GUEST g JOIN
BOOKING b
ON g.GNO = b.gNO
WHERE b.GINDATE >= DATE '2019-01-01' AND
b.GINDATE < DATE '2020-01-01';
Of course, there might be slight variations depending on your database.

Related

Date automatically where clause - SQL

I have on my DB the dates that I can filter like this:
select *
where
a.y=2021 and a.m=2 and a.d=7
However if I run this query tomorrow I'll have to go there and change manually.
Is there a way to do this automatically as in if I run the query tomorrow I'll get d=8 and the day after d=9 and so on?
I tried to use get date but I get the following error:
SQL Error [6]: Query failed (#20210207_153809_06316_2g4as): line 2:7: Function 'getdate' not registered
I also don't know if that is the right solution. Does anybody know how to fix that?
you can use NOW to get the current date, and use YEAR , MONTH , DAY to get parts of the date
SELECT *
FROM [TABLE]
WHERE a.y=YEAR(NOW()) and a.m=MONTH(NOW()) and a.d=DAY(NOW())
The best solution is to have a date column in your data. Then you can just use:
where datecol = current_date
Or whatever your particular database uses for the current date.
Absent that, you have to split the current date into parts. In Standard SQL, this looks like:
where y = extract(year from current_date) and
m = extract(month from current_date) and
d = extract(day from current_date)
That said, date functions notoriously vary among databases, so the exact syntax depends on your database.
For instance, a common way to write this in SQL Server would be:
where y = year(getdate()) and
m = month(getdate()) and
d = day(getdate())

Oracle query displays data by month and year

I want to display the amount of data by month and year. This is an example of displaying data by date:
select count(*) from db.trx where trxdate = to_date('2018-04-23','yyyy-mm-dd')
When I try to display the amount of data by month and year, no query results appear. Is there something wrong with the query?
The query:
select count(*) from db.trx where trxdate = to_date('2018-04','yyyy-mm')
You need to apply the function to trxdate. Using your logic:
SELECT Count(*)
FROM olap.trxh2hpdam
WHERE To_char(trxdate, 'YYYY-MM') = '2018-04';
However, I strongly recommend that you use direct date comparisons:
WHERE trxdate >= date '2018-04-01'
AND
trxdate < date '2018-05-01'
This will allow the database to use an index on trxdate.
There are a couple of ways of accomplishing what you're trying to do. Which one works for you will depend on your database design (for example, the indexes you've created). One way might be this:
SELECT COUNT(*) FROM olap.trxh2hpdam
WHERE TRUNC(trxdate, 'MONTH') = DATE'2018-04-01';
This will round the date down to the first of the month (and, of course, remove any time portion). Then you simply compare it to the first of the month for which you want the data. However, unless you have an index on TRUNC(trxdate, 'MONTH'), this may not be the best course of action; if trxdate is indexed, you'll want to use:
SELECT COUNT(*) FROM olap.trxh2hpdam
WHERE trxdate >= DATE'2018-04-01'
AND trxdate < DATE'2018-05-01';
There are a number of functions at your disposal in Oracle (e.g. ADD_MONTHS()) in the event that the date you use in your query is supposed to be dynamic rather than static.
Just FYI, there is no reason not to use ANSI date literals when trying to retrieve data by day as well. I'm not sure your original query is a good example of getting data for a particular day, since the Oracle DATE datatype does at least potentially include a time:
SELECT COUNT(*) FROM olap.trxh2hpdam
WHERE trxdate >= DATE'2018-04-23'
AND trxdate < DATE'2018-04-24';
or:
SELECT COUNT(*) FROM olap.trxh2hpdam
WHERE TRUNC(trxdate) = DATE'2018-04-23';
EDIT
In case the month and year are dynamic, I would build a date from them (e.g., TO_DATE('<year>-<month>-01', 'YYYY-MM-DD')) and then use the following query:
SELECT COUNT(*) FROM olap.trxh2hpdam
WHERE trxdate >= TO_DATE('<year>-<month>-01', 'YYYY-MM-DD')
AND trxdate < ADD_MONTHS( TO_DATE('<year>-<month>-01', 'YYYY-MM-DD'), 1 );
Hope this helps.

Select and group multiple range of dates in oracle SQL

I want to Select the same range of dates and group them by year. In this case I want the total of the bills of a customer per year from September and October.
My Code right now:
SELECT c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY'), sum(b.sume)
FROM customer c, bill b
WHERE b.customerId = c.id
AND b.datum BETWEEN '01.10.2016' AND '30.09.2016'
GROUP BY c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY');
This Query works perfectly but when I want to add another year the result is an endless loop:
AND b.datum BETWEEN '01.10.2016' AND '30.09.2016'
OR b.datum BETWEEN '01.10.2015' AND '30.09.2015'
I also tried to do it in a String way. In this case when I only look for the results of September it works perfectly, but again as soon as I add the OR it becomes again an endless loop:
AND TO_CHAR(kp.tmstmp) LIKE '%.09.%'
OR TO_CHAR(kp.tmstmp) LIKE '%.10.%'
I know I am somehow missing something in the OR operator, but since this is only happening me with dates I am a bit confused.
This query works fine and return 0 rows, I guess - exactly the number of dates between 01.10.2016 and 30.09.2016 :)
If you wish to check several ranges, you should enclose them into braces:
... and
(
b.datum between date '2016-10-01' and date '2017-09-30' or
b.datum between date '2015-10-01' and date '2016-09-30' or
...
) and
...
It looks like your date criteria in the WHERE clause are being phrased incorrectly. Try this instead:
SELECT c.name, c.telefonnumber, TO_CHAR(b.billdate, 'YYYY'), sum(b.sume)
FROM customer c, bill b
WHERE b.customerId = c.id AND
(b.datum BETWEEN TO_DATE('01.10.2016', 'DD.MM.YYYY') AND
TO_DATE('30.09.2016', 'DD.MM.YYYY') OR
b.datum BETWEEN TO_DATE('01.10.2015', 'DD.MM.YYYY') AND
TO_DATE('30.09.2015', 'DD.MM.YYYY'));
I made two changes, namely making certain that the date checks occur in one logical place, surrounded by outer parentheses, and that we use bona fide dates when comparing against the datum column.

How to get the third month of a query without using variables?

I have a query in which I need to get the third month of the given reporting date using SQL and then use it as part of the query. I am able to get all the months but I specifically need to get the third month how would I go about doing that? I know this is fairly easy to do in other languages but is it possible in SQL?
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 AS fb
JOIN (
--derrived core set
SELECT service_no, subscription_id
FROM database2
WHERE REPORTING_MONTH = '2015-04-01' <-- this is the reporting month
) AS c
ON fb.SERVICE_NO = c.service_no
AND fb.subscription_id = c.subscription_id
AND fb.REPORTING_MONTH = '2015-07-01' <-- THIS SHOULD BE THE THIRD MONTH
AND fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1
For example if the reporting month is '2015-04-01 I need the variable month to then be '2015-07-01' to be used as part of the query
You don't specify the database you are using. A typical approach would be:
SELECT REPORTING_MONTH, COUNT(*)
FROM database1 fb JOIN
database2 c
ON fb.SERVICE_NO = c.service_no AND
c.REPORTING_MONTH = '2015-04-01' AND
fb.subscription_id = c.subscription_id AND
fb.REPORTING_MONTH = c.reporting_month + interval '3 month' AND
fb.ACTIVE_BASE_IND_NEW = 1
GROUP BY 1
ORDER BY 1;
The exact syntax for + interval '3 month' varies by database.
If the field REPORTING_MONTH is text then you might have to use SUBSTRING (SQL Server) or MID (Others).
If it's a proper date field then perhaps DATEPART(month, fb.REPORTING_MONTH) = 3 will work?
My SQL is a bit rusty but try those functions.

How to find sum of a column between a given date range, where the table has only start date and end date

I have a postgresql table userDistributions like this :
user_id, start_date, end_date, project_id, distribution
I need to write a query in which a given date range and user id the output should be the sum of all distributions for every day for that given user.
So the output should be like this for input : '2-2-2012' - '2-4-2012', some user id :
Date SUM(Distribution)
2-2-2012 12
2-3-2012 15
2-4-2012 34
A user has distribution in many projects, so I need to sum the distributions in all projects for each day and output that sum against that day.
My problem is what I should group by against ? If I had a field as date (instead of start_date and end_date), then I could just write something like
select date, SUM(distributions) from userDistributions group by date;
but in this case I am stumped as what to do. Thanks for the help.
Use generate_series to produce your dates, something like this:
select dt.d::date, sum(u.distributions)
from userdistributions u
join generate_series('2012-02-02'::date, '2012-02-04'::date, '1 day') as dt(d)
on dt.d::date between u.start_date and u.end_date
group by dt.d::date
Your date format is ambiguous so I guess while converting it to ISO 8601.
This is much like #mu's answer.
However, to cover days with no matches you should use LEFT JOIN:
SELECT d.d::date, sum(u.distributions) AS dist_sum
FROM generate_series('2012-02-02'::date, '2012-02-04'::date, '1 day') AS d(d)
LEFT JOIN userdistributions u ON d.d::date BETWEEN u.start_date AND u.end_date
GROUP BY 1