Grouping and summarizing values - sql-server-2000

Find out makers who produce only the models of the same type, and the number of those models exceeds 1.
maker model type
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2112 PC
E 2113 PC
The result should be
Model Type
D Printer
I tried
Select maker,type from Product
Group by maker ,type
Having count(*)>1
The above query gives me the maker whose number of model exceeds one .But im not able to find the model which only produces same type .Please help

Select Distinct Maker, Type From Product
Where Maker In (Select Maker From Product
Group By Maker
Having Count(Distinct Type) = 1
And Count(Distinct Model) > 1)

Related

Visualize all products of the order with the highest value, to extract the largest amount and orders from it

Desired result
NAME PRICE
Lenovo X1 Carbon 4009.00
Lenovo ThinkVision X1 1549.00
Lenovo YOGA 520 1349.00
Motorola Moto Z2 999.00
Motorola Moto Z 549.00
Motorola Moto E5 179.00
ID CATEGORY NAME PRICE ORDER_ID
1 PC Lenovo ThinkaPad L380 1579 5
2 Mobile Motorola Moto E5 179 1
3 Mobile Motorola Moto Z 549 2
4 Monitor Lenovo ThinkVision X1 1549 4
5 PC Lenovo X1 Carbon 4009 3
6 Mobile Motorola Moto Z2 999 4
7 PC Lenovo Legion Y530 2099 4
8 PC Lenovo YOGA 520 1349 3
9 Monitor Lenovo ThinkVision X1 1549 6
10 PC Lenovo YOGA 520 1349 6
11 Monitor Lenovo ThinkVision X1 1549 3
12 Mobile Motorola Moto Z2 999 3
13 Mobile Motorola Moto E5 179 3
14 Mobile Motorola Moto Z 549 3
I used these 2 queries to extract information from the table in the photo
but i need to find a way to retrieve the information with just one query please help
SELECT TOP 1 NAME , PRICE, ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID like '%3%'
ORDER BY PRICE DESC
Desired result
SELECT NAME ,PRICE
FROM PRODUCTS
WHERE ORDER_ID =
(
SELECT TOP 1 ORDER_ID
FROM PRODUCTS
ORDER BY PRICE DESC
)
-- Get everything from the table
SELECT
*
FROM
Products
-- Where the total order value for the order is the highest
Where
order_id =
(
-- The largest order total
SELECT TOP 1
order_id
FROM
Products
GROUP BY
order_id
ORDER BY
SUM(price) DESC
)
ORDER BY
Price DESC

How to Properly Write SELECT Statements When Dealing with Multiple Tables

To demonstrate what I am asking, I will present two tables: The Product table and the PC table. The product table contains the following information:
MAKER MODEL TYPE
----- ---------- -------
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2111 PC
E 2112 PC
The PC table contains the following information:
CODE MODEL SPEED RAM HD CD PRICE
---------- ---------- ---------- ---------- ---------- ---------- ----------
1 1232 500 64 5 12x 600
2 1121 750 128 14 40x 850
3 1233 500 64 5 12x 600
4 1121 600 128 14 40x 850
5 1121 600 128 8 40x 850
6 1233 750 128 20 50x 950
7 1232 500 32 10 12x 400
8 1232 450 64 8 24x 350
9 1232 450 32 10 24x 350
10 1260 500 32 10 12x 350
11 1233 900 128 40 40x 980
12 1233 800 128 20 50x 970
In order to execute a query that returns all PC models, which of the following queries would be better:
SELECT model FROM pc;
OR
SELECT model FROM product JOIN pc USING(model);
Since you only need PC models, the first query you stated will be OK to use.
In your second query, you are getting the product table content first then joining them into PC table, which is slower than your first query and since you do not need any column from Product table, it is not necessary.
You can go with the query below as an alternative, to return only PC related rows, if you do not add the where clause and there is a PC model as 1276. your second query would return the A - 1276 - Printer row.
SELECT DISTINCT model FROM product JOIN pc USING(model) where product.type='PC';

TSQL Interleaved sequence without join operations or user defined objects

Is that possible to create an interleaved sequence with first and last values going in turns untill data set is empty without using joins and user defined functions or procedures?
code type model price
1 Hats 1298 700,00
1 Shoes 1232 600,00
1 Pants 1276 400,00
2 Hats 1321 970,00
2 Shoes 1121 850,00
2 Pants 1433 270,00
3 Hats 1750 1200,00
3 Shoes 1233 600,00
3 Pants 1434 290,00
4 Hats 1298 1050,00
4 Shoes 1121 850,00
4 Pants 1401 150,00
5 Hats 1752 1150,00
5 Shoes 1121 850,00
5 Pants 1408 270,00
6 Hats 1298 950,00
6 Shoes 1233 950,00
6 Pants 1288 400,00
7 Shoes 1232 400,00
8 Shoes 1232 350,00
9 Shoes 1232 350,00
10 Shoes 1260 350,00
11 Shoes 1233 980,00
12 Shoes 1233 970,00
I have added extra spaces between rows to get the interleaved sequence idea.
You want to get the odd values going from lowest coded items (asc) and even values with the highest coded items (desc). You also want to order type by Hats,Shoes and Pants.
code type model price
1 Hats 1298 700,00
1 Shoes 1232 600,00
1 Pants 1276 400,00
6 Hats 1298 950,00
12 Shoes 1233 970,00
6 Pants 1288 400,00
2 Hats 1321 970,00
2 Shoes 1121 850,00
2 Pants 1433 270,00
5 Hats 1752 1150,00
11 Shoes 1233 980,00
5 Pants 1408 270,00
3 Hats 1750 1200,00
3 Shoes 1233 600,00
3 Pants 1434 290,00
4 Hats 1298 1050,00
10 Shoes 1260 350,00
4 Pants 1401 150,00
4 Shoes 1121 850,00
9 Shoes 1232 350,00
5 Shoes 1121 850,00
8 Shoes 1232 350,00
6 Shoes 1233 950,00
7 Shoes 1232 400,00
Right now I came up with solution that includes joins but I am looking for something that would work without using it.
My solution with using joins:
with cteasc as
(
select
ROW_NUMBER() over(order by code,charindex(type, 'HatsShoesPants'))id
,(ROW_NUMBER() over(partition by type order by code,charindex(type, 'HatsShoesPants')) + 1) / 2 offsetasc
,code,model,price,type
from mydata
),
ctedsc as
(
select
ROW_NUMBER() over (partition by type order by code desc)id
,code,model,price,type
from cteasc
)
select t1.id
,case
when t1.code%2=1
then LAG(t1.type,t1.code-t1.offsetasc,t1.type)over(partition by t1.type order by t1.id)
else LAG(t2.type,t1.code-t1.offsetasc,t1.type)over(partition by t1.type order by t1.id)
end type
,case
when t1.code%2=1
then LAG(t1.model,t1.code-t1.offsetasc,t1.model)over(partition by t1.type order by t1.id)
else LAG(t2.model,t1.code-t1.offsetasc,t1.model)over(partition by t1.type order by t1.id)
end model
,case
when t1.code%2=1
then LAG(t1.price,t1.code-t1.offsetasc,t1.price)over(partition by t1.type order by t1.id)
else LAG(t2.price,t1.code-t1.offsetasc,t1.price)over(partition by t1.type order by t1.id)
end price
from
cteasc t1
join ctedsc t2
on t1.code = t2.id
and t1.type = t2.type
The idea would be to generate two sequences:
The ascending items with row numbers 1, 3, 5, ...
The descending items with row numbers 2, 4, 6, ...
Then, we simply need to UNION ALL the sequences and sort them.
with ascItems as (
select *, row_number() over (order by someCol ASC) r * 2 - 1 as r from T
)
, descItems as (
select *, row_number() over (order by someCol DESC) r * 2 as r from T
)
select * from ascItems
union all
select * from descItems
order by r
This should require the table T to be available sorted twice (either by index or by 2xsorting). The UNION ALL should manifest itself as a cheap merge concat.

how to make Numbering rows in a query

I have a simple select statement:
Get numbering of rows from Product table in the following order: a name of the manufacturer in
--decreasing order of quantity of models produced by it (when there is identical quantity of models
--for a number of manufacturers, their names should follow in increasing alphabetic order), model (increasing order).
--Output: number in accordance with the above order, a name of the manufacturer (maker), model
RESULT SHOULD BE -
no maker model
1 A 1232
10 E 2112
11 E 2113
12 B 1121
13 B 1750
14 D 1288
15 D 1433
16 C 1321
2 A 1233
3 A 1276
4 A 1298
5 A 1401
6 A 1408
7 A 1752
8 E 1260
9 E 1434
MY QUERY :
with result as
(select *, count(*) over (partition by maker) as ModelsCount from Product)
select row_number() over (order by ModelsCount desc,model) AS No, Maker, Model
from result
MY RESULT:
no maker model
1 A 1232
10 E 2112
11 E 2113
12 B 1121
13 D 1288
14 D 1433
15 B 1750
16 C 1321
2 A 1233
3 A 1276
4 A 1298
5 A 1401
6 A 1408
7 A 1752
8 E 1260
9 E 1434

Point out the maker and speed of the laptops having hard drive capacity more or equal to 10 Gb

I tried this in two ways (I think they are the same):
select product.maker,speed from product,laptop
where product.model=laptop.model and hd>='10'
and
select product.maker, speed from laptop
join product
on (product.model=laptop.model)
where hd>='10'
and I receive this in the simulator:
"Your query produced correct result set on main database, but it failed test on second, checking database. * Wrong number of records (more by 1)
This exercise has FAQ"
The relevant tables are:
Table Laptop
code model speed ram hd price screen
------------------------------------------------------
1 1298 350 32 4.0 700.0000 11
2 1321 500 64 8.0 970.0000 12
3 1750 750 128 12.0 1200.0000 14
4 1298 600 64 10.0 1050.0000 15
5 1752 750 128 10.0 1150.0000 14
6 1298 450 64 10.0 950.0000 12
Table Product:
maker model Type
-----------------------
A 1232 PC
A 1233 PC
A 1276 Printer
A 1298 Laptop
A 1401 Printer
A 1408 Printer
A 1752 Laptop
B 1121 PC
B 1750 Laptop
C 1321 Laptop
D 1288 Printer
D 1433 Printer
E 1260 PC
E 1434 Printer
E 2112 PC
E 2113 PC
When this answer was written, one query used hd>=10 and one used hd>='10'. The query has since been edited.
When you use the '10' in the comparison, you cause the DBMS to do the comparison as a string instead of as a number. Under string comparison, 9 is greater than 10.
As a general rule of thumb, if the data column is a string type, you should compare it with a string: if the column is numeric type, you should compare it with plain numbers (not with strings). Note that different DBMS may have different ways of interpreting a mixed-type expression.
select distinct product.maker, speed from laptop
join product
on (product.model=laptop.model)
where hd>='10'
or
select distinct product.maker,speed from product,laptop
where product.model=laptop.model and hd>='10'
Select P.maker, L.speed
from Laptop L, Product P
where P.model =L.model
And L.hd > = 10
Group by P.maker, L.speed
Try to use group by statement to get the correct solution.
Select distinct Product.maker,Laptop.speed from Product,Laptop where Product.model=Laptop.model and Laptop.hd>=10 and type='laptop'
select DISTINCT maker, speed
from Product
join Laptop on (Product.model=Laptop.model)
where hd >= 10
order by speed asc
You forgot to filter for the laptops:
SELECT Product.maker, Laptop.speed
FROM Product INNER JOIN Laptop ON Laptop.model = Product.model
WHERE Laptop.hd >= 10 AND Product.type = 'Laptop'
Use keyword Distinct before product.maker.