Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a database table looks like this:
Group - Year - Value
-------------------------------
Group A 2018 200
Group A 2019 300
Group A 2020 400
Group B 2019 500
Group B 2020 300
I want to write a SQL query or something like that or a reporting tool to generate a report as below:
Group 2018 2019 2020
----------------------------------
Group A 200 300 400
Group B ---- 500 300
I tried different ways but still not sure how to do that? Anyone can help?
You can use conditional aggregation:
select group,
sum(case when year = 2018 then value end) as value_2018,
sum(case when year = 2019 then value end) as value_2019,
sum(case when year = 2020 then value end) as value_2020
from t
group by group;
Note that group is a really bad name for a column, because it is a SQL keyword. If this is the actual name, I would suggest you change it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I have a table with a date and value for example:
AccID Date Value
1 01/01/2007 10
1 01/02/2008 20
1 01/03/2009 40
I want to create a new table that starts from the date in row 1 and ends with the date in row 2 and so on.....for example
AccID Date DateEnd Value
1 01/01/2007 01/02/2008 10
1 01/02/2008 01/03/2009 20
1 01/03/2009 01/04/2050 40
Select
date,
isnull(lead(date) over (partition by accID order by date), '01/04/2050') as DateEnd,
value
from column A
I have tried this code but I can't seem to get the correct output. This is the output I am currently getting
AccID Date DateEnd Value
1 01/02/2008 01/02/2009 20
1 01/02/2009 01/03/2007 40
1 01/01/2007 01/04/2050 10
You are not getting any output, you are getting an error
Incorrect syntax near 'partition'.
You get the correct results when you correct the error
Select
date,
isnull(lead(date) over (partition by accID order by date), '01/04/2050') as DateEnd,
value
from column A
Edit: Do yourself some favours and apply the following:
Avoid the use of reserved words or if you "must" use them, surround the column or table name with [ ] e.g. [date], [value],[column]
Research the function you're going to use to make the most of what it has to offer, to simplify your queries e.g. See the documentaion for Lead
Use date formats that are unambiguous e.g. '2050-04-01' in preference to '01/04/2050'. The latter could be either 1st April or 4th January depending on where in the world you happen to be
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Imagine we have a table 'user' with several different column. Two of them are date (date) and isValid (boolean). I would like to write sql what sort this by two columns (date and isValid). First of all i would like to sort by date ASC, then every row what has isValid = 1 should be after all row with isValid = 0. So even if have a row with date = 2022.01.01 with isValid = 0 should be before row with date 2021.01.01
Initial Data:
Date IsValid
2023 0
2022 1
2025 0
2024 1
2026 0
Expected Data:
Date IsValid
2023 0
2025 0
2026 0
2022 1
2024 1
With a comma between the columns...
SELECT
date,
isValid
FROM
yourTable
ORDER BY
isValid,
date
based on your edit your ordering of column will be changed isvalid will come first then date so I changed my answer like below
order by isValid, date
You will need to specify desc for isValid column as it seems that you want true value records first, so your query would be like below:
SELECT * FROM `USER` ORDER BY `DATE`, ISVALID DESC;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
There are 2 tables
CUSTOMERS(ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER, ID_CUSTOMER, AMOUNT);
The task is to show all orders (all fields), that have been made before year 2015 and sort the data by ID.
This my code:
SELECT * FROM ORDERS
WHERE YEAR(DATE_ORDER) < 2015
ORDER BY ID
Error when executed:
no such function YEAR
I tried to do that like this:
SELECT * FROM ORDERS
WHERE strfttime('%Y', DATE_ORDER) < 2015
ORDER BY ID
and still doesn't work
I would advise you to write the code using date logic:
where date_order < date('2015-01-01') -> "I tried this one and it worked" - author of the post
Standard SQL uses the function extract():
where extract(year from date_order) < 2015
However, the exact syntax for both depends on the database you are using.
This smells like SQLite. Assuming that your dates be in the format YYYY-MM-DD you could try:
SELECT *
FROM ORDERS
WHERE CAST(SUBSTR(DATE_ORDER, 1, 4) AS integer) < 2015
ORDER BY ID;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to show data created after 01.01.2014 - data created before should not be shown in the output.
I got this so far:
AND TO_CHAR(table1.datum,'IW') = 17 (number of the week - just a start from me)
AND TO_CHAR(table1.datum,'YYYY') = 2014 (shows only data from 2014)
Can you help me?
I would like to show data created after 01.01.2014 - data created before should not be shown in the output.
Then just put the date condition in the filter predicate.
WHERE table1.datum > TO_DATE('01-01-2014','DD-MM-YYYY')
Note that this will not include the rows which were created on '01-01-2014', to include them as well you need greater than or equal to:
WHERE table1.datum >= TO_DATE('01-01-2014','DD-MM-YYYY')
For example,
SQL> WITH table1 AS(
2 SELECT to_date('20-12-2013','DD-MM-YYYY') datum FROM dual UNION ALL
3 SELECT to_date('01-04-2014','DD-MM-YYYY') datum FROM dual UNION ALL
4 SELECT to_date('20-10-2015','DD-MM-YYYY') datum FROM dual
5 )
6 SELECT * FROM table1
7 WHERE table1.datum >= TO_DATE('01-01-2014','DD-MM-YYYY')
8 /
DATUM
---------
01-APR-14
20-OCT-15
SQL>
So, '20-12-2013' is filtered out and you have rows created only created after '01.01.2014'.
Select * from table
Where the_date_field < to_date('01-01-2014','dd-mm-yyyy')
Order by ..... /* If needed */
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm facing a problem with sorting groups of data in a very old (13 years or so) SQL database program. The data looks like this:
Entry Batch Time
1 1 7-1-2013 13:35
2 1 3-3-2014 7:48
3 1 1-2-2014 18:49
4 2 3-1-2011 13:23
5 2 5-3-2014 20:48
6 2 7-2-2014 3:11
7 3 2-3-2012 15:09
8 3 5-3-2014 10:37
9 3 6-2-2014 7:16
I want to sort the entries by grouping those from the same batches together, and then sort them based on the lowest time entry in those groups. In this example, the group order would be 2-3-1 and the entry order would be 4-6-5-9-7-8-1-3-2.
Is there any easy way to do this? I tried working with order by (select blah), but no success so far. Any help would be much appreciated :)
If I am reading correctly, you want to sort on groups by the oldest date in the group first and then by time.
If this were oracle or sql server, you could use analytics for this:
select Entry,
Batch,
Time
from ( select Entry,
Batch,
Time,
MIN(Time) OVER (PARTITION BY Batch) AS MinTime
from MyTable ) MyTable2
order by MinTime, Time
If you do not have a database that supports this, you could try the following:
select MyTable.Entry,
MyTable.Batch,
MyTable.Time
from MyTable
join ( select Batch, MIN(Time) AS MinTime
from MyTable ) MyTable2 on MyTable.Batch = MyTable2.Batch
order by MyTable2.MinTime, MyTable.Time