Uneven Results Doing A Sort / Orderby on Amazon SimpleDB - amazon-simpledb

I've been getting really uneven results trying to request a numerically sorted list of records from Amazon SimpleDB.
I am zero padding my numbers to get them selected lexigraphically, but still no luck. These two queries are giving the same result, for example:
select * from cbcallers where calls_completed is not null order by calls_completed desc
select * from cbcallers where calls_completed is not null order by calls_completed asc
However, I am getting the correct results using Amazon's query language:
['calls_completed'starts-with ''] sort 'calls_completed' desc
And last week, I was getting different (unordered) results from this query on the same dataset. Anyone have any idea what's up? Is my query jacked?
The dataset looks like this:
Sdb-Item-Name, calls_completed, name, icon
8uda23sd7, 0000002, john smith, /myimgicon.jpg
8uda5asd3, 0000015, john smarts, /myimgicon2.jpg
8udassad8, 0000550, john smoogie, /myimgicon3.jpg

Your query looks completely correct. I loaded your data and used your queries verbatim and got just what you would expect.
Ascending:
select * from cbcallers where calls_completed is not null order by calls_completed asc
[
Item 8uda23sd7
icon: myimgicon.jpg
name: john smith
calls_completed: 0000002,
Item 8uda5asd3
icon: myimgicon2.jpg
name: john smarts
calls_completed: 0000015,
Item 8udassad8
icon: myimgicon3.jpg
name: john smoogie
calls_completed: 0000550]
Descending:
select * from cbcallers where calls_completed is not null order by calls_completed desc
[
Item 8udassad8
icon: myimgicon3.jpg
name: john smoogie
calls_completed: 0000550,
Item 8uda5asd3
icon: myimgicon2.jpg
name: john smarts
calls_completed: 0000015,
Item 8uda23sd7
icon: myimgicon.jpg
name: john smith
calls_completed: 0000002]
Maybe it is an issue with your SimpleDB client, which one are you using, do you know if it is using the latest SimpleDB API version ("2009-04-15")?

Related

Many-to-many SELECT divided into multiple rows for some reason

I have two tables joined via third in a many-to-many relationship. To simplify:
Table A
ID-A (int)
Name (varchar)
Score (numeric)
Table B
ID-B (int)
Name (varchar)
Table AB
ID-AB (int)
A (foreign key ID-A)
B (foreign key ID-B)
What I want is to display the B-Name and a sum of the "Score" values of all the As belonging to the given B. However, the following code:
WITH "Data" AS(
SELECT "B."."Name" As "BName", "A"."Name", "Score"
FROM "AB"
LEFT OUTER JOIN "A" ON "AB"."A" = "A"."ID-A"
LEFT OUTER JOIN "B" ON "AB"."B" = "B"."ID-B")
SELECT "BName", SUM("Score") AS "Total"
FROM "Data"
GROUP BY "Name", "Score"
ORDER BY "Total" DESC
The results display several rows for every "BName" with the "score" divided into semingly random increments between these rows. For example, if the desired result for Johnny is 12 and for April it's 25, the query may shows something like:
Johnny | 7
Johnny | 3
Johnny | 2
April | 19
April | 5
April | 1
etc.
Even after trying to nest the query and doing another SELECT with SUM("Score"), the results are the same. I'm not sure what I'm doing wrong?
Remove Score from the GROUP BY clause:
SELECT BName, SUM(Score) AS Total
FROM Data
GROUP BY BName
ORDER BY Total DESC;
The purpose of your query is to summarize by name, so name alone should appear in the GROUP BY clause. By also including the score, you will get a record in the output for each unique name/score combination.
Okay, I figured out my problem. Indeed, I had to GROUP BY "Name" only, but Firebird I thought wasn't letting me do that. Turns out it was just a typo. Oops.

Why does this Advantage Database query take so long?

First, there are indices on Orders.Order_Number, OrderDet.Order_Number, and OrderDet.PatID. There are other indices, but these are the ones that look pertinent here to me.
This query takes anywhere from 20 to 114 seconds to perform, in the testing that I've done.
Update O
SET O.BenefitID = 1,
O.LastChangedBy = 'RH Test'
FROM Orders O
JOIN OrderDet od ON od.Order_Number = O.Order_Number
WHERE
Od.PatID = 703007
and Od.Status IN ('2', '7', '50', '51', '52', '78', '82');
If I do this instead, I get times of under 60ms:
SELECT ODetailID, Order_Number INTO #OrdNum FROM OrderDet
WHERE PatID = 703007
AND Status IN ('2', '7', '50', '51', '52', '78', '82');
Update Orders
SET BenefitID = 1,
LastChangedBy = 'RH Test'
WHERE Order_Number in (SELECT Order_Number from #OrdNum);
DROP TABLE #OrdNum;
Can someone tell me why my query takes so long when joining the OrderDet table to Orders? It isn't making sense to me that the join takes so long. If I select on either table based on order_number, I get under 200ms response. If I select on OrderDet using PatID, I get response of under 40ms. Selecting on Orders by PatId takes longer - 1-2 seconds, but there's no index on that column. I don't understand why it would be taking up to 114 seconds with the join, since the join is on a column that is indexed in both tables. Any help in understanding this is greatly appreciated.
The reason for this behavior is because when performing an UPDATE operation using join, the table being updated is fixed as the driving table of the nested loop join. Since there is no direct condition on the Orders table, table scan is the only option.
In a SELECT query with inner join of two tables, the tables may be switched to place the table with more restrictive result as the driver. This results in more optimized performance.
An alternative to using the temporary table will be using subquery which is more standard anyway:
Update Orders
SET BenefitID = 1,
LastChangedBy = 'RH Test'
WHERE Order_Number in
(SELECT Order_Number
FROM OrderDet
WHERE PatID = 703007
AND Status IN ('2', '7', '50', '51', '52', '78', '82'));

sql server 2008 r2 pivot

Would it be possible to provide Example:
Basic: With data and columns predefined
More advanced: With dynamic data and column predefined
Expert usage of: With dynamic data and dynamic columns
Thank you
Basic Examples:
USE AdventureWorks2008R2;
GO
SELECT DaysToManufacture, AVG(StandardCost) AS AverageCost
FROM Production.Product
GROUP BY DaysToManufacture;
More Advanced Examples
-- Pivot table with one row and five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]
FROM
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable
PIVOT
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
Expert Examples:
USE AdventureWorks2008R2;
GO
SELECT VendorID, [250] AS Emp1, [251] AS Emp2, [256] AS Emp3, [257] AS Emp4, [260] AS Emp5
FROM
(SELECT PurchaseOrderID, EmployeeID, VendorID
FROM Purchasing.PurchaseOrderHeader) p
PIVOT
(
COUNT (PurchaseOrderID)
FOR EmployeeID IN
( [250], [251], [256], [257], [260] )
) AS pvt
ORDER BY pvt.VendorID;
Hope it helps

Postgres group by day of month, ActiveRecord(Rails) returns array with nil ids while database query works fine [duplicate]

Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
Build query:
SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc
Result:
[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]
It not return count column for me. How can I get it?
Have you tried calling the count method on one of the returned Tag objects? Just because inspect doesn't mention the count doesn't mean that it isn't there. The inspect output:
[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]
will only include things that the Tag class knows about and Tag will only know about the columns in the tags table: you only have id and name in the table so that's all you see.
If you do this:
tags = Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
and then look at the counts:
tags.map(&:count)
You'll see the array of counts that you're expecting.
Update: The original version of this answer mistakenly characterized select and subsequent versions ended up effectively repeating the current version of the other answer from #muistooshort. I'm leaving it in it's current state because it has the information about using raw sql. Thanks to #muistooshort for pointing out my error.
Although your query is in fact working as explained by the other answer, you can always execute raw SQL as an alternative.
There are a variety of select_... methods you can choose from, but I would think you'd want to use select_all. Assuming the build query that you implicitly generated was correct, you can just use that, as in:
ActiveRecord::Base.connection.select_all('
SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags`
INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id`
GROUP BY quote_tags.tag_id
ORDER BY count desc')
See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html for information on the various methods you can choose from.

query , can not select column count

Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
Build query:
SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags` INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id` GROUP BY quote_tags.tag_id ORDER BY count desc
Result:
[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]
It not return count column for me. How can I get it?
Have you tried calling the count method on one of the returned Tag objects? Just because inspect doesn't mention the count doesn't mean that it isn't there. The inspect output:
[#<Tag id: 401, name: "different">, ... , #<Tag id: 4, name: "family">]
will only include things that the Tag class knows about and Tag will only know about the columns in the tags table: you only have id and name in the table so that's all you see.
If you do this:
tags = Tag.joins(:quote_tags).group('quote_tags.tag_id').order('count desc').select('count(tags.id) AS count, tags.id, tags.name')
and then look at the counts:
tags.map(&:count)
You'll see the array of counts that you're expecting.
Update: The original version of this answer mistakenly characterized select and subsequent versions ended up effectively repeating the current version of the other answer from #muistooshort. I'm leaving it in it's current state because it has the information about using raw sql. Thanks to #muistooshort for pointing out my error.
Although your query is in fact working as explained by the other answer, you can always execute raw SQL as an alternative.
There are a variety of select_... methods you can choose from, but I would think you'd want to use select_all. Assuming the build query that you implicitly generated was correct, you can just use that, as in:
ActiveRecord::Base.connection.select_all('
SELECT count(tags.id) AS count, tags.id, tags.name FROM `tags`
INNER JOIN `quote_tags` ON `quote_tags`.`tag_id` = `tags`.`id`
GROUP BY quote_tags.tag_id
ORDER BY count desc')
See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html for information on the various methods you can choose from.

Resources