Hi guys i got stuck at one very simple issue that the following two queries are working fine but i want the result of both the queries in one table ,so what would be the possible way of joining these two so that they will combine and give result in one table,i tried UNION but it didnt work.Kindly tell me the solution.Thanks!
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY
from ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL
Select RATE=(CASE WHEN Discount IS NULL OR Discount=0 THEN RATE ELSE RATE-(RATE*(Discount/100))END),
AMOUNT=(CASE WHEN Discount IS NULL OR Discount=0 THEN AMOUNT ELSE AMOUNT-(AMOUNT*(Discount/100)) END)
from ITEM_DETAILS
if it is sql server did you try something like this
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY,
CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END AS 'RATE',
CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END AS 'AMOUNT'
from ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL
I am not sure if you want all the rates and amount from the ITEM_DETAILS table or only those which satisfy the join condition
By looking at both of your queries you want a combined result. In this case you do not Need UNION. You just Need to Combine the columns as below
Select ITEM_DETAILS.ITEM_MODEL,
ITEM_DETAILS.ITEM_NAME,
ITEM_DETAILS.ITEM_DESCRIPTION,
ITEM_DETAILS.VENDOR_NAME,
ITEM_DETAILS.INVOICE_NUM,
ITEMS_MASTER.QUANTITY,
RATE=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.RATE ELSE ITEM_DETAILS.RATE-(ITEM_DETAILS.RATE*(ITEM_DETAILS.Discount/100))END),
AMOUNT=(CASE WHEN ITEM_DETAILS.Discount IS NULL OR ITEM_DETAILS.Discount=0 THEN ITEM_DETAILS.AMOUNT ELSE ITEM_DETAILS.AMOUNT-(ITEM_DETAILS.AMOUNT*(ITEM_DETAILS.Discount/100)) END)
FROM ITEM_DETAILS
inner join ITEMS_MASTER
on ITEM_DETAILS.ITEM_MODEL=ITEMS_MASTER.ITEM_MODEL
Related
I have a table that is for PaymentRequest (PR), each PR table has two forms of pay, a WorkProgress, and a AdvanceByWarranty, the relation is like this:
I Need to create a report to get all the payment made in that (PR), and some other fields between them
The sql that I was using to join PaymentRequest with WorkProgress is this, and it works, it returns the WorkProgress of that payment
To get the Advances by warranty I use this, it also works, returns 2 Advances, as it should be
But, when I mix both, it doesnt return 3 rows, as it should be, it returns two. The result is this
I was expecting something like this (With shorter names)
How can i get the expected query?
Edit:
The sql to get the expected query is this
select
pr.ProjectId, pr.NumberPaymentState,
wp.ToCollectAmmount as WPAmmount, wp.ToCollectPercent as WPPercent,
null as AWAmmount, null as AWPercent
from PaymentRequests pr
left join WorkProgresses wp on (wp.ProjectId = pr.ProjectId and wp.NumberPaymentState = pr.NumberPaymentState)
union all
select
pr.ProjectId, pr.NumberPaymentState,
null as WPAmmount, null as WPPercent,
aw.ToCollectAmmount as AWAmmount, aw.ToCollectPercent as AWPercent
from PaymentRequests pr
left join AdvanceByWarranties aw on (aw.ProjectId = pr.ProjectId and aw.NumberPaymentState = pr.NumberPaymentState)
You might have wanted what you show, but it doesn't work like this.
Just use UNION ALL between your first two queries, and rename the columns accordingly. Here is some pseudocode hybrid to guide you:
select keycols, workercols, null as advancecols
from pr join worker
union all
select keycols, null as workercols, advancecols
from pr join advancecols
im looking for solutions to my problem.
i have a query
select em_name, sum(abs_day_left)
from pp_employees,
pp_types_abs,
pp_abs
where em_id = abs_em_id and abs_abs_id = abs_id and
abs_kod in ('12','13','14','15')
group by em_name
i want to make more columns with another abs_kod number (image attachment)
for example
second column
... abs_kod in
('656','44','323','33')
third column
... abs_kod in
('63','55','565','556')
and more..
example table
thanks for help and nice weekend
One more thing...
the formula counts days from the whole month
how to make it count correctly the days when it sets the parameters for the half month, for example from 1980-01-01 to 1980-03-15
thanks in advance
bob
I think that you are looking for conditional aggregation:
select
em_name,
sum(case when abs_kod in (12,13,14,15) then abs_day_left end) abs_day_left_1,
sum(case when abs_kod in (656,44,323,33) then abs_day_left end) abs_day_left_2,
sum(case when abs_kod in (63,55,565,556) then abs_day_left end) abs_day_left_3
from pp_employees
inner join pp_abs on em_id = abs_em_id
inner join pp_types_abs on abs_abs_id = abs_id
where and abs_kod in (12,13,14,15,656,44,323,33,63,55,565,556)
group by em_name
Notes:
always use explicit joins instead of old-shool, implicit joins - I tried to fix this but I am unsure I did it correctly, for a reason that lies in the following point...
always qualify the columns in the query with the table they belong to
My query below is doubling the amount on the last record it returns. I have 3 tables - activities, bookings and tempbookings. The query needs to list the activities and attached information and pull the total number (using the SUM) of places booked (as BookingTotal) from the booking table by each activity and then it needs to calculate the same for tempbookings (as tempPlacesReserved) providing the reservedate field inside that table is in the future.
However the first issue is that if there are no records for an activity in the tempbookings table it does not return any records for that activity at all, to get around this i created dummy records in the past so that it still returns the record, but if I can make it so I don't have to do this I would prefer it!
The main issue I have is that on the final record of the returned results it doubles the booking total and the places reserved which of course makes the whole query useless.
I know that I am doing something wrong I just haven't been able to sort it, I have searched similar issues online but am unable to apply them to my situation correctly.
Any help would be appreciated.
P.S. I'm aware that normally you wouldn't need to fully label all the paths to the databases, tables and fields as I have but for the program I am planning to use it in I have to do it this way.
Code:
SELECT [LeisureActivities].[dbo].[activities].[activityID],
[LeisureActivities].[dbo].[activities].[activityName],
[LeisureActivities].[dbo].[activities].[activityDate],
[LeisureActivities].[dbo].[activities].[activityPlaces],
[LeisureActivities].[dbo].[activities].[activityPrice],
SUM([LeisureActivities].[dbo].[bookings].[bookingPlaces]) AS 'bookingTotal',
SUM (CASE WHEN[LeisureActivities].[dbo].[tempbookings].[tempReserveDate] > GetDate() THEN [LeisureActivities].[dbo].[tempbookings].[tempPlaces] ELSE 0 end) AS 'tempPlacesReserved'
FROM [LeisureActivities].[dbo].[activities],
[LeisureActivities].[dbo].[bookings],
[LeisureActivities].[dbo].[tempbookings]
WHERE ([LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[bookings].[activityID]
AND [LeisureActivities].[dbo].[activities].[activityID]=[LeisureActivities].[dbo].[tempbookings].[tempActivityID])
AND [LeisureActivities].[dbo].[activities].[activityDate] > GetDate ()
GROUP BY [LeisureActivities].[dbo].[activities].[activityID],
[LeisureActivities].[dbo].[activities].[activityName],
[LeisureActivities].[dbo].[activities].[activityDate],
[LeisureActivities].[dbo].[activities].[activityPlaces],
[LeisureActivities].[dbo].[activities].[activityPrice];
Your current query is using an INNER JOIN between each of the tables so if the tempBookings table has no records, you will not return anything.
I would advise that you start to use JOIN syntax. You might also need to use subqueries to get the totals.
SELECT a.[activityID],
a.[activityName],
a.[activityDate],
a.[activityPlaces],
a.[activityPrice],
coalesce(b.bookingTotal, 0) bookingTotal,
coalesce(t.tempPlacesReserved, 0) tempPlacesReserved
FROM [LeisureActivities].[dbo].[activities] a
LEFT JOIN
(
select activityID,
SUM([bookingPlaces]) AS bookingTotal
from [LeisureActivities].[dbo].[bookings]
group by activityID
) b
ON a.[activityID]=b.[activityID]
LEFT JOIN
(
select tempActivityID,
SUM(CASE WHEN [tempReserveDate] > GetDate() THEN [tempPlaces] ELSE 0 end) AS tempPlacesReserved
from [LeisureActivities].[dbo].[tempbookings]
group by tempActivityID
) t
ON a.[activityID]=t.[tempActivityID]
WHERE a.[activityDate] > GetDate();
Note: I am using aliases because it is easier to read
Use new SQL-92 Join syntax, and make join to tempBookings an outer join. Also clean up your sql with table aliases. Makes it easier to read. As to why last row has doubled values, I don't know, but on off chance that it is caused by extra dummy records you entered. get rid of them. That problem is fixed by using outer join to tempBookings. The other possibility is that the join conditions you had to the tempBookings table(t.tempActivityID = a.activityID) is insufficient to guarantee that it will match to only one record in activities table... If, for example, it matches to two records in activities, then the rows from Tempbookings would be repeated twice in the output, (causing the sum to be doubled)
SELECT a.activityID, a.activityName, a.activityDate,
a.activityPlaces, a.activityPrice,
SUM(b.bookingPlaces) bookingTotal,
SUM (CASE WHEN t.tempReserveDate > GetDate()
THEN t.tempPlaces ELSE 0 end) tempPlacesReserved
FROM LeisureActivities.dbo.activities a
Join LeisureActivities.dbo.bookings b
On b.activityID = a.activityID
Left Join LeisureActivities.dbo.tempbookings t
On t.tempActivityID = a.activityID
WHERE a.activityDate > GetDate ()
GROUP BY a.activityID, a.activityName,
a.activityDate, a.activityPlaces,
a.activityPrice;
I have to provide counts for different activities, some of the columns are from some table but few I have to take by joining other tables. And in the last column I have to add counts *of 5 columns togather in one field*.Please see my query below and advice the best way to achieve my results :)
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
**SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT +c.count(*))as Total** --------- This is where I am getting stuck and confused???
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
I am really stuck as the table which is joined doesn'thave field named activity_count or activity_type so to see the counts I can only do count(*) but to add it all other fileds in one column and sum it up is very confusing??
Any help??/
SELECT web. OID,web. MARKETING_GROUP,
SUM(DECODE(WEB.EVENT_TYPE,5,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONCOMMENT ,
SUM(DECODE(WEB.EVENT_TYPE,6,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONSTART ,
SUM(DECODE(WEB.EVENT_TYPE,7,WEB.ACTIVITY_COUNT,0)) AS DISCUSSIONVIEW,
SUM(case when o.when _clicked is not null then c(*))AS clickcount,
SUM(case when WEB.EVENT_TYPE in(5,6,7,8)then WEB.ACTIVITY_COUNT END) +c.count(*) as Total
from GMMI_AIR.WEB_ACTIVITY_FCT WEB join GMMI_AIR.COUPON_WEB_ACTIVITY C
on WEB.OID_WEB_ACTIVITY_FCT = C.OID_COUPON_WEB_ACTIVITY
GROUP BY web. OID,web. MARKETING_GROUP;
I normally work with MySQL so not sure how to get a query to work on SQL.
I have 3 tables:
Table1
ODBC_ORDER_LINE_ALL
This contains ORDER_LINE_NETT_TOTAL and also the ORDER_LINE_ORDER_ID
Table 2
ODBC_ORDER_ALL
This contains the ORDER_ID and also the ORDER_ACCOUNT_ID
Table 3
ODBC_ACCOUNT
This contains the ACCOUNT_ACCID and some other information on the account we may need in the future, hence why we are adding it.
I am joining table 1,2,3 together using the above constraints.
What I want to be able to do is generate a Sales Report of Spend showing Account Number and Total Spend but I can't seem to get the syntax correct.
Any help much appreciated.
Here are the tables:
ODBC_ORDER_LINE_ALL
ORDER_LINE_ID
ORDER_LINE_ORDER_ID
ORDER_LINE_DATE
ORDER_LINE_MEDIACODE
ORDER_LINE_SOURCE
ORDER_LINE_PRODUCT_ID
ORDER_LINE_PRODUCT_CODE
ORDER_LINE_PRODUCT_NAME
ORDER_LINE_QUANTITY
ORDER_LINE_NETT_PRICE
ORDER_LINE_VAT_CODE
ORDER_LINE_VAT_RATE
ORDER_LINE_VAT
ORDER_LINE_NETT_TOTAL
ORDER_LINE_VAT_TOTAL
ORDER_LINE_GROSS_TOTAL
ORDER_LINE_UNIT_COST
ORDER_LINE_COST_CURRENCY
ORDER_LINE_COST_EXCHANGE_RATE
ORDER_LINE_TYPE
ORDER_LINE_STAGE
ORDER_LINE_STAGE_DATE
ORDER_LINE_INVOICE_ID
ORDER_LINE_INVOICE_DATE
ORDER_LINE_KIT_COMPONENT
ORDER_LINE_KIT_LINE_ID
ORDER_LINE_USER
ODBC_ORDER_ALL
ORDER_ID
ORDER_TYPE
ORDER_MEDIA_CODE_ID
ORDER_MEDIA_CODE
ORDER_MEDIA_SUBCODE
ORDER_TAKEN_BY
ORDER_DATE
ORDER_ACCOUNT_ID
ORDER_DELIVERY_ACCOUNT_ID
ORDER_SOURCE_ID
ORDER_SOURCE
ORDER_WEB_ORDER_ID
ORDER_MULTI_CLIENT_ID
ORDER_MULTI_CLIENT
ODBC_ACCOUNT
ACCOUNT_ACCID
ACCOUNT_ACC_TYPE
ACCOUNT_REF
ACCOUNT_TITLE
ACCOUNT_FORENAME
ACCOUNT_SURNAME
ACCOUNT_COMPANY_NAME
ACCOUNT_HOUSE_NAME
ACCOUNT_ADD1
ACCOUNT_ADD2
ACCOUNT_ADD3
ACCOUNT_ADD4
ACCOUNT_POSTCODE
ACCOUNT_COUNTRY
ACCOUNT_PHONE1
ACCOUNT_PHONE2
ACCOUNT_FAX1
ACCOUNT_FAX2
ACCOUNT_EMAIL
ACCOUNT_WEBSITE
ACCOUNT_PRICELIST_ID
ACCOUNT_MEDIACAMPAIGN_ID
ACCOUNT_CREATED_DATE
Assuming that the table names are 'Table 1' ODBC_Order_Line_All, 'Table 2' ODBC_Order_All, 'Table 3' ODBC_Account, then you simply need to join the three tables on the relevant joining columns, and sum the order line nett total entries for each account:
SELECT A.Account_AccID, SUM(L.Order_Line_Nett_Total) AS TotalSpend
FROM ODBC_Account AS A
JOIN ODBC_Order_All AS O ON A.Account_Acc_ID = O.Order_Account_ID
JOIN ODBC_Order_Line_All AS L ON O.Order_ID = L.Order_Line_Order_ID
GROUP BY A.Account_AccID;
However, while there is nothing but the Account_AccID selected from ODBC_Account, there is no actual need to select from the ODBC_Account table; you can join just the two tables, yielding a simpler query:
SELECT O.Order_Account_ID AS Acount_AccID, SUM(L.Order_Line_Nett_Total) AS TotalSpend
FROM ODBC_Order_All AS O ON A.Account_Acc_ID = O.Order_Account_ID
JOIN ODBC_Order_Line_All AS L ON O.Order_ID = L.Order_Line_Order_ID
GROUP BY Account_AccID;
There is nothing here that is different in MySQL from any other SQL DBMS, so your knowledge of MySQL should transfer directly.