SQLite Query _ Select only different Varchar and add them to one - sql

this might sound kind of strange, but what I want to do is this:
SQLite table # SQL Fiddle
I want as result:
peter
when I query ID<=5
and
alvinpeter
if ID is <=10 (names added to one in alphabetical order)
Anyone know how to do this with SQLite only?
Thanks in advance, best regards!

Following will do:
SELECT GROUP_CONCAT(name,'')
FROM (
SELECT DISTINCT name
FROM testtable
WHERE ID<=10
ORDER BY name
);
(in SQL Fiddle)

Related

how to combine multiple rows and record based on the result in a particular column?

I am attempting to combine a rows based on the customer id and a T/F column. I am new to coding and SQL and I've tried 'GROUP BY' in my query that has 50 plus columns but i am unable to do so.
Data Set:
Desired Result
Thank you in advance.
There are probably more elegant solutions, but try the following...
select recordID, city, max(ispaid)
from dat
group by recordID, city
Here's a link to SQL Fiddle where you can play with it.
You can use distinct
select
distinct recordId,
city,
isPaid
from yourTable

MS Access SQL how to display the unique result

I am new with the MS access SQL and the following is what I would like to do
in my database table
I have the following data
basically no duplicate, I tried "Distinct" but given my result is not one column only so I can't do that. I research online and look like it might need to use inner join but I only have one table only.
Appreciate if anyone can help me up..
Thanks
Regards,
Marc
You should be able to run the following query:
SELECT DISTINCT name, birthday
FROM [table];
select DISTINCT column1, column2, ...
from TABLENAME;
Replace columns and table name with respective values.
like this
select DISTINCT name, birthday from TABLENAME;

SQL Query to select one field order by other field

I want to select a field in select statement and order by with another field but sql server doesn't allows this as it says order by item must appear in the select statement if select distinct is specified.
This is what I tried :
select DISTINCT format_type
from Labels_Add_Label
where external_group_id= 2826
order by group_sequence
What changes are required to do in this query?
Please provide the changed query
You can rewrite your query this way (equivalent to distinct):
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
and you can't use ORDER BY group_sequence here. There may be more than one row with same format_type but different group_sequence. SQL server doesn't know which one should be used for the ordering.
You can however use aggregate functions with a GROUP BY query:
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
ORDER BY MIN(group_sequence) ; -- or MAX(group_sequence)
I just took this question to test my knowledge and have come with the following solution (using CTE in MS SQL Server), please correct me if I'm wrong - Using the NORTHWIND Database (Employees Table) on MS SQL Server, I have written this query - This could be one other option that could be used, if there be a need to!
WITH CTE_Employees(FirstName, LastName, BirthDate)
AS
(
SELECT FirstName, LastName, BirthDate
FROM Employees
WHERE Region IS NOT NULL
)
SELECT FirstName FROM CTE_Employees ORDER BY BirthDate DESC
As mentioned above, there can be a same Employee FirstName but with a different LastName, hence SQL Server imposes a condition where we can't use DISTINCT in conjunction with ORDER BY...
Hope this helps!

counting rows in select clause with DB2

I would like to query a DB2 table and get all the results of a query in addition to all of the rows returned by the select statement in a separate column.
E.g., if the table contains columns 'id' and 'user_id', assuming 100 rows, the result of the query would appear in this format: (id) | (user_id) | 100.
I do not wish to use a 'group by' clause in the query. (Just in case you are confused about what i am asking) Also, I could not find an example here: http://mysite.verizon.net/Graeme_Birchall/cookbook/DB2V97CK.PDF.
Also, if there is a more efficient way of getting both these results (values + count), I would welcome any ideas. My environment uses zend framework 1.x, which does not have an ODBC adapter for DB2. (See issue http://framework.zend.com/issues/browse/ZF-905.)
If I understand what you are asking for, then the answer should be
select t.*, g.tally
from mytable t,
(select count(*) as tally
from mytable
) as g;
If this is not what you want, then please give an actual example of desired output, supposing there are 3 to 5 records, so that we can see exactly what you want.
You would use window/analytic functions for this:
select t.*, count(*) over() as NumRows
from table t;
This will work for whatever kind of query you have.

SQL alias select

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.
There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s