Advertisement
junbjn98

SQL

Apr 3rd, 2022
5,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.02 KB | None | 0 0
  1. --7/
  2. DECLARE @maSpTiviBanCuoiNam VARCHAR(255);
  3. DECLARE @maSpTiviBanDauNam VARCHAR(255);
  4.  
  5. SELECT @maSpTiviBanCuoiNam = STRING_AGG(CHITIETHOADON.masp, ', ')
  6.    FROM CHITIETHOADON
  7.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  8.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  9.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.maloaisp
  10.    WHERE LOAISANPHAM.tenloaisanpham = 'TIVI'
  11.      AND ngaythanhtoan >= '2021-07-01'
  12.      AND ngaythanhtoan <= '2021-12-31'
  13.  
  14. SELECT @maSpTiviBanDauNam = STRING_AGG(CHITIETHOADON.masp, ', ')
  15.    FROM CHITIETHOADON
  16.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  17.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  18.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  19.    WHERE LOAISANPHAM.tenloaisanpham = 'TIVI'
  20.      AND ngaythanhtoan >= '2021-01-01'
  21.      AND ngaythanhtoan <= '2021-06-30'
  22.  
  23. -- SELECT @maSpTiviBanCuoiNam
  24. -- SELECT @maSpTiviBanDauNam
  25.  
  26. SELECT *
  27. FROM SANPHAM
  28. WHERE @maSpTiviBanCuoiNam LIKE CONCAT('%', masp, '%')
  29.   AND @maSpTiviBanDauNam NOT LIKE CONCAT('%', masp, '%')
  30.  
  31. --8/
  32. SELECT TOP 5 SANPHAM.MaSP,
  33.            SANPHAM.tensp,
  34.            SUM(CHITIETHOADON.soluongban) AS SL
  35. FROM CHITIETHOADON
  36. INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  37. INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  38. INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  39. GROUP BY SANPHAM.MaSP,
  40.          SANPHAM.tensp
  41. ORDER BY SL DESC
  42.  
  43. --9/
  44. SELECT HOADON.mahd,
  45.        HOADON.manv,
  46.        HOADON.makh,
  47.        HOADON.ngaythanhtoan,
  48.        HOADON.phuongthucthanhtoan,
  49.        CHITIETHOADON.SoLuongBan * SANPHAM.GiaBan AS TongTien
  50. FROM CHITIETHOADON
  51. INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  52. INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  53.  
  54. -- 10/
  55. DECLARE @spBanQ32020 VARCHAR(255);
  56.  
  57. SET @spBanQ32020 =
  58.   (SELECT STRING_AGG(CHITIETHOADON.masp, ', ')
  59.    FROM CHITIETHOADON
  60.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  61.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  62.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  63.    WHERE ngaythanhtoan >= '2020-10-01'
  64.      AND ngaythanhtoan <= '2020-12-31' );
  65.  
  66. DECLARE @spBan4Lan2021 VARCHAR(255);
  67.    
  68. WITH temp AS (
  69.    SELECT CHITIETHOADON.masp,SUM( CHITIETHOADON.soluongban) AS SoLuong
  70.    FROM CHITIETHOADON
  71.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  72.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  73.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  74.    WHERE ngaythanhtoan >= '2021-01-01'
  75.    GROUP BY CHITIETHOADON.masp
  76. )
  77.  
  78. SELECT @spBan4Lan2021 = STRING_AGG(masp, ', ') FROM temp WHERE SoLuong >= 4
  79.  
  80. SELECT *
  81. FROM SANPHAM
  82. WHERE @spBanQ32020 NOT LIKE CONCAT('%', masp, '%')
  83.   AND @spBan4Lan2021 NOT LIKE CONCAT('%', masp, '%')
  84.  
  85. -- 11/
  86. WITH temp AS (
  87.   SELECT LOAISANPHAM.MaLoaiSP, LOAISANPHAM.TenLoaiSanPham, CHITIETHOADON.SoLuongBan * SANPHAM.GiaBan AS TongTien
  88.    FROM CHITIETHOADON
  89.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  90.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  91.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  92. )
  93.  
  94. SELECT MaLoaiSP, TenLoaiSanPham, SUM(TongTien) AS DoanhThu
  95.   FROM temp
  96.     GROUP BY MaLoaiSP, TenLoaiSanPham
  97.     ORDER BY DoanhThu
  98.  
  99. -- 12/
  100. DECLARE @nvBanNhieuSpNhat VARCHAR(255);
  101. DECLARE @nvBanDoanhThuCaoNhat VARCHAR(255);
  102.  
  103. WITH temp1 AS (
  104.   SELECT TOP 1 HOADON.manv, SUM(CHITIETHOADON.SoLuongBan) AS SL
  105.   FROM CHITIETHOADON
  106.   INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  107.   INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  108.   INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  109.   GROUP BY HOADON.manv
  110.   ORDER BY SL DESC
  111. ), temp2 AS (
  112.   SELECT HOADON.manv, CHITIETHOADON.SoLuongBan * SANPHAM.GiaBan AS TongTien
  113.   FROM CHITIETHOADON
  114.   INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  115.   INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  116.   INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  117. ), temp3 AS (
  118.   SELECT TOP 1 manv, SUM(TongTien) AS DoanhThu
  119.   FROM temp2
  120.   GROUP BY manv
  121.   ORDER BY DoanhThu DESC
  122. )
  123.  
  124. SELECT @nvBanNhieuSpNhat = manv FROM temp1
  125. SELECT @nvBanDoanhThuCaoNhat = manv FROM temp3
  126.  
  127. SELECT * FROM NHANVIEN WHERE MaNV = @nvBanNhieuSpNhat OR MaNV = @nvBanDoanhThuCaoNhat
  128.  
  129. -- 13/  
  130. WITH temp AS (
  131.    SELECT CHITIETHOADON.masp,SUM( CHITIETHOADON.soluongban) AS SoLuong
  132.    FROM CHITIETHOADON
  133.    INNER JOIN HOADON ON CHITIETHOADON.MaHD = HOADON.mahd
  134.    INNER JOIN SANPHAM ON CHITIETHOADON.masp = SANPHAM.masp
  135.    INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  136.    GROUP BY CHITIETHOADON.masp
  137. )
  138.  
  139. SELECT SANPHAM.masp, SANPHAM.tensp, LOAISANPHAM.tenloaisanpham, SANPHAM.soluongtrongkho - temp.SoLuong AS SoLuongConLai
  140.   FROM SANPHAM
  141.   INNER JOIN temp ON SANPHAM.masp = temp.masp
  142.   INNER JOIN LOAISANPHAM ON LOAISANPHAM.MaLoaiSP = SANPHAM.MaLoaiSP
  143.  
  144. -- 14/
  145. UPDATE KHACHHANG
  146. SET DiaChi = 'Da Nang'
  147. WHERE DiaChi = 'Quang Nam' AND makh IN (SELECT MaKH FROM HOADON GROUP BY MaKH HAVING COUNT(*) >= 3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement