Skip to content

Tag: sql

Database join where

Database Users id lastname firstname 1 Sardor Sattarov 2 Nurmuhammad To’xtayev 3 Jasur Sattarov Group_items id student_id group_id 1 2 55 2 1 55 3 2 11 Return example 1 condition users.id == group_items.student_id do not publish a table that satisfies this desire group_items.id == 55 id lastname firstname 3 J…

How can I limit the max value of an SQL field?

I am trying to force the value of the field PRICE to be under 500 if is it paid with cash (PAYMENT = “CASH”), to make impossible to insert anything over that. The table (SALES) is something like this: ID (PK) [INT] | DATE [VARCHAR] | SHOP [VARCHAR] | PRICE [FLOAT] | PAYMENT [VARCHAR] I am almost n…

DB2 SQL query to SUM the column and Subtract column

I have TRAN_AM (Transaction amount) and DB_CRD_ID(Debit Credit ind). I have to SUM the TRAN_AM based on the credit debit indicator in a single query. Input: My output should be 10, D. Also have to manage negative if Credit is higher. This query will be used as subquery, As I have to compare this amount with t…

UNION ALL Slower than N queries

This question is following this question where I wanted to select the MAX value of multiples fields while retrieving each row. The accepted answer with UNION ALL worked like a charm but I now have some scaling issues. To give some context, I have more than 3 million rows in my matches table and the filters us…