having dates as column name while executing select statement - sql

I want to display dates as column names while executing select statement, like
select number as sysdate from employee;
But this is not working. How can i do this

SQL queries work on fixed columns, i.e. the result columns (and their names) are known before executing the query.
Of course you can work with column names like "today" and the like:
select
sum(case when mydate = sysdate then value end) as sum_today,
sum(case when mydate = sysdate - 1 then value end) as sum_yesterday
from mytable;
but SQL cannot change the column names.
Anyway, users usually don't work with some geek SQL IDE, but with a program or website written by us. So why bother? Have SQL get you the data and then care about the layout (often with a loop and a grid) in your app.
Simply
select mydate, sum(value) as sum_value
from mytable
group by mydate
order by mydate;
and do the rest in your app.

Related

Oracle order by query using select case

in Oracle Live SQL i was trying to use simple order by sql using select (case when) query
i tried to get to same result select * from tt order by 1
replace 1 with (select (case when 1=1 then 1 else 2 end) from dual)
but two result completely different.
i want table ordered by column 1 however the query using select case when query doesn't sort by column 1.
I don't know why and want to know how this query works in oracle db
Compare
...
order by 2
and
...
order by 1+1
At "compile" time the first 2 is an integer constant so it is a position of the column, the db engine sorts by the specified column. The second 1+1 is an integer expression and the db engine sorts by this value '2'. Same, (select (case when 1=1 then 1 else 2 end) from dual) is an expression, not a column specification.
When you specify a number in the ORDER BY clause, Oracle will sort by that column of the resulting select. As an example, ORDER BY 1,2 will sort by the first column, then the second column. If there is no second column, then you will get an error.
In the ORDER BY of the outermost query, there is essentially no sorting happening in your query because 1 is always returned from your subquery. This is sorting by the value 1 and not the first column.
If you explain the logic you are hoping to achieve, then we may be able to assist, but that is what is happening with your existing queries.

Netezza SQL statement to return count or null based on day of week

I'm a beginner with SQL. I'm trying to determine if there's a way to write a SQL statement that will return a null value for certain days of the week, and a count on other days of the week. I can't use a script (the interface I'm using only allows me to execute a single statement).
The logic is something like this:
if max(as_of_date) is a Saturday or Sunday, then return null
Else select count(*) from table where (etc).
I assume that AS_OF_DATE is a column in your source table, and that your output should only be one row, and if even ONE row in the source table holds a record with the relevant date, then it will return non-null. Please elaborate on question (desired input/output would be nice)
Select
case when cnt>0
then cnt
end
from
( select count(*) cnt
from THE_TABLE
where EXTRACT(dow FROM AS_OF_DATE) not in (1,7)
) x

SQL - Count Duplicate Values Within Date Range

I have a simple, but large, database that I need to write a SQL statement for. The statements needs to do the following:
Get the 15 most popular values for a field.
From those 15, get the count that value has appeared within a particular time period.
My table contains both a Date and a Value field. I am able to extract the 15 most popular values, or get the count for a particular value in a given time period. I do not know how to put the two together.
This is my current SQL:
SELECT
Count( Value ) AS Total,
Value AS Value
FROM
Database
GROUP BY
Value
ORDER BY
Total DESC
LIMIT 15
That will get my most popular 15. But from that, I want to display the COUNT() that each Value is between two dates.
Would this require a HAVING clause?
I simplified the previous solution (which would also do a job) a little bit:
SELECT
Value,
Count(*) as TotalInPeriod
FROM Database
WHERE Value in (SELECT Value FROM Database GROUP BY Value
ORDER BY count(*) DESC LIMIT 15)
AND date_field BETWEEN your_start_date and your_end_date
GROUP BY Value
Try something like this. Make an inner query that finds the top 15 values overall, and join it to the main set to limit it to those values.
SELECT
Count( Value) as TotalInPeriod,
Value as Value
FROM
Database a
JOIN (SELECT
Count( Value ) AS Total,
Value AS Value
FROM
Database
GROUP BY
Value
ORDER BY
Total DESC
LIMIT 15) as topValues
ON
a.Value = topValues.Value
WHERE
a.date_field BETWEEN your_start_date and your_end_date
GROUP BY
a.Value

PostgreSQL limiting results by year

i have a working PostgreSQL query, column "code" is common in both tables and table test.a has date column and i want to limit search results on year, date format is like ( 2010-08-25 )
SELECT *
FROM test.a
WHERE form IN ('xyz')
AND code IN (
SELECT code
FROM test.city)
any help is appreciated
To return rows with date_col values in the year 2010:
SELECT *
FROM test.a
WHERE form = 'xyz'
AND EXISTS (
SELECT 1
FROM test.city
WHERE code = a.code
)
AND date_col >= '2010-01-01'
AND date_col < '2011-01-01';
This way, the query can use an index on date_col (or, ideally on (form, date_col) or (form, code, date_col) for this particular query). And the filter works correctly for data type date and timestamp alike (you did not disclose data types, the "date format" is irrelevant).
If performance is of any concern, do not use an expression like EXTRACT(YEAR FROM dateColumn) = 2010. While that seems clean and simple to the human eye it kills performance in a relational DB. The left-hand expression has to be evaluated for every row of the table before the filter can be tested. What's more, simple indexes cannot be used. (Only an expression index on (EXTRACT(YEAR FROM dateColumn)) would qualify.) Not important for small tables, crucial for big tables.
EXISTS can be faster than IN, except for simple cases where the query plan ends up being the same. The opposite NOT IN can be a trap if NULL values are involved, though:
Select rows which are not present in other table
If by "limit" you mean "filter", then I can give you an option
SELECT
*
FROM
test_a
WHERE
form IN ('xyz')
AND code IN (
SELECT code
FROM test_city
)
AND EXTRACT(YEAR FROM dateColumn) = 2010;
db-fiddle for you to run and play with it: https://www.db-fiddle.com/f/5ELU6xinJrXiQJ6u6VH5/6

Condition inside a count -SQL

I am trying to write a condition inside a count statement where it should only count the entries which do not have an ENDDATE. i am looking for writing the condition inside the count as this is a very small part of a large SQl Query
sample query,
select product, count(*) as quantity
from table
where end_date is null
group by age
This query lists quantity for each product which do not have an end date
One method uses conditional aggregation:
select sum(case when end_date is null then 1 else 0 end) as NumNull
. . .
Another method is just to subtract two counts:
select ( count(*) - count(end_date) ) as NumNull
count(end_date) counts the number that are not NULL, so subtracting this from the full count gets the number that are NULL.
Uhmmmm.
It sounds like you are looking for conditional aggregation.
So, if you have a current statement that's sort of working (and we're just guessing because we don't see anything you have attempted so far...)
SELECT COUNT(1)
FROM mytable t
And you want another another expression that returns a count of rows that meet some set of conditions...
and when you say "do not have an ENDDATE", you are refderring to rows that have an ENDDATE value of NULL (and again, we're just guessing that the table has a column named ENDDATE. Every row will have an ENDDATE column.)
We'll use a ANSI standards compliant CASE expression, because this would work in most databases (SQL Server, Oracle, MySQL, Postgres... and we don't have clue what database you are using.
SELECT COUNT(1)
, COUNT(CASE WHEN t.ENDDATE IS NULL THEN 1 ELSE NULL END) AS cnt_null_enddate
FROM mytable t