Using UNION with ORDER BY does not produce expected results in Sybase - sorting

I have a list of Currencies E.g, USD, AED, INR, EUR, GBP (in order of entry in table Currencies). I need to prepare a query with all currencies including 'ALL' as well but sorted with 'ALL' appearing as the first one.
Expected result should be in the following order:
ALL
AED
EUR
GBP
INR
USD
My SQL is:
SELECT 'ALL' Currency
UNION ALL
SELECT Currency FROM Currencies ORDER BY Currency
However, the above SQL produces with AED as the first one due to sorting.
AED
ALL
EUR
GBP
INR
USD
Help appreciated to get 'ALL' on top.

You can add a "Rank" column which is used in the ordering, and use a derived table that you order and select the Currency column from:
SELECT Currency FROM (
SELECT 'ALL' Currency, 1 Rank
UNION ALL
SELECT Currency, 2 Rank FROM Currencies
) t
ORDER BY Rank, Currency

Related

MS Access 2013: Top 10 Items sold in each country

I need write a query to get top 10 Items sold in each country
Database Type is MS Access 2013
Table Name: SoldItems
Fields
Country
Item
Query
This query will get me all items sold in each country ordered descending by count of sold items in each country, i only need top 10 items sold in each country
SELECT count(*) As CountOfItemsSold, Country, Item
from SoldItems
group by Country, Item
order by 2,1 desc
You will need to use a sub query that returns the Top 10 items grouped by country and use that as the basis for a filter. Something like:
SELECT S.Country, S.Item, COUNT(S.Item) AS CountOfItem
FROM SoldItems AS S
GROUP BY S.Country, S.Item
HAVING S.Item In (SELECT TOP 10 R.Item
FROM SoldItems AS R
GROUP BY R.Country, R.Item
HAVING R.Country=S.Country
ORDER BY COUNT(R.Item) DESC
)
ORDER BY S.Country, COUNT(S.Item) DESC;
Note that if there are items with an equal number of items sold, then you will get more than 10 records returned per country.
Regards,

Select rows having same Rate column value

In the following table I want Supp_ID that have same rate for same combination of Product and RouteCode
So, I should get all the rows except the first row.
Supp_ID Product RouteCode Rate
25sdf-1 PROD1 2-00 0.0436
302qq-6 PROD1 1-00 0.0815
30wqa-6 PROD1 1-00 0.0815
20xdf-1 PROD1 3-00 0.0936
28xdf-1 PROD1 3-00 0.0936
You could use a Window function to help out here:
SELECT *
FROM (SELECT supp_id, product, routecode, rate,
count(*) OVER (PARTITION BY rate, Supp_Id, Product) as count_of_shared_rate
FROM table) t1
WHERE count_of_shared_rate > 1

How can i solve this query in sql oracle?

It's an exercise that is not solved in the book in which I am studying.
The goal is to find the seller who has had the highest number of sales per month,
during all the months for which there is registered information. The problem is that I do not know how to divide tuples into periods of one month.
First table is:
Table Sellers
Id_seller
Name_Product
And the other one is:
Table Product
Name_Product
View_datetime
Budget
What did i do?
I made this query:
SELECT id_seller FROM(SELECT id_seller, COUNT(id_seller)
FROM SELLERS INNER JOIN PRODUCT
ON SELLERS.name_product = PRODUCT.name_product
GROUP BY id_seller HAVING COUNT(id_seller)>= 1
ORDER BY 2 DESC)
WHERE ROWNUM = 1;
The query returns me the seller that most sales has done, but not "per month since there are records" as the statement asks. Any ideas? I'm so lost...
The idea is to compare the total sales of each salesman in this month (sysdate), with those of a month ago, two months ago ... so long as there are older records. And get the maximum from each seller. And then you print the seller with more sales from the previous list. If a seller sells 400 products this month(April, the sysdate), but another seller sold in October last year 500, the result would be the second seller . That's what I do not know how to do.
Thanks ^^
You could try this query
select MonthName, id_seller, max(TotalSales) from (
select to_char(sysdate, 'Month') MonthName, sellers.id_seller, count(sellers.id_seller) TotalSales
from sellers inner join product
on sellers.name_product = product.name_product
group by to_char(view_datetime, 'Month'), sellers.id_seller
) tab
group by MonthName, id_seller
There are a few points to make...
The tables are weird. I assume your table sellers would better be called sales, right?
In this example, having count... >= 1 is a no-op. Count could only be 0 if there were no rows at all, in which case there would be no row in the group- by output. You can just leave this count away, here.
To get the sales per month, just add the month to the group by. I.e. group by id_seller, To_date(view_datetime,'YYYYMM').

What if I want Oracle SQL to GROUP BY this but not that?

This is what I get previously from another SQL code:
Customer Id week_ending Purchase Id Price
1234 2/28/2015 8604220 15
1234 2/28/2015 8604220 13.75
1234 2/28/2015 8604220 12.95
1234 2/28/2015 8604220 18.95
567890 8/15/2015 6376243 5.15
567890 8/15/2015 6376243 0.89
567890 8/15/2015 6376243 3.99
567890 8/15/2015 6376243 2.3
1234 1/24/2015 8824241 0.99
1234 1/24/2015 8824241 3.99
1234 1/24/2015 8824241 3.89
Now I want to sum the price by Purchase ID since it is unique for every of our customer's order but I don't want my SQL to think and sum it by Customer ID (since each customer could order multiple times with multiple Purchase ID). Following is my code that I wrote but I'm afraid that it would sum them by customer_id. How do I avoid this mistake of double accounting? Thanks in advance!
WITH example AS(SELECT
customer_id
,MAX(nvl(promised_arrival_day, ship_day)) OVER (PARTITION BY purchase_id) AS ship2_day
,purchase_id
,SUM(price) AS order_size
FROM
my_table
GROUP BY
customer_id
,MAX(nvl(promised_arrival_day, ship_day)) OVER (PARTITION BY customer_purchase_id)
,purchase_id)
SELECT
example.customer_id
,TO_CHAR(example.ship2_day + (7-TO_CHAR(example.ship2_day,'d')),'MM-DD-YYYY') AS week_ending
,example.purchase_id
,example.order_size
FROM
example;
Just
SELECT customer_id, purchase_id, sum(price)
FROM your_table
GROUP BY customer_id, purchase_id
Each record will be counted only once. It doesn't do any "double accounting" as you say.
You will get one record for each unique combination of customer_id/puchase_id in your data.
Looking at your data, I would do something like.
with tbl as(
-- query by which you are getting dataset in example
)
select customer_id,purchase_id, sum(price) as total_price from tbl
group by purchase_id,customer_id

Get Shipping State, Zip, and Tax paid on Magento Orders in Date Range

I need to export a list of all orders between dates X & Y that shows the following:
Order ID
State Shipping
Zip Shipped
Sales Tax Collected
Is there an easy query I can run to pull this information from the orders table?
The current X is January 1, 2015; the current Y is March 31, 2015.
I really only need orders shipped TO California (the only state we charge tax), but can filter this out through sorting the exported CSV list later.
Thank you!
You need two tables to get your data, here is the SQL :
SELECT a.increment_id AS 'Order ID', b.region AS 'State Shipping', b.postcode AS 'Zip Shipped', a.base_tax_amount AS 'Sales Tax Collected'
FROM sales_flat_order a
JOIN sales_flat_order_address b
ON a.entity_id = b.parent_id
WHERE a.created_at >= '2015-01-01 00:00:00' AND a.created_at <= '2015-03-31 23:59:59'
GROUP BY a.entity_id
few things need be care:
tax in sales_flat_order table has many fields, I am not sure this is what you looking for
the create_at value you might want to change. In my case, my Magento order created time value is faster 11 hours than my computer time, maybe the timezone issue.
the 'GROUP BY' is for get rid of duplicate rows after select the data from two tables.
Below query will help you:-
You can implement where clause as per your requirement.
SELECT increment_id AS `Order Id` , address.region AS `state` , address.postcode AS `zipcode` , order.base_subtotal_incl_tax AS `tax` FROM sales_flat_order `order` JOIN sales_flat_order_address `address` ON order.entity_id = address.parent_id

Resources