Syntax error on date sort - sorting

I am using Ultralite 12. I have a table with a date field called "Date." (I didn't name it.) I want to sort my results by this field.
The query:
SELECT * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
works and returns the results
HistoryType,ItemID,UM_ID,CustomerID,Date,OrderHeaderID
1,'BC-2000-005',1,'227B05',2014-11-24,'849446-1'
1,'BC-2000-005',1,'227B05',2014-12-17,'852747-1'
1,'BC-2000-005',1,'227B05',2015-01-02,'854701-1'
1,'BC-2000-005',1,'227B05',2015-02-09,'859811-1'
I want to return the top answer when the results are sorted by date (in other words, the last one).
SELECT top 1 * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
order by date DESC
gives me a syntax error by DESC. I've tried this as well:
SELECT top 1 * FROM ItemHistory
where itemid = 'BC-2000-005' and customerid = '227B05'
order by [date] DESC

Try with "date". You could find more info from http://dev.cs.uni-magdeburg.de/db/sybase9/help/dbrfen9/00000010.htm

Related

Is there an alternative to MySQL Field() function in VFP?

I have a multi fields table with a Countries column. I like the results from a query to be ordered by a particular country first and the rest alphabetically. In MySQL I would do something like:
Select * from myTable Order By Field(Countries,'Italy'),Countries
In Visual-FoxPro I have tried indexing the Cursor created by this query:
Select * from myTable Order By Countries
Index on Countries<>'Italy' TAG test
This would display all results for 'Italy' first, but leave the rest in an unpredictable order.
How to achieve this in Visual-FoxPro?
In VFP you can do it with something like this:
SELECT * FROM myTable WHERE Countries='Italy' ;
UNION ALL ;
SELECT * FROM (SELECT * FROM myTable WHERE Countries<>'Italy' ORDER BY Countries) as secsel
It does order by "if countries is not Italy first then Italy", Countries. Right?
In VFP you can use IIF(). ie:
Select *, iif(Countries == 'Italy', 1, 0) as CItaly ;
from myTable :
Order By CItaly,Countries
Note: If you want to do this via an index then you can use a composite index like:
index on iif(Countries = 'Italy', '1', '0') + Countries tag myCountry

I want to select all table field with left join 2nd table in order by first table 1st column

below code
$getQueList = mysql_query("SELECT * FROM questions_master order by questions_master.question_id desc LEFT JOIN users_master ON questions_master.userId = users_master.userId ");
I want to get result as per questions_master.question_id in descending order
Here is the query you have to use for getting result questions_master.question_id in descending order
SELECT *
FROM questions_master
LEFT JOIN users_master
ON questions_master.userId = users_master.userId
ORDER BY questions_master.question_id desc
Order by clause comes at the end.

Issue with selecting max id rows using criteria query / hibernate query?

I am unable to select the rows where TestId is max for respective student, I wrote the code as follows which does not get the required output. my code is as follows,
Criteria c = sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList().add(Projections.property("answer"),"answer"));
c.add(Restrictions.eq("surveyId",send_Survey));
//c.add(Restrictions.eq("testId", "1" ));
//c.setProjection(Projection.max("testId"));
c.addOrder(Order.desc("testId"));
c.add(Restrictions.eq("questionid",FinalQuestionsOne));
List<String> age=c.list();
My table structure is as follows,
I need the following output. select the answer column for max TestId's. How can I get the output using criteria query
So I think what you're trying to get can be achievedd by the following sql:
SELECT TestId, MAX(answer) WHERE questionId = 1 GROUP BY TestId;
You should be able to achieve this with the following Hibernate:
sessionFactory.getCurrentSession().createCriteria(student.class).setProjection(Projections.projectionList()
.add(Projections.property("TestId"), "TestId")
.add(Projections.groupProperty("TestId"))
.add(Projections.max("answer")));

How to get the last element by date of each "type" in LINQ or TSQL

Imagine to have a table defined as
CREATE TABLE [dbo].[Price](
[ID] [int] NOT NULL,
[StartDate] [datetime] NOT NULL,
[Price] [int] NOT NULL
)
where ID is the identifier of an action having a certain Price. This price can be updated if necessary by adding a new line with the same ID, different Price, and a more recent date.
So with a set of a data like
ID StartDate Price
1 01/01/2009 10
1 01/01/2010 20
2 01/01/2009 10
2 01/01/2010 20
How to obtain a set like the following?
1 01/01/2010 20
2 01/01/2010 20
In SQL, there are several ways to say it. Here's one that uses a subquery:
SELECT *
FROM Price p
WHERE NOT EXISTS (
SELECT *
FROM Price
WHERE ID = p.ID
AND StartDate > p.StartDate
)
This translates fairly trivially to LINQ:
var q = from p in ctx.Price
where !(from pp in ctx.Price
where pp.ID == p.ID
&& pp.StartDate > p.StartDate
select pp
).Any()
select p;
Or should I say, I think it does. I'm not in front VS right now, so I can't verify that this is correct, or that LINQ will be able to convert it to SQL.
Minor quibble: Don't use the name ID to store a non-unique value (the type, in this case). It's confusing.
Assuming ID & StartDate will be unique:
SELECT p.ID, p.StartDate, p.Price
FROM Price p
JOIN
(
SELECT ID, MAX(StartDate) AS LatestDate
FROM Price
GROUP BY ID
) p2 ON p.ID = p2.ID AND p.StartDate = p2.LatestDate
Since you tagged your question with LINQ to SQL, here is an LINQ query to express what you want:
from price in db.Prices
group price by price.Id into group
let maxDateInGroup = group.Max(g => g.StartDate)
let maxDatePrice = group.First(g => g.StartDate == maxDateInGroup)
select
{
Id = group.Key,
StartDate = maxDatePrice.StartDate,
Price = maxDatePrice.Price
};

how to get alphabetically next and prev records wiht minimal fetched records?

I have a page that is displaying a company name and its details from a table A.
Now say i have a company displayed,and its name is 'Company_one' now i want to have alphabetically sorted next company and previous company and their details etc.
The data in my table is not sorted.Its stored as it gets the data.
So now what kind of query should i write that it gives only one previous and one next alphabetically sorted record??
Plz help!!
There's no nice way to do that in a single query. Just do two queries.
To get the previous one:
SELECT * FROM companies
WHERE name < variable_with_current_name
ORDER BY name DESC
LIMIT 1
To get the next one along:
SELECT * FROM companies
WHERE name > variable_with_current_name
ORDER BY name ASC
LIMIT 1
You need to use the sort clause to sort your table. The prototype for sort is:
sort by fieldname
Example Query:
select * from your_table sort by company asc
If you want to limit records, use limit clause:
select * from your_table sort by company asc limit 0, 1
Based on Dominic's answer, you can achieve the same result using a single query by combining them with WHERE and OR.
For example:
SELECT * FROM `companies`
WHERE (
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` < 'variable_with_current_name'
ORDER BY `name` DESC
LIMIT 1)
, 0)
OR
`name` = IFNULL(
(SELECT `name` FROM `companies`
WHERE `name` > 'variable_with_current_name'
ORDER BY `name` ASC
LIMIT 1)
, 0)
)
Hope that helps.

Resources