Change the order of a table according to preferences given in another table - sql

In SQL Server, I have a table A as below
Name Food PreferenceOrder
------------------------------
Jon Burger 1
Jon Coke 2
Jon Pizza 3
Jon Fries 4
Sam Pizza 1
Sam Coke 2
I have another table B that can override the preference order above
Name Food PreferredOverFood
--------------------------------
Jon Pizza Fries
Jon Coke Burger
Jon Fries Coke
Sam Coke Pizza
Basically here, Food should come before PreferredOverFood (Pizza > Fries)
Now, I want to reorder table A according to Table B preferences, so the result should be like
Name Food PreferenceOrder
------------------------------
Jon Burger 4
Jon Pizza 1
Jon Fries 3
Jon Coke 2
Sam Pizza 2
Sam Coke 1
I tried by using cursors, so I created a dynamic cursor, with each fetch I am updating Table B with table A preference, but since we are updating things row by row its not considering rows that violate the previous preferences, so I am getting Fries before Pizza (since Fries > Coke is run and it forgot about first preference (Pizza > Fries)).
So dynamic cursor is not working, (its not refreshing the result set after update). Can I use CTE or something to do like above. (Can also have circular dependencies, but not too worried about it for now)
Thanks

Related

How to continue a sequence when inserting

I have tried to simplify my question with the following example:
I have a table with the following data:
Marker Name Location
1 Eric Benson Mixed
2 John Smith Rural
3 A David Rural
4 B John Mixed
And i want to insert into the table:
Name Location
Andy Jones Mixed
Ian Davies Rural
How can i continue the sequencein the Marker column to end up with:
Marker Name Location
1 Eric Benson Mixed
2 John Smith Rural
3 A David Rural
4 B John Mixed
5 Andy Jones Mixed
6 Ian Davies Rural
If you make this with a Stored Procedure you can ask the max of the Marker before to insert.
(That only works if the Marker Column is not identity)
Like This:
declare #max_marker int
set #max_marker=isnull((select max(marker) from table),0)
--Insert comes here
Insert into table (Marker,Name,Location) Values(#max_marker+1,'Andy Jones','Mixed')

shop that has served more than 3 people query sql

I have a table called frequents that has two columns name and pizzeria. Each name is linked to a pizzeria. Some names are mentioned more than once since they are linked to different pizzerias. I need help writing a query that shows all the pizzerias that have served more than 3 people. Thank you.
Name Pizzareia
Amy Pizza Hut
Ben Pizza Hut
Ben Chicago Pizza
Cal Straw Hat
Cal New York Pizza
Dan Straw Hat
Dan New York Pizza
Eli Straw Hat
Eli Chicago Pizza
Fay Dominos
Fay Little Caesars
Gus Chicago Pizza
Gus Pizza Hut
Hil Dominos
Hil Straw Hat
Hil Pizza Hut
Ian New York Pizza
Ian Straw Hat
Ian Dominos
And the query:
SELECT name, count(pizzeria)
FROM frequency
GROUP BY name
HAVING COUNT(pizzeria) >= 3
The result is supposed to show the pizzeria where its name has come up more than 3 times
I need help writing a query that shows all the pizzerias that have served more than 3 people
You need to GROUP BY pizzeria, not by name:
SELECT pizzeria FROM frequency GROUP BY pizzeria HAVING COUNT(*) >= 3

SQL Server : count items in different columns and group them

Looked through a few posts and haven't found an workable answer to this yet.
Example of a table I have:
Item 1 | Item 2 | Item 3 | Item 4 |
-----------------------------------------------------
Hamburger Fries Soda Salad
Fries Hamburger Soda
Salad Soda Soda
Hamburger Fries
Then I'm trying to count and group them so they show up like this.
4 Soda
3 Hamburger
3 Fries
2 Salad
Unpivot the data and do the count
select Items,count(1)
from yourtable
cross apply(values (Item1),(Item2),(Item3),(Item4)) CA (items)
Group by Items

Get data from string of specific values SQL

I'm rather new at SQL programming, and still struggling with the basics. I need to extract some specific rows, from a specified string of IDs.
ID Product City
1 Apple London
2 Banana Berlin
3 Orange Berlin
4 Orange Paris
5 Apple Paris
6 Banana Copenhagen
7 Banana Copenhagen
8 Banana London
9 Apple Paris
10 Orange London
11 Apple Berlin
12 Apple Copenhagen
13 Apple Paris
If I need to select ID=1,2,5,6,10,11,13 how do I extract these specific rows from the database?
I'm using SQLite.
Thanks in advance.
You should use the in clause
select * from your_table
where id in (1,2,5,6,10,11,13)

Why does my view query split into two?

I am trying to create a view that records the selected attributes for all Computer Science majors.
This is my query to create a view:
DROP VIEW CS_grade_report;
CREATE VIEW CS_grade_report AS
SELECT Student.student_id AS "ID",
student_name AS "Name",
course_number AS "Course #",
credit AS "Credit",
grade AS Grade
FROM Student, Class, Enrolls
WHERE major = 'CSCI'
AND Student.student_id = Enrolls.student_id
AND Class.schedule_num = Enrolls.schedule_num;
SELECT *
FROM CS_grade_report;
And this is what is generated:
ID Name Course # Credit GR
------ ------------------------- -------- ---------- --
600000 John Smith CSCI3200 4 B+
600000 John Smith CSCI3700 3 C
600000 John Smith SPAN1004 3 A-
600000 John Smith CSCI4300 3 A+
600001 Andrew Tram MUSC2406 2 A+
600001 Andrew Tram SPAN1004 3 A
600001 Andrew Tram CSCI3700 3 B-
600002 Jane Doe CSCI4200 3 D+
600003 Michael Jordan CSCI4300 3 A+
600004 Tiger Woods MUSC1000 1 A
600007 Dominique Davis CSCI4300 3 F
ID Name Course # Credit GR
------ ------------------------- -------- ---------- --
600009 Will Smith CSCI3200 4 A
600010 Papa Johns CSCI3200 4 B
600011 John Doe CSCI3200 4 C
600012 Jackie Chan CSCI3200 4 D
600013 Some Guy CSCI3200 4 E
16 rows selected.
I am assuming this is output from sqlplus. There is a "pagesize" option to define when breaks are added. If you only want to see one heading, set the size to a large enough value prior to running your SELECT statement as such:
set pagesize 500
(or whatever size you want)
There are many command options for sqlplus. This link is a good cheat-sheet.