Reading through a table row by row in SQL Server 2000 - sql-server-2000

I have a table that I want to read row by row to search for a specific match.
Here are the result of my table and the columns. Pos_Scan represents what is scanned at the point of sale and how many
UniqueID INGREDINETID KITCHENITEMID QUANTITY POS_SCAN STATUS
1 510 0111 1 5 0
2 54491472 0111 1 2 0
3 6001069201906 6006749006031 1 0 0
4 54491472 6006749006031 1 5 0
HAS more than these records here what i want to do is to be able to loop through this table and pull out rows that have the same kitchenitemid and have pos_scan >= quantity this is to form a complete recipe for example row 1 and 2 will be a complete recipe but row 3 and 4 will not be a recipe because pos_scan for ingredientid at uniqueid 3 is 0
please help me
thank you

If my reconstructed requirement suggestions in the comments are close to right, then something like the following would do:
SELECT
KitchenItemID,MIN(Satisfied),MAX(Satisfied)
FROM
(
SELECT
KitchenItemID,
CASE WHEN pos_scan >= Quantity THEN 1 ELSE 0 END as Satisfied
FROM
OriginalTableNotNamdInQuestion
) t
GROUP BY
KitchenItemID
HAVING
MIN(Satisfied) = MAX(Satisfied) --Change this to < to find incomplete "recipes"

Related

SQL Server : how can I get difference between counts of total rows and those with only data

I have a table with data as shown below (the table is built every day with current date, but I left off that field for ease of reading).
This table keeps track of people and the doors they enter on a daily basis.
Table entrance_t:
id entrance entered
------------------------
1 a 0
1 b 0
1 c 0
1 d 0
2 a 1
2 b 0
2 c 0
2 d 0
3 a 0
3 b 1
3 c 1
3 d 1
My goal is to report on people and count entrances not used(grouping on people), but ONLY if they entered(entered=1).
So using the above table, I would like the results of query to be...
id count
----------
2 3
3 1
(id=2 did not use 3 of the entrances and id=3 did not use 1)
I tried queries(some with inner joins on two instances of same table) and I can get the entrances not used, but it's always for everybody. Like this...
id count
----------
1 4
2 3
3 1
How do I not display results id=1 since they did not enter at all?
Thank you,
You could use conditional aggregation:
SELECT id, count(CASE WHEN entered = 0 THEN 1 END) AS cnt
FROM entrance_t
GROUP BY id
HAVING count(CASE WHEN entered = 1 THEN 1 END) > 0;
DBFiddle Demo

How to arrange Sql rows in a specific format

I have table with up to 50 rows... like given below.
ID menu dispOdr ParntID
---------------------
1 abc 1 0
2 cde 2 0
3 fgh 1 2
4 ghdfdj 2 2
5 tetss 1 1
6 uni 3 0
but I want to be sorted
Like
ID menu dispOdr ParntID
---------------------
1 abc 1 0
5 tetss 1 1
2 cde 2 0
3 fgh 1 2
4 ghdfdj 2 2
6 uni 3 0
If have any query please let me know.. thanks in advance.
I am using sql server 2014
I think you need your current vs. desired output reversed. You say you want the menu column sorted, but it appears that it already is.
So assuming you are actually starting with the second table, you can sort the menu column simply using ORDER BY:
SELECT *
FROM mytable
ORDER BY menu ASC
I think that the query below produces the required output:
SELECT t1.ID, t1.menu, t1.dispOdr, t1.ParntID
FROM mytable AS t1
LEFT JOIN mytable AS t2 ON t1.ParntID = t2.ID
ORDER BY CASE
WHEN t1.ParntID = 0 THEN t1.dispOdr
ELSE t2.dispOdr
END,
CASE
WHEN t1.ParntID = 0 THEN 1
ELSE 2
END,
t1.dispOdr
The first CASE expression groups records according to the dispOdr of their parent. The second CASE places parent on the top of its subgroup. Finally, the last expression used in the ORDER BY clause orders all child records within a subgroup.
Note: The above query works with one level of nesting.

Insert missing row(s)

I have a table of supply/price curve, where for some items the zero price row is missing, and I want to insert that row for those items. The table looks like this:
Item Point Price Quantity
-----------------------------
A 1 0 0
A 2 100 5
A 3 200 10
B 1 50 6
B 2 70 8
The number of rows per item can be different (like 3 points for item A, and less or more points for another item). In the above, the zero price and quantity point is missing for item B, so the updated table should look like this:
Item Point Price Quantity
-----------------------------
A 1 0 0
A 2 100 5
A 3 200 10
B 1 0 0
B 2 50 6
B 3 70 8
Where a new row (point with zero price/quantity) is added and point of the other rows is updated. The new row should be added for all items where point 1 is not Price=0 and Quantity = 0. How to do it in Oracle sql server?
Hmmm, you can do this in two steps. First, I would insert point with a value of 0 and then increment the value:
insert into t(item, point, price, quantity)
select item, 0 as point, 0 as price, 0 as quantity
from t
group by item
having min(price) <> 0;
Then, increment the point column:
update t
set point = point + 1
where exists (select 1 from t t2 where t2.item = t.item and t2.point = 0);
commit;

Inserting a new indicator column to tell if a given row maximizes another column in SQL

I currently have a table in SQL that looks like this
PRODUCT_ID_1 PRODUCT_ID_2 SCORE
1 2 10
1 3 100
1 10 3000
2 10 10
3 35 100
3 2 1001
That is, PRODUCT_ID_1,PRODUCT_ID_2 is a primary key for this table.
What I would like to do is use this table to add in a row to tell whether or not the current row is the one that maximizes SCORE for a value of PRODUCT_ID_1.
In other words, what I would like to get is the following table:
PRODUCT_ID_1 PRODUCT_ID_2 SCORE IS_MAX_SCORE_FOR_ID_1
1 2 10 0
1 3 100 0
1 10 3000 1
2 10 10 1
3 35 100 0
3 2 1001 1
I am wondering how I can compute the IS_MAX_SCORE_FOR_ID_1 column and insert it into the table without having to create a new table.
You can try like this...
Select PRODUCT_ID_1, PRODUCT_ID_2 ,SCORE,
(Case when b.Score=
(Select Max(a.Score) from TableName a where a.PRODUCT_ID_1=b. PRODUCT_ID_1)
then 1 else 0 End) as IS_MAX_SCORE_FOR_ID_1
from TableName b
You can use a window function for this:
select product_id_1,
product_id_2,
score,
case
when score = max(score) over (partition by product_id_1) then 1
else 0
end as is_max_score_for_id_1
from the_table
order by product_id_1;
(The above is ANSI SQL and should run on any modern DBMS)

Can't figure out correct SQL query for historical table records

I'm trying for hours to find out the correct SQL query to select the latest historcal record from a table (in MySQL).
In my application I'd like to keep a history of every data modification. So my idea was, instead to make an UPDATE to an existing record, I'd rather make an INSERT of a new record. Additionally there is a revision counter, which gets increased with each record modification.
This is my table:
uid rid created_by deleted revision username password admin
1 1 0 0 0 stefan abcdefg 1
2 2 1 0 0 maria bcdefgh 1
3 3 1 0 0 carl cdefghi 0
4 4 1 0 0 SUSANN ABC123 0
5 4 1 0 1 SUSANN 123ABC 0
6 4 1 0 2 SUSANN 123ABC 1
7 4 1 1 3 SUSANN 123ABC 1
Note the rows with uid 4 to 7 are actually the same record, namely of "SUSANN". Row 4 is the initial row. Row 5 modified tha password, row 6 modifies the admin-flag, row 7 modified the deleted-flag.
uid is an auto-incrementor and identifies the row in the table for internal purposes. rid is the actual record-ID.
Now. Selecting the most current revision of a single record could be done this way:
SELECT * FROM table
WHERE rid=4
ORDER BY revision DESC
LIMIT 1
My problem is selecting a list of all the latest revision of all logins: Based on the sample data the result set should be:
uid rid created_by deleted revision username password admin
1 1 0 0 0 stefan abcdefg 1
2 2 1 0 0 maria bcdefgh 1
3 3 1 0 0 carl cdefghi 0
7 4 1 1 3 SUSANN 123ABC 1
Could someone point me in the right direction. I think the right keywords would be sufficient already. From there I could probably figure out a way.
Thanks.
This should work:
SELECT t.*
FROM table t
JOIN (SELECT rid, MAX(revision) MaxRevision FROM table GROUP BY rid) mt
ON t.rid = mt.rid AND t.revision = mt.MaxRevision