sqlite, selecting number of unique values for each month - sqlite

I am relatively new to sqlite and I am trying to count the number of unique stores each month in my table. My query looks like this:
SELECT COUNT(DISTINCT Store_Num) FROM 'heroes' WHERE strftime('%y-%m', Date)='2005-01';
however it is returning 0 each time. I think it may have to do with my strftime syntax, but I have gone through the documentation and am unable to figure it out.
Thanks!

Uppercase "Y". strftime('%Y-%m', Date)
sqlite> select strftime('%Y-%m', dt) from test;
2014-01

Related

How to get previous day via SQLite in Swift

I have a sqlite table with a number of dates. I would like to execute a query to select those records where the Date is the previous date. I have tried the following but it doesn't work:
SELECT SUM(Amount) AS TOTAL FROM (SpentAmount) Where Date ='\(Date().yesterday)'"
and output is : '2018-07-02 05:43:16 +0000'
But I want only date and this format: '02-07-2018'.
And I m trying This : SELECT SUM(Amount) AS TOTAL FROM SpentAmount WHERE Date = DATE('now', '-1 days');
but this Query give me no result
This is my database Database
but when i execute the query then give me this result
If you want to target yesterday (i.e. the date before today), then use DATE('now', '-1 days'):
SELECT SUM(Amount) AS TOTAL
FROM SpentAmount
WHERE Date = STRFTIME('%d.%m.%Y', DATE('now', '-1 days'));
As a general comment, your current date format is very non ideal. It will be very hard to sort your table by date. Instead, you should always store your date information in ISO format with SQLite, i.e. use 2018-07-02, not 02-07-2018.

SQL in Power Query using ODBC

I have a Table with following Columns:
Account_No, Start_Date, End_Date
I downloaded this table into power query using SQL Select command through ODBC.
Now i want to get sum and count of transactions of all accounts given in the above table from Start_Date to End_Date from another Table. e.g. Transaction_Table. What should i do to get my desired results.
Regards
KAM
You probably don't need Power Query at all at this point.
Assuming your DB server is MS SQL Server 2008 or higher,
WITH t1([Account_No], StartDate, EndDate) As
(
SELECT [Account_No], StartDate = MIN([Start_Date]), EndDate = MAX([End_Date])
FROM Table1
GROUP BY Account_No
)
SELECT
[Account_No]
, Amount = SUM([Field_Transaction_Total])
, [Transaction_Count] = COUNT([Field_Transaction_ID])
FROM [Transaction_Table] t2
INNER JOIN t1 ON t2.[Account_No] = t1.Account_No
AND t2.[Field_Transaction_Date] BETWEEN t1.StartDate AND t1.EndDate
You can also use a copy of a query inside WITH block to get this table with accounts and dates to Excel, if you need it.
If you use another SQL Server, just refactor this code, I hope you've got the idea.
You could use a GROUP BY statement in the SQL you wrote, or you could filter the table based on the Start_Date to the End_Date and then right-click on the column you want to count and choose Group By.
I would start a new Query based on your Transaction_Table. Then I would add a Merge step, joining to your 1st Query on Account_No. Then I would Expand the Start_Date and End_Date from the generated NewColumn.
Next I would Add a Custom Column, and write a formula like this:
= [Transaction_Date] >= [Start_Date] and [Transaction_Date] <= [End_Date]
The resulting column will show TRUE or FALSE. Filter for TRUE.
Finally I would add a Group By step to Sum and Count as required.
I hope I've understood your requirement correctly - it wasn't really clear from your question.

rownum equivlent in teradata

How to convert rownum in following query(oracle) to teradata equivalent:
and not exists(select 1
from CSE, SPD
WHERE cse.id=spd.id
AND ROWNUM = 1
AND CSE.STATUSID IN(6,7,8,13)
thanks.
There's no ROWNUM in Teradata, but you can usually rewrite it using ROW_NUMBER plus QUALIFY.
In your case there's no need for ROWNUM at all (at least logically, maybe Oracle prefers it to do a better plan), this is exactly the same:
and not exists(select *
from CSE, SPD
WHERE cse.id=spd.id
AND CSE.STATUSID IN(6,7,8,13)
Teradata specifically does not have any rownumber attached to the rows in a table as in Oracle.
But it has two analytical functions such as ROW_NUMBER() and RANK() which will give a number to your row and then you can accordingly pick you data.
You may use something like below:
QUALIFY ROW_NUMBER()(Partition by Id order by date)=1
Here, Partition by with a column or few columns can be used to group your data, like suppose some id column in your table and order by will sort the data for that id in your table according to the order by column you provide and =1 means it then chooses the row for that id which is given row number as 1.
Use somethin like below:
row_number() over(partition by '' order by statusid asc) as rownum_1
qualify rownum_1 = 1
The above statement imitates the rownum functionality of oracle.
you can use row_number. keep in mind the columns you want to take into consideration while calculating the row number. You can use qualify for the same.
other options are dense_rank, rank etc. In your case you can use rank in the sub query and put the condition rank = 1. if you want the syntax do let me know.
As you using this tweak for efficiency, I suppose, QUALIFY ROW_NUMBER() and other window functions will not suite you during their heavy use of CPU.
You can simply remove this part, Teradata should be fine.

Getting the nth highest value row in sqlite

My problem sounds simple enough but I havent been able to find a solution that works.
I need to get the row with say 5th highest value of an attribute with sqlite.. Entries are random of course. So its not sorted.
I did find a few solutions with sql but apparently not all sql functionalities are supported by sqlite.
Thanks in advance
To remove duplicates, use DISTINCT.
To get only the fifth value, use the OFFSET clause:
SELECT DISTINCT SomeAttribute
FROM MyTable
ORDER BY 1 DESC
LIMIT 1 OFFSET 4
Try the 'Limit' keyword.
The query below will eliminate the first four rows by using 'not in'.
Then the last 'Limit 1' will select the 5th row.
Select * from Programs
Where ProgramId not in
(Select ProgramId From Programs order by programId limit 4 )
order by programId Limit 1
EDIT:
To add in CV's 'Distinct' and 'OFFSET' suggestions, the finished query would look something like...
Select StudentName From Students Where Marks in (
Select distinct Marks from Students Order By Marks desc Limit 1 offset 4)

Select older date

I have a database limited to n records, if a new record has to be inserted and there's no space I want to delete the oldest one, mind that there could be more than a record with same date: in this case I just remove the first one.
Is it possibile to achieve something like this in sqllite which doesn't have date support?
First of all, to be able to sort your records by date, you have to insert them in the format YYYYMMDD or YYYYMMDDHHmm
Now to get one of oldest ones with same date, you can do this :
SELECT * FROM URTABLE WHERE
LAST_UPDATE_DATE = (SELECT MAX(LAST_UPDATE_DATE) FROM URTABLE) LIMIT 1

Resources