I have table customer with columns country, customer_id and customer_name.
I need to order the mat table in angular by columns country, customer_id and customer_name, like in SQL query.
SELECT * FROM customers ORDER BY country, Customer-Name;
Related
Here is what my table looks like:
Here what I need:
I tried this code
SELECT DISTINCT(product_id),
SUM(quantity) OVER (PARTITION BY product_id) AS sumquantity
FROM ord1;
GROUP BY will do the trick in this case!
SELECT emp, name, product, SUM(quantity)
FROM ord1
GROUP BY emp, name, product
ORDER BY emp, name, product
Replace the column names with the ones from your database table.
SELECT county, category_name, SUM(bottle_qty*(btl_price-state_btl_cost)) AS profit
FROM sales
GROUP BY county, category_name
ORDER BY profit DESC
I want profit for each county and what category_name is produces the most profit in that county.
So I just want the first row, 8th row and 11th row:
You can use distinct on to solve this greatest-n-per-group problem:
SELECT DISTINCT ON(county)
county,
category_name,
SUM(bottle_qty*(btl_price-state_btl_cost)) AS profit
FROM sales
GROUP BY county, category_name
ORDER BY county, profit DESC
However this does not let you control the ordering of the rows in the resultset. In that case, use row_number() instead:
SELECT *
FROM (
SELECT
county,
category_name,
SUM(bottle_qty*(btl_price-state_btl_cost)) AS profit
ROW_NUMBER() OVER(
PARTITION BY county
ORDER BY SUM(bottle_qty*(btl_price-state_btl_cost)) DESC
) rn
FROM sales
GROUP BY county, category_name
) t
WHERE rn = 1
ORDER BY profit
I have 1 table called day_shift, with columns: employee_id, shop_id and date.
I need to create SELECT query to my DB table to get shop_id for each employee_id ordered by the highest amount of date-records. Main idea: Employee able to work in any shop, programm add day-shift by shop_id ordered to date, but Employee will be assigned to the department in which it appears more often.
Actual query that give just first record in table by employee_id:
SELECT TOP 1 shop_id FROM day_shift WHERE employee_id = ?1 ORDER BY date desc
How to get shop_id with most frequent equal record date for user?
[EDIT 1]: Table also contain column id but I dont use it in query.
to get shop_id for each employee_id ordered by the highest amount of date-records.
I think you are using SQL Server. In any case, building on your query syntax, you would use:
SELECT TOP (1) WITH TIES employee_id, shop_id, date, COUNT(*)
FROM day_shift
GROUP BY employee_id, shop_id, date
ORDER BY ROW_NUMBER() OVER (PARTITION BY employee_id ORDER BY COUNT(*) DESC);
If you want this just for one employee:
SELECT TOP (1) employee_id, shop_id, date, COUNT(*)
FROM day_shift
WHERE employee_id = ?
GROUP BY employee_id, shop_id, date
ORDER BY COUNT(*) DESC;
I have a table with names of countries. The country names are duplicated in the table. Eg say there are 8 rows in the table, 5 rows with country Germany and 3 with country UK. I want to get count the countries in the table (eg. I should get the number 2). But I am unable to come up with the query
I tried SELECT Country FROM Customers; but that will give me 8 rows. I tried SELECT DISTINCT Country FROM Customers; but that gives me 2 rows. I tried using count as SELECT DISTINCT Count(Country) FROM Customers; but I get 8 (probably because DISTINCT is applied on result set of SELECT Count(Country) FROM Customers; How could I get 2?
You can use distinct inside count:
select count(distinct country)
from customers;
Which is equivalent to:
select count(*)
from (
select distinct country
from customers
where country is not null
) t;
use inside distinct
SELECT count( distinct Country) FROM Customers
You can use distinct country within count as below:
SELECT count(DISTINCT country)
FROM customers;
You can use distinct country within count and group by country for getting country name as well:
SELECT count(1), country
FROM customers
GROUP BY country;
Here is one way to do this using analytic functions:
SELECT ROW_NUMBER() OVER (ORDER BY COUNT(*)) cnt
FROM customers
GROUP BY country
ORDER BY cnt DESC
LIMIT 1;
I have two tables(and their columns) in my DB:
CUSTOMERS(ID, FIRSTNAME, LASTNAME, ADDRESS);
ORDERS (ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER DATE, ID_CUSTOMER, AMOUNT);
Here is what should I exactly do:
List the first and last names of the customers along with the count
of their orders.
List the first and last names of the customers and calculate the total sum of their orders.
Please make series of SELECTs and sort each one by FIRSTNAME and LASTNAME.
You could join the customers table with an aggregate query on the orders table:
SELECT firstname, lastname, num_orders, sum_orders
FROM customers
JOIN (SELECT id_customer, COUNT(*) AS num_orders, SUM(amount) AS sum_orders
FROM orders
GROUP BY id_customer) OR id_customer = id
ORDER BY 1, 2