Check between two columns in two different tables in sql server? - sql

Hi i have two tables named as ITEM_DETAILS AND SALES in my database both of them have QUANTITY columns.
I want to make a check that my SALES.QUANTITY table becomes dependent on ITEM_DETAILS.QUANTITY and I want the check that if ITEM_DETAILS.QUANTITY is zero than it does not allow me to SALE any QUANTITY from SALES.QUANTITY.
How I could do that?
I have a check constraint on :
ITEM_DETAILS.QUANTITY>=0
but how sales quantity will become dependent on this?

Related

SSAS - Cannot create relationships due to dupicate column entries

I am currently building a tabular cube in SSAS, however I'm having issues when creating relationships.
I have 3 tables
Master
Supplier
Customer
In the master table I have a list of unique IDs, these ID's appear in the other 2 tables (there can be duplicate records for the ID in the Supplier and Customer table).
What I'm tring to do is create a relationship between the Master table and the Supplier and Customer table. One to Many. But SSAS gives me an error The relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values.
Any advise would be greatly appreciated
Based on the error message it seems to be there are some duplicate values in the master table within the column that is being selected for relationship.

How to add constraints to make sure a customer can only purchase a certain number of product - Postgresql

I am trying to write a Check to make sure that a customer can only buy 10 of a certain product type (smartphone) and 4 of another product type(tv), and buy another product only during morning(before 12:30). The table is
invoice(PK invoice_no, FK product, amount, customer_name,time, FK opening_time )
Check (amount >10) would check the amount but I want to limit only a few types of products. Also product type isn't in my invoice table, should I add that to the table or can I join another table (Product) in check?
I guess this kind of validation / restriction is better fit to your application's business level rather then your data / database level

How do you make a SQL table reflect vertical records in a horizontal fashion

I have two tables, one has sales records and one has deduct records. There is a primary key that links the two tables called Sales_ID but there are multiple deduct records that belong to one sales record. I am trying to get the sales information and the deduct information on one line. See below for tables and the desired result.
Sales Table:
Deducts table:
Desired results:
SELECT SALES.SALES_ID, SALES.SALE_QTY, SALES.SALE_AMT
FROM SALES
LEFT JOIN DEDUCTS
ON SALES.SALES_ID = DEDUCTS.SALE_ID
I understand that If I add a deduct code in the select statement I will get duplicates, but I don't know what to try to avoid that.
thanks in advance!

How can I create a square matrix table in SQL?

I'm working on a website in Laravel where we have realised that what we want to do is setup a matrix where the rows contain buyer IDs and the columns contain seller IDs. Each user on this website is both a buyer and a seller. And in each (buyer,seller) cell contains the quantity that the buyer has bought from that respective seller. This matrix is ordered so that the buyer on the 7th row is also a seller on the 7th column and it also needs to be dynamic in the sense that a buyer and seller can be added or removed. I'm new to SQL and eloquent so how could I go about creating this?
Don't!
Use a many to many relationship on the Users table to realize this. You can use a primary key on the table consisting of buyer_id and seller_id to prevent duplicates. And you can add an extra field to the pivot table to keep track of the quantity.
The database structure of your application should never be changed because of data.

Nesting Tables in SQL Server Express

I am creating a purchase order system where someone can store details of their purchase. So I created a database, with tables for supplier information from which we will buy stuff and another table where we store what we are buying.
In the PurchaseOrder table, there are columns for :
PurchaseOrderNo (Primary)
BuyerInitials
DateOfPurchase
ProjectCode
Items (linking to the PurchaseItems table)
I want to add another column with Items' details, so I created another table PurchaseItems which has the following columns:
PurchaseOrderNo. (this would be repeat for each part)
PartNo
Description
Quantity
UnitAmount
VATAmount
TotalAmount
Logically it seems simple, but I can't seem to get my head around on how I would link the tables. Thank you very much for the help :)
Create a seperate table say OrderedItems which maps the PartNo and PurchaseOrderNo. This avoids the repetition of PurchaseOrderNo in each row and also maintains the relation between PurchaseOrder and PurchaseItems table.