Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
steph94
New Member

sql statement, trying to use an alias but i cant seem to slice by alias

Hey guys, i have an sql statement that joins four tables, i'm using this data to create a dashboard in power BI. Each table has the same columns but the tables themselves get data from different sources within a hospital. For example my billing_ippharmacy table gets its data from the In Patient (ip) ,my billing_paynowpharmacy gets its data from the cash department, billing_paylaterpharmacy gets its data from people who are paying via insuarance i need to split these tables to generate the report based on where the data is coming from. Currently trying to use an alias but i cant seem to slice by alias any help would be much appreciated

SELECT SUM(CountMedicine),medicinecode, medicinename, quantity, SUM(Revnue), billdate,billtype, accountname, paymentstatus, locationname, locationcode, categoryname
FROM(
    SELECT * FROM(
        SELECT COUNT(b.medicinecode) AS CountMedicine,medicinecode, b.medicinename, b.quantity,SUM(b.amount) AS Revnue ,b.billdate,b.billtype, b.accountname, b.paymentstatus, b.locationname, b.locationcode, a.categoryname
        FROM billing_paynowpharmacy as b
        LEFT JOIN master_itempharmacy as a ON (b.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY b.billdate, b.medicinecode, a.categoryname, b.locationcode
    ) AS paynowpharmacy
    UNION ALL

    SELECT * FROM(
        SELECT COUNT(c.medicinecode) AS CountMedicine,medicinecode, c.medicinename, c.quantity,SUM(c.amount)  AS Revnue ,c.billdate,c.billtype, c.accountname, c.paymentstatus, c.locationname, c.locationcode, a.categoryname
        FROM billing_paylaterpharmacy as c
        LEFT JOIN master_itempharmacy as a ON (c.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY c.billdate, c.medicinecode, a.categoryname, c.locationcode
    ) AS paylaterpharmacy
    UNION ALL

    SELECT * FROM(
        SELECT COUNT(d.medicinecode) AS CountMedicine,medicinecode, d.medicinename, d.quantity,SUM(d.amount)  AS Revnue ,d.billdate,d.billtype, d.accountname, d.paymentstatus, d.locationname, d.locationcode, a.categoryname
        FROM billing_ippharmacy as d
        LEFT JOIN master_itempharmacy as a ON (d.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY d.billdate, d.medicinecode, a.categoryname,  d.locationcode
    ) AS ippharmacy
    UNION ALL
SELECT * FROM(
        SELECT COUNT(e.medicinecode) AS CountMedicine,medicinecode, e.medicinename, e.quantity,SUM(e.amount)  AS Revnue ,e.billdate,e.billtype, e.accountname, e.paymentstatus, e.locationname, e.locationcode, a.categoryname
        FROM billing_externalpharmacy as e
        LEFT JOIN master_itempharmacy as a ON (e.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY e.billdate, e.medicinecode, a.categoryname, e.locationcode
    ) AS externalpharmacy

)as pharmacytables
WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
GROUP BY billdate,medicinecode, categoryname,locationcode

 

1 ACCEPTED SOLUTION
Rayshin
Memorable Member
Memorable Member

Try this.

SELECT * FROM(
        SELECT COUNT(b.medicinecode) AS CountMedicine,medicinecode, b.medicinename, b.quantity,SUM(b.amount) AS Revnue ,b.billdate,b.billtype, b.accountname, b.paymentstatus, b.locationname, b.locationcode, a.categoryname
        FROM billing_paynowpharmacy as b
        LEFT JOIN master_itempharmacy as a ON (b.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY b.billdate, b.medicinecode, a.categoryname, b.locationcode
    ) AS paynowpharmacy

 ->

This way you now have an additional column to slice on, hopefully I have understood what you saying.

   SELECT * , 'paynowpharmacy' AS Source FROM(
        SELECT COUNT(b.medicinecode) AS CountMedicine,medicinecode, b.medicinename, b.quantity,SUM(b.amount) AS Revnue ,b.billdate,b.billtype, b.accountname, b.paymentstatus, b.locationname, b.locationcode, a.categoryname
        FROM billing_paynowpharmacy as b
        LEFT JOIN master_itempharmacy as a ON (b.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY b.billdate, b.medicinecode, a.categoryname, b.locationcode
    ) AS paynowpharmacy

 

View solution in original post

4 REPLIES 4
Rayshin
Memorable Member
Memorable Member

Try this.

SELECT * FROM(
        SELECT COUNT(b.medicinecode) AS CountMedicine,medicinecode, b.medicinename, b.quantity,SUM(b.amount) AS Revnue ,b.billdate,b.billtype, b.accountname, b.paymentstatus, b.locationname, b.locationcode, a.categoryname
        FROM billing_paynowpharmacy as b
        LEFT JOIN master_itempharmacy as a ON (b.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY b.billdate, b.medicinecode, a.categoryname, b.locationcode
    ) AS paynowpharmacy

 ->

This way you now have an additional column to slice on, hopefully I have understood what you saying.

   SELECT * , 'paynowpharmacy' AS Source FROM(
        SELECT COUNT(b.medicinecode) AS CountMedicine,medicinecode, b.medicinename, b.quantity,SUM(b.amount) AS Revnue ,b.billdate,b.billtype, b.accountname, b.paymentstatus, b.locationname, b.locationcode, a.categoryname
        FROM billing_paynowpharmacy as b
        LEFT JOIN master_itempharmacy as a ON (b.medicinecode = a.itemcode)
        WHERE billdate BETWEEN "2022-01-01" AND "2022-01-31"
        GROUP BY b.billdate, b.medicinecode, a.categoryname, b.locationcode
    ) AS paynowpharmacy

 

yeah it makes sense, thank you. works like a charm!

Rayshin
Memorable Member
Memorable Member

is there an error message with the SQL? is it direct query?

no error message, just need to split as per the table names or aliases atleast and it doesnt split the tables since they're all joind.

yes its a direct query

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.