These are the questions:
Write a script that will give you six columns of output.
a) the customer first name
b) the customer last name
c) the order number
d) the item name
e) the discount
f) a column with the following words for the discount----either NONE, LESS THAN TWENTY, LESS THAN FORTY, or MORE THAN FORTY
I have done this:
SELECT
Customers.FirstName,
Customers.LastName,
Orders.OrderID,
Products.ProductName,
OrderItems.DiscountAmount,
IF(OrderItems.DiscountAmount = 0, 'NONE', IF(OrderItems.DiscountAmount <= 20, 'LESS THAN TWENTY', IF(OrderItems.DiscountAmount <= 40, 'LESS THAN FORTY', 'MORE THAN FORTY'))) AS discount
FROM
Customers
INNER JOIN
Orders ON (Customers.ShippingAddressID = Orders.ShipAddressID
AND Customers.BillingAddressID = Orders.BillingAddressID)
INNER JOIN
OrderItems ON (Orders.OrderID = OrderItems.OrderID)
INNER JOIN
Products ON (OrderItems.ProductID = Products.ProductID)
It gives me an error on my if statement? Can you see what I'm doing wrong?
This is the database I'm using:
Error messages:
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near ','.
Your database diagram and errors suggest you are using Microsoft SQL Server, not MySQL. The IF command/expression/keyword is not standardized and implemented differently in different platforms.
Therefore you should not use MySQL proprietary IF-syntax but the MSSQL proprietary IIF-syntax. IF is a control-of-flow statement in MSSQL.
This is SQL Server. There is no if statement. Use case:
(CASE WHEN OrderItems.DiscountAmount = 0 THEN 'NONE'
WHEN OrderItems.DiscountAmount <= 20 THEN 'LESS THAN TWENTY'
WHEN OrderItems.DiscountAmount <= 40 THEN 'LESS THAN FORTY'
ELSE 'MORE THAN FORTY'
END) AS discount
Use "case..when" statement here. Usage link here: case statement
Case
when OrderItems.DiscountAmount = 0 then 'NONE'
when OrderItems.DiscountAmount <= 20 then 'LESS THAN TWENTY'
when OrderItems.DiscountAmount <= 40 then 'LESS THAN FORTY'
else 'MORE THAN FORTY'
End AS discount
Related
SELECT
st.SupplyID,
FORMAT(SupplyTrDate, 'MMM dd, yyyy') AS 'Supply Date',
COUNT(SDQuantity) AS 'Total Item Purchased',
SUM(itemsSupply_price) AS 'Total Expense'
FROM
SupplyTransaction st
JOIN
SupplyTransDetail std ON st.SupplyID = std.SupplyID
JOIN
items i ON std.itemsID = i.itemsID
WHERE
SupplyTrDate % 2 = 0
AND itemsName LIKE '%A%' OR itemsName LIKE '%a%'
GROUP BY
st.SupplyID, st.SupplyTrDate, SDQuantity, itemsSupply_price, SDQuantity
HAVING
SupplyTrDate IS NOT NULL;
I get an error when running this query:
Msg 402, Level 16, State 1, Line 300
The data types date and int are incompatible in the modulo operator
I don't know what you're trying to do here - but you just cannot "divide a date by 2" - or can you tell me what March 27, 2019 divided by 2 is supposed to be??
I'm just guessing here - but if you want to check if the DAY (as a number) is an "even" day - then you'd need to apply the DAY() function to your date to make that check:
WHERE
DAY(SupplyTrDate) % 2 = 0
Also: since you're not working with an aggregated column, the condition SupplyTrDate IS NOT NULL should really be part of the WHERE clause - not the HAVING
I want to use the sum function in the ELSE part of the CASE Statement. I need to give the SUM function conditions but I'm unable to do so.
I tried to use the GROUP BY Function within the sum function but it doesn't work.
CASE WHEN SectionCrossList = NULL THEN InstructorCredits ELSE InstructorCredits/Sum(Instructorcredits) GROUP BY (AcademicYear,SectionCrossList) END NumCreditSplit
Msg 156, Level 15, State 1, Line 220 Incorrect syntax near the keyword 'GROUP'.
Msg 102, Level 15, State 1, Line 221 Incorrect syntax near 'NumCreditSplit'.
You should use CTE or window function sum
SELECT AcademicYear,SectionCrossList,
CASE
WHEN SectionCrossList = NULL
THEN
InstructorCredits
ELSE
InstructorCredits/Sum(Instructorcredits)
end NumCreditSplit
FROM <yourTable>
group by AcademicYear,SectionCrossList
I've got a stored procedure made for my Crystal Reports. It does not work yet and I haven't got an idea on how to fix this.. This is my query:
use [fmsAir];
WITH Main AS (
SELECT [File].[FileNumber], [CostRevenue].[COSTAMOUNT], [CostRevenue].[RevenueAmount], [File].[FileDate], [CostRevenue].[REVENUERATE], [CostRevenue].[CostRate], [CostRevenue].[REVENUECURRENCY],
[File].[COMPANY], [CostRevenue].[CRNUMBER], [CostRevenue].[IINVOICENUMBER], [File].[RELATIONCODE], [CostRevenue].[COSTRELATIONCODE], [CostRevenue].[REVENUEDESCRIPTION]
FROM
[fmsAir].[dbo].[File]
INNER JOIN
[fmsAir].[dbo].[CostRevenue] ON [File].[FILENUMBER] = [CostRevenue].FILENUMBER
WHERE
[File].[FILEDATE] BETWEEN '2016-08-01 00:00:00' AND '2018-01-20 00:00:00' AND
[File].[FILENUMBER] > 500
),
Relation AS (
SELECT [Relation].[Companyname], [Relation].[RELATIONCODE]
FROM [fms].[dbo].[Relation]
WHERE [Relation].[Relationcode] = Main.[File].[RELATIONCODE]
),
OutgoingInvoiceLine AS (
SELECT [OutgoingInvoiceLine].[AMOUNT], [OutgoingInvoiceLine].[RATE], [OutgoingInvoiceLine].[Currency], [OutgoingInvoiceLine].[CRNUMBER]
FROM [fmsAir].[dbo].[OutgoingInvoiceLine]
WHERE [OutgoingInvoiceLine].[CRNUMBER] = Main.[CostRevenue].[CRNUMBER]
),
IncomingInvoiceLine AS (
SELECT [IncomingInvoiceLine].[AMOUNT], [IncomingInvoiceLine].[RATE], [IncomingInvoiceLine].[CURRENCY], [IncomingInvoiceLine].[CRNUMBER]
FROM [fmsAir].[dbo].[IncomingInvoiceLine]
WHERE [IncomingInvoiceLine].[CRNUMBER] = Main.[CostRevenue].[CRNUMBER]
),
RelationCost AS (
SELECT [Relation].[CompanyNAME], [Relation].[RelationCode]
FROM [fms].[dbo].[Relation]
WHERE Relation.RELATIONCODE = Main.[CostRevenue].[COSTRELATIONCODE]
)
SELECT main.FILENUMBER, main.COSTAMOUNT from Main
Now the part where i use WHERE and then the Main.. it does not work because that is not the way you can use it, but I do not know a way on how to do it the right way either. Can anybody help me make this work?
These are the errors:
Msg 4104, Level 16, State 1, Line 16
The multi-part identifier "Main.File.RELATIONCODE" could not be bound.
Msg 4104, Level 16, State 1, Line 21 The multi-part identifier
"Main.CostRevenue.CRNUMBER" could not be bound. Msg 4104, Level 16,
State 1, Line 26 The multi-part identifier "Main.CostRevenue.CRNUMBER"
could not be bound. Msg 4104, Level 16, State 1, Line 31 The
multi-part identifier "Main.CostRevenue.COSTRELATIONCODE" could not be
bound.
Thank you in advance!
Try to Create the column name in the cte expression and use that column name in the below mentioned cte.
I hope this will help
Why your are using 4 cte if you are only fetching the result from only first cte table.
I have a SQL Server Procedure like this:
//Beginning of procedure
SELECT
CASE IDNumber
WHEN (DATALENGTH(IDNumber)>7)
THEN SUBSTRING(IDNumber,0,6)
WHEN (DATALENGTH(IDNumber) < 7)
THEN CONCAT((REPLICATE(0,7-LEN(IDNumber)),IDNumber)
END AS NID
//Rest of the procedure here
the code on execution throws the error Incorrect syntax near '>'. on line WHEN (DATALENGTH(IDNumber)>7) .
IDNumber is a nvarchar. I tried using LEN(IDNumber) but in vain.
I don't know what the error is!
Rewrite your query like that:
SELECT CASE
WHEN DATALENGTH(IDNumber) > 7 THEN SUBSTRING(IDNumber, 0, 6)
WHEN DATALENGTH(IDNumber) < 7 THEN CONCAT(REPLICATE(0, LEN(IDNumber)), IDNumber)
END AS NID;
When you write CASE Column, you have to compare it to direct values, not to an expression.
If you have case variable then you need to provide values to When part, not the condition
I've MYSQL Query, its working fine
The QUERY Is:
SELECT * , IF(totexec >= totexecrun1, totexec-totexecrun1,0) AS rewrk,
SUM(tcu) tcunit ,
IF(totexec=0, ((SUM(tcu)/totexec)*100),0) AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b
ON b.tester=a.tester
AND a.project=b.project
LEFT JOIN tc_details c
ON b.tc_id=c.tc_name
AND a.project=c.project
WHERE a.rel_name='automanual_assign'
AND a.project='JupiterQA'
GROUP BY a.tester;
I tried to execute the same Query in MSSQL but its throwing error.
ERROR IS:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ','.
Am I did anything wrong with this query?
SELECT * ,
CASE WHEN totexec >= totexecrun1 THEN totexec - totexecrun1
ELSE 0
END AS rewrk ,
SUM(tcu) tcunit ,
CASE WHEN totexec = 0 THEN ( SUM(tcu) / totexec ) * 100
ELSE 0
END AS proflevel
FROM mntest_schedule a
LEFT JOIN mnrelease_details b ON b.tester = a.tester
AND a.project = b.project
LEFT JOIN tc_details c ON b.tc_id = c.tc_name
AND a.project = c.project
WHERE a.rel_name = 'automanual_assign'
AND a.project = 'JupiterQA'
GROUP BY a.tester;
Use CASE instead of IF.
Refer this(Understanding Case Expression in SQL Server with Example) to learn CASE in SQL SERVER.
SELECT * ,
CASE WHEN (totexec >= totexecrun1)
THEN totexec-totexecrun1
ELSE 0 END AS rewrk,
SUM(tcu) tcunit ,
CASE WHEN (totexec=0)
THEN ((SUM(tcu)/totexec)*100)
ELSE 0 END AS proflevel
FROM mntest_schedule a LEFT JOIN mnrelease_details b
ON b.tester=a.tester AND a.project=b.project
LEFT JOIN tc_details c ON b.tc_id=c.tc_name AND a.project=c.project
WHERE a.rel_name='automanual_assign' AND a.project='JupiterQA'
GROUP BY a.tester;