I'm having a problem with getting the count of the total passenger and baggage on the specific place.
This is my table,enter image description here
but my query shows the total passenger is always 2,
enter image description here
and lastly, this is the query
select
ticket_placeto,
(select count(*) as count from tickets where passenger_type <> 'Baggage' group by ticket_placeto)as Pass ,
ifnull((select count(*) as count from tickets where passenger_type = 'Baggage' group by ticket_placeto),0) as Baggage
from tickets
Rephrase your query to use conditional aggregation:
SELECT
ticket_placeto,
COUNT(CASE WHEN passenger_type <> 'Baggage' THEN 1 END) AS Pass,
COUNT(CASE WHEN passenger_type = 'Baggage' THEN 1 END) AS Baggage
FROM tickets
GROUP BY ticket_placeto;
The idea here is that for each ticket_placeto group you want to take two conditional counts depending on the passenger type.
Related
I have the date stored in a int field and when I select it I use
strftime('%Y-%m-%d', table.date/1000, 'unixepoch') as date
I need to have data for the past 30 days for charting purposes, this means that I need to have in my final query something like:
2017-01-18 ,2
2017-01-17, 0
2017-01-16, 0
2017-01-15, 1
In my table I only have rows for 18th and 15th Jan. So running a query against it, will only return rows for the days that exists,
2017-01-18 ,2
2017-01-15, 1
However, I need to have rows, with 0 value, for the past 30 days that don't have any values, as a final result. In order to have this, I need a way to union my data with a query that returns
now-1 day, 0
now-2 days ,0
now-3 days, 0
....
for the past 30 days. In this way, when I combine the 2 queries, will results in the desired results.
Using from https://stackoverflow.com/a/32987070/379865
I managed to get the data
WITH RECURSIVE dates(date) AS (
VALUES(date('now'))
UNION ALL
SELECT date(date, '-1 day')
FROM dates
limit 30
)
SELECT date, 0 as value FROM dates;
However I don't know how to union it with my other query since WITH does not seem to go as part of a union all
I'm not entirely sure what you are after but I'll have a shot.
Aggregate by strftime('%Y-%m-%d', table.date/1000, 'unixepoch') and then have a count.
e.g.
SELECT COUNT(*), strftime('%Y-%m-%d', table.date/1000, 'unixepoch') as date FROM table GROUP BY strftime('%Y-%m-%d', table.date/1000, 'unixepoch')
Hopefully this is what you are looking for ..
WITH does go with compound queries:
WITH ...
SELECT date, 0 AS value FROM dates
UNION ALL
SELECT ...;
However, to remove the dummy rows for dates that exist, you should use a join:
WITH ...
SELECT ...
FROM dates
LEFT JOIN (SELECT ...your actual query...)
USING (date);
i have two tables:
Acounts:
ID Name
1 cash
2 bank
3 credit card
Transactions
ID accounts_id details income expenses
1 1 abc 1000 0
2 1 xyz 0 500
3 2 avc 200 0
what i want is to get the sum of income and expenses column for all the accounts in account table (even if there is not record in the transaction table for that account_id)
required output:
account_id total_income total_expenses
1 1000 500
2 200 0
3 0 0
what i am trying in sql:
select account_id,coalesce (sum(income),0) as total_income,coalesce(sum(expenses),0) as total_expenses from transactions where account_id in (select id as accounts_id from accounts) group by account_id
what the above query gives:
account_id total_income total_expenses
1 1000 500
2 200 0
account with ID=3 is not included in the result..
i know i am doing something wrong.. or may be completely wrong..
Thanks in advance.
You need to get all the accounts in the account table. To do that, you need a join, specifically an outer join:
select a.account_id, coalesce(sum(t.income),0) as total_income,
coalesce(sum(t.expenses),0) as total_expenses
from accounts a left join
transactions t
on a.account_id = t.account_id
group by a.account_id;
Your attempt to do this in the where clause is counterintuitive. The where clause filters values, so it reduces the number of rows; it cannot increase the number.
WITH TEMP AS
(
SELECT A.ID,T.*
FROM ACCOUNTS A INNER JOIN TRANSACTIONS T
ON A.ID=T.ID
)
SELECT ACCOUNT_ID,SUM(INCOME) AS INCOME,SUM(EXPENSE) AS EXPENSE FROM TEMP
GROUP BY ACCOUNT_ID;
I have two identical tables (month1, month2) and I am trying to find all records from both tables where task1_done = 1. I want the last row in that set (i move cursor to last for this). I have played with inner outer natural joins but can't seem to get month2 values. Here is what I have:
String query = "SELECT m1.columnA, m1.columnB, m1.columnC, m1.columnD, m1.columnE, m1.columnF FROM month1 m1, month2 m2 WHERE m1.task1_done = 1 OR m2.task1_done = 1";
Any help would be great!
I think you want a union all for this query:
select m.*
from (select *
from months1
union all
select *
from months2
) m
where task1_done = 1;
Note: I have used * as a convenience because you said the two tables have the same structure. You should actually list the columns that you want from the two tables.
In general, having two tables with the same layout is a sign of a bad database design. It is usually better to have a bigger table, with another column identifying "month1" or "month2".
EDIT:
SQL tables do not have a "last" value. If you have a an id or timestamp column that you can use for ordering, then you can do:
select m.*
from (select *
from months1
union all
select *
from months2
) m
where task1_done = 1
order by id desc
limit 1;
Are these tables related or have any references? if not you can have separate statement and do a union
i.e.
select top 1 column1, column2.. from month1 WHERE task1_done = 1 order by IdentityColumn Desc
union
select top 1 column1, column2.. from month2 WHERE task1_done = 1 order by IdentityColumn Desc
I have an SQL table which contains flashcard objects. The table has a column indicated whether a flashcard is free or paid. Flashcards are divided into categories. In my android app I need to display the number of flashcards which are free, and also the number of flashcards which are paid, for each category. If a flashcard isn't free, it is paid.
My SQL isn't great, so far I have a query which returns the number of flashcards which are free:
SELECT _id, category_primary, count(category_primary) FROM Flashcards WHERE available = '1' GROUP BY category_primary;
I want to try to get the count of both free and paid flashcards in a single query/cursor as I display the result in a ListView using an adapter.
You can add the available column to the GROUP BY:
SELECT
_id,
category_primary,
available,
count(category_primary)
FROM
Flashcards
GROUP BY
available,
category_primary;
As an aside, I would have expected you to need the _id column in both your original query and this updated version - I have left it out because I'm assuming your original query works fine.
select f.category_primary, count(f1._id)as available_count, count(f0._id)as disable_count
from (select _id, category_primary from Flashcards) f
left join (select _id from Flashcards where available='1') f1 on f1._id=f._id
left join (select _id from Flashcards where available='0') f0 on f0._id=f._id
group by f.category_primary
Steven Fenton's answer came close, but wasn't quite what I wanted. I found my answer here: How to get multiple counts with one SQL query?.
The query that worked for me is:
SELECT
_id,
category_primary,
sum(CASE WHEN Flashcards.available = '1' THEN 1 ELSE 0 END) category_free,
sum(CASE WHEN Flashcards.available = '0' THEN 1 ELSE 0 END) category_paid
FROM
Flashcards
GROUP BY
category_primary;
The naming of the column available is pretty poor on my part, and I didn't phrase my question very clearly. A better name for the column would be is_free Thanks for those who helped answer my question!
I have an SQLite table where I have a list of messages:
to | from
==========
9999 ME
9999 ME
ME 9999
ME 8888
The result of the query should be in the following format:
number(number of records matching)
9999 (3)
8888 (1)
How can I write a query to give me this resultset? I'm working with Android.
Maybe you can do something using this http://www.postgresql.org/docs/current/static/functions-conditional.html
I guess something like this (didn't run it):
SELECT number, count(number) as `Count` FROM (
SELECT
CASE
WHEN to = 'ME' THEN from
ELSE to
END AS number
FROM table
) GROUP BY number;
SELECT value, count(*)
FROM (
SELECT to as value
FROM your_table
UNION ALL
SELECT "from" as value
FROM your_table
) t
GROUP BY value
To filter out unwanted values for to and from use an approriate WHERE clause (ideally in the inner select to reduce the number of rows that need to be processed.
select value, sum (count) as count, from
(
select count(*) as count , "from" as value from your_table
group by "from"
union all
select count(*) as count , "to" as value from your_table
group by "to"
) t
group by t.value
Thanks #a_horse_with_no_name & #razvi!
If you are only looking to count the occurrences of numeric values this should work.
Use concatenation ("||") to form the output that you want.
SELECT to_from || ' (' || count(to_from) || ')' FROM (
SELECT
CASE
WHEN to ~ '^[0-9]+$' THEN to
WHEN from ~ '^[0-9]+$' THEN from
END to_from
FROM
testing
) a
GROUP BY to_from
Results in
?column?
----------
9999 (3)
8888 (1)
(2 rows)